class FreckleApi
Coercions
should retain a proc, so that we can easily use them with Hashie's coercion methods.
E.g.:
coerce_key, :the_key, name_of_method_that_returns_proc
TODO: actual error handling
Constants
- BASE_URI
- USER_AGENT
- VERSION
Public Class Methods
new(api_key)
click to toggle source
# File lib/freckle_api.rb, line 21 def initialize(api_key) @api_key = api_key end
uri(*path)
click to toggle source
# File lib/freckle_api.rb, line 17 def self.uri(*path) URI.parse [BASE_URI, *[path]].join('/') end
Public Instance Methods
project(id)
click to toggle source
# File lib/freckle_api.rb, line 25 def project(id) Project.new(request :get, self.class.uri('projects', id)) end
projects()
click to toggle source
# File lib/freckle_api.rb, line 29 def projects request(:get, self.class.uri('projects'), coerce_to: Project) end
request(method, uri, parse: true, coerce_to: Hash, params: {}, body: {})
click to toggle source
# File lib/freckle_api.rb, line 45 def request(method, uri, parse: true, coerce_to: Hash, params: {}, body: {}) request = build_request(method, uri, params: params, body: body) response = send_request(request, uri) parse ? JSON.parse(response.body, object_class: coerce_to) : response end
timer(project)
click to toggle source
TODO: consider whether it's necessary to find a timer by ITS id, not that of the project.
# File lib/freckle_api.rb, line 35 def timer(project) project_id = project.respond_to?(:id) ? project.id : project Timer.new(request :get, self.class.uri('projects', project_id, 'timer')) end
timers()
click to toggle source
# File lib/freckle_api.rb, line 41 def timers request(:get, self.class.uri('timers'), coerce_to: Timer) end
Private Instance Methods
build_request(method, uri, params: {}, body: {})
click to toggle source
# File lib/freckle_api.rb, line 54 def build_request(method, uri, params: {}, body: {}) http_class(method).new(uri.path, headers).tap do |request| request.set_form_data(params) request.body = body.to_json if request.request_body_permitted? end end
headers()
click to toggle source
# File lib/freckle_api.rb, line 70 def headers { 'Content-Type' => 'application/json', 'User-Agent' => USER_AGENT, 'X-FreckleToken' => @api_key } end
http_class(method)
click to toggle source
# File lib/freckle_api.rb, line 66 def http_class(method) Object.const_get "Net::HTTP::#{method.to_s.capitalize}" end
send_request(request, uri)
click to toggle source
# File lib/freckle_api.rb, line 61 def send_request(request, uri) https = Net::HTTP.new(uri.host, uri.port).tap { |h| h.use_ssl = true } https.request(request) end