Last active
April 12, 2018 11:16
-
-
Save jasper-lyons/af6781e834b73bb21af95f8b4b5c9a77 to your computer and use it in GitHub Desktop.
Fluent / OOP wrapper for trello api.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'uri' | |
require 'openssl' | |
require 'json' | |
require 'time' | |
class Http | |
def initialize(headers = {}, params = {}) | |
@headers = headers | |
@params = params | |
end | |
def with_params(url, params) | |
new_params = @params. | |
merge(params || {}). | |
map do |key, value| | |
case value | |
when Array | |
# trello way of doing params | |
"#{key}=#{value.join(',')}" | |
else | |
"#{key}=#{value}" | |
end | |
end | |
"#{url}?#{new_params.join('&')}" | |
end | |
def request(method, url, params = nil, body = {}) | |
uri = URI(with_params(url, params)) | |
http = Net::HTTP.new(uri.host, uri.port) | |
if uri.scheme == 'https' | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
end | |
req = case method | |
when 'get', 'GET', :get | |
Net::HTTP::Get.new(uri) | |
else | |
raise "HTTP Method #{method} not implemented" | |
end | |
http.request(req).tap do |res| | |
unless res.kind_of?(Net::HTTPSuccess) | |
puts "#{uri} - #{res.code} - #{res.body}" | |
end | |
end | |
end | |
def get(url, params = nil) | |
request(:get, url, params) | |
end | |
end | |
module Trello | |
class Base | |
def initialize(http, data, path) | |
@http = http | |
@data = data | |
@path = path | |
@cache = {} | |
end | |
attr_accessor :data | |
def id | |
@data['id'] | |
end | |
def from_api(path) | |
"https://api.trello.com/1/#{path}" | |
end | |
def path(entity) | |
"#{@path}/#{id}/#{entity}" | |
end | |
def nested(entity, params = nil) | |
@cache.fetch(entity) do | |
path = from_api(path(entity)) | |
res = @http.get(path, params) | |
JSON.parse(res.body) | |
end | |
end | |
def ==(other) | |
self.id == other.id | |
end | |
end | |
class Member < Base | |
def initialize(http, data) | |
super(http, data, 'members') | |
end | |
def organizations | |
nested('organizations').map { |o| Organization.new(@http, o) } | |
end | |
def actions(params = nil) | |
nested("actions", params).map { |a| Action.new(@http, a) } | |
end | |
def full_name | |
@data['fullName'] | |
end | |
end | |
class Organization < Base | |
def initialize(http, data) | |
super(http, data, 'organizations') | |
end | |
def members(params = nil) | |
nested('members', params).map { |m| Member.new(@http, m) } | |
end | |
def boards | |
nested('boards').map { |m| Board.new(@http, m) } | |
end | |
def actions | |
nested("actions").map { |a| Action.new(@http, a) } | |
end | |
def display_name | |
@data['displayName'] | |
end | |
end | |
class Board < Base | |
def initialize(http, data) | |
super(http, data, 'board') | |
end | |
def actions(params = nil) | |
nested("actions", params).map { |a| Action.new(@http, a) } | |
end | |
def name | |
@data['name'] | |
end | |
end | |
class Action < Base | |
def initialize(http, data) | |
super(http, data, 'actions') | |
end | |
def type | |
@data['type'] | |
end | |
def date | |
Time.parse(@data['date']) | |
end | |
def creator | |
Member.new(@http, @data['memberCreator']) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'time' | |
require './lib/trello.rb' | |
end_time = Time.parse('2018-03-09T09:00:00Z') | |
start_time = (end_time - (60 * 60 * 24 * 14)) | |
http = Http.new({}, { | |
token: "...", | |
key: "..." | |
}) | |
me = Trello::Member.new(http, 'id' => 'me') | |
me.boards.each do |board| | |
board.actions.each do |action| | |
puts "#{actions.creator.fullName} #{action.type}" | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment