module TimestampAPI
Constants
- VERSION
Attributes
api_endpoint[RW]
api_key[RW]
verbose[RW]
Public Class Methods
request(method, path, query_params = {}, payload = {})
click to toggle source
# File lib/timestamp_api.rb, line 30 def self.request(method, path, query_params = {}, payload = {}) request_options = request_options(method, path, query_params, payload) output(request_options) if verbose response = RestClient::Request.execute(request_options) modelify(JSON.parse(response)) rescue RestClient::Forbidden raise InvalidAPIKey rescue JSON::ParserError raise InvalidServerResponse end
Private Class Methods
modelify(json)
click to toggle source
# File lib/timestamp_api.rb, line 57 def self.modelify(json) case json when Array then Collection.new(json.map { |item| modelify(item) }) when Hash then ModelRegistry.model_for(json).new(json) end end
output(request_options)
click to toggle source
# File lib/timestamp_api.rb, line 64 def self.output(request_options) print "TimestampAPI ".colorize(:red) print "#{request_options[:method].upcase} ".colorize(:yellow) full_path = request_options[:url] full_path += "?#{request_options[:headers][:params].each_with_object([]) { |p, acc| acc << "#{p[0]}=#{p[1]}" }.join("&")}" unless request_options[:headers][:params].empty? print full_path.colorize(:yellow) puts " #{request_options[:payload] unless request_options[:payload].empty?}".colorize(:green) end
request_options(method, path, query_params, payload)
click to toggle source
# File lib/timestamp_api.rb, line 43 def self.request_options(method, path, query_params, payload) { method: method, url: api_endpoint + path, payload: camelize_keys(payload).to_json, headers: { "X-API-Key" => api_key || ENV["TIMESTAMP_API_KEY"] || raise(MissingAPIKey), :accept => :json, :user_agent => "TimestampAPI Ruby gem (https://github.com/alpinelab/timestamp_api)", :params => camelize_keys(query_params) } } end