Browse Source

cleanup some code

master
Richard Cagle 1 year ago
parent
commit
38cc576cfa
6 changed files with 34 additions and 21 deletions
  1. 2
    0
      Gemfile
  2. 1
    0
      Gemfile.lock
  3. 1
    0
      app.rb
  4. 18
    18
      basecamp3.rb
  5. 6
    0
      public/style.css
  6. 6
    3
      views/index.erb

+ 2
- 0
Gemfile View File

@@ -25,3 +25,5 @@ gem "activerecord", "~> 6.0"
gem "rake", "~> 13.0"

gem "sucker_punch", "~> 2.1"

gem "activesupport", "~> 6.0"

+ 1
- 0
Gemfile.lock View File

@@ -81,6 +81,7 @@ PLATFORMS

DEPENDENCIES
activerecord (~> 6.0)
activesupport (~> 6.0)
awesome_print (~> 1.8)
dotenv (~> 2.7)
oauth2 (~> 1.4)

+ 1
- 0
app.rb View File

@@ -3,6 +3,7 @@
require "dotenv/load"
require "bundler/setup"
require "active_record"
require 'active_support/time'
require "pg"
require "logger"
require "sucker_punch"

+ 18
- 18
basecamp3.rb View File

@@ -3,7 +3,7 @@
require "oauth2"

class Basecamp3
attr_accessor :client, :token, :api_url, :auth_info, :authorize_url, :request_count, :request_start
attr_accessor :client, :token, :api_url, :auth_info, :authorize_url, :request_count, :request_start, :token

def initialize
@client = OAuth2::Client.new(
@@ -17,31 +17,31 @@ class Basecamp3
@authorize_url = @client.auth_code.authorize_url(redirect_uri: ENV["REDIRECT_URL"], type: "web_server")

unless Token.first.blank?
token_record = Token.find_by(client_id: ENV["BASECAMP_CLIENT_ID"])
@token = OAuth2::AccessToken.from_hash(@client, token_record.oauth_hash)
@token = @token.refresh if @token.expired?
@token = Token.find_by(client_id: ENV["BASECAMP_CLIENT_ID"])
@ouath_token = OAuth2::AccessToken.from_hash(@client, @token.oauth_hash)
@ouath_token = @ouath_token.refresh if @ouath_token.expired?

if token_record.auth_data.nil?
token_record.auth_data = authorization
token_record.save!
if @token.auth_data.nil?
@token.auth_data = authorization
@token.save!
end

@auth_info = token_record.auth_data
@auth_info = @token.auth_data
@api_url = @auth_info["accounts"][0]["href"]
end
end

def authenticated?
return false unless @token
return false unless @ouath_token

@token = @token.refresh if @token.expired?
@ouath_token = @ouath_token.refresh if @ouath_token.expired?
true
end

def get_access_token(code)
access_token_response = @client.auth_code.get_token(code, type: "web_server", redirect_uri: ENV["REDIRECT_URL"])
@token = Token.find_or_initialize_by(client_id: ENV["BASECAMP_CLIENT_ID"])
@token.update!(
@ouath_token = Token.find_or_initialize_by(client_id: ENV["BASECAMP_CLIENT_ID"])
@ouath_token.update!(
access_token: access_token_response.token,
refresh_token: access_token_response.refresh_token,
expires_in: access_token_response.expires_in,
@@ -50,7 +50,7 @@ class Basecamp3
end

def projects
result = @token.get("#{@api_url}/projects.json")
result = @ouath_token.get("#{@api_url}/projects.json")
all_results(result)
end

@@ -58,9 +58,9 @@ class Basecamp3
todo_lists = []
projects.each do |project|
project_todo_set = project["dock"].find { |i| i["name"] == "todoset" }
todolist_url = @token.get(project_todo_set["url"]).parsed["todolists_url"]
todolist_url = @ouath_token.get(project_todo_set["url"]).parsed["todolists_url"]

todo_lists.concat(all_results(@token.get(todolist_url)))
todo_lists.concat(all_results(@ouath_token.get(todolist_url)))
end
todo_lists
end
@@ -68,14 +68,14 @@ class Basecamp3
def todo_items
todo_items = []
todo_lists.each do |list|
todo_items.concat(all_results(@token.get(list["todos_url"])))
todo_items.concat(all_results(@ouath_token.get(list["todos_url"])))
end
todo_items
end

private
def authorization
@token.get("/authorization.json").parsed
@ouath_token.get("/authorization.json").parsed
end

def all_results(result)
@@ -99,7 +99,7 @@ class Basecamp3
if result.headers["x-total-count"].to_i > results.count
until result.headers["link"].nil?
url = result.headers["link"].gsub("<", "").split(">")[0]
result = @token.get(url)
result = @ouath_token.get(url)
results.concat(result.parsed)
end
end

+ 6
- 0
public/style.css View File

@@ -1,3 +1,9 @@
@media print {
.noprint {
display: none;
}
}

body {
font-family: "Lato", sans-serif;
}

+ 6
- 3
views/index.erb View File

@@ -1,8 +1,11 @@
<h2 class="subtitle has-text-centered">
This is <%= basecamp.auth_info["accounts"][0]["name"] %>
<i class="fas fa-sync"></i>
<% if Token.find_by(client_id: ENV["BASECAMP_CLIENT_ID"]).synced_at %>
<br><small class="is-size-7">Last Sync: <em><%= Token.find_by(client_id: ENV["BASECAMP_CLIENT_ID"]).synced_at %></em></small>
<i class="fas fa-sync noprint"></i>
<% if basecamp.token.synced_at %>
<br>
<small class="is-size-7">
Last Sync: <em><%= basecamp.token.synced_at.in_time_zone(ENV.fetch("TIMEZONE")).strftime("%m/%d/%Y %r") %></em>
</small>
<% end %>
</h2>


Loading…
Cancel
Save