class Tr8n::Api::Client
Constants
- API_HOST
- API_PATH
Public Class Methods
error?(data)
click to toggle source
# File lib/tr8n/api/client.rb, line 77 def self.error?(data) not data['error'].nil? end
Public Instance Methods
access_token()
click to toggle source
# File lib/tr8n/api/client.rb, line 41 def access_token return Tr8n::Session.access_token if Tr8n::Session.access_token application.access_token ||= begin token = Tr8n.cache.fetch("#{application.key}/access_token") do api('oauth/token', { :client_id => application.key, :client_secret => application.secret, :grant_type => :client_credentials }, {:method => :post}) end Tr8n::Session.access_token = token['access_token'] Tr8n::Session.access_token end end
api(path, params = {}, opts = {})
click to toggle source
# File lib/tr8n/api/client.rb, line 93 def api(path, params = {}, opts = {}) if Tr8n.session.inline_mode? return process_response(execute_request(path, params, opts), opts) end if opts[:method] == :get and opts[:cache_key] data = Tr8n.cache.fetch(opts[:cache_key]) do Tr8n.cache.read_only? ? {} : execute_request(path, params, opts) end process_response(data, opts) else process_response(execute_request(path, params, opts), opts) end end
connection()
click to toggle source
# File lib/tr8n/api/client.rb, line 85 def connection @connection ||= Faraday.new(:url => host) do |faraday| faraday.request(:url_encoded) # form-encode POST params # faraday.response :logger # log requests to STDOUT faraday.adapter(Faraday.default_adapter) # make requests with Net::HTTP end end
delete(path, params = {}, opts = {})
click to toggle source
# File lib/tr8n/api/client.rb, line 73 def delete(path, params = {}, opts = {}) api(path, params, opts.merge(:method => :delete)) end
execute_request(path, params = {}, opts = {})
click to toggle source
# File lib/tr8n/api/client.rb, line 134 def execute_request(path, params = {}, opts = {}) response = nil error = nil # oauth path is separate from versioned APIs path = prepare_api_path(path) params = params.merge(:access_token => access_token) unless path.index('oauth') if opts[:method] == :post params = params.merge(:api_key => application.key) end Tr8n.logger.trace_api_call(path, params, opts) do begin if opts[:method] == :post response = connection.post(path, params) elsif opts[:method] == :put response = connection.put(path, params) elsif opts[:method] == :delete response = connection.delete(path, params) else response = connection.get(path, params) end rescue Exception => ex Tr8n.logger.error("Failed to execute request: #{ex.message[0..255]}") error = ex nil end end raise Tr8n::Exception.new("Error: #{error}") if error if response.status >= 500 and response.status < 600 raise Tr8n::Exception.new("Error: #{response.body}") end return if response.body.nil? or response.body == '' begin data = JSON.parse(response.body) rescue Exception => ex raise Tr8n::Exception.new("Failed to parse response: #{ex.message[0..255]}") end if data.is_a?(Hash) and not data['error'].nil? raise Tr8n::Exception.new("Error: #{data['error']}") end data end
get(path, params = {}, opts = {})
click to toggle source
# File lib/tr8n/api/client.rb, line 61 def get(path, params = {}, opts = {}) api(path, params, opts.merge(:method => :get)) end
host()
click to toggle source
# File lib/tr8n/api/client.rb, line 81 def host application.host || API_HOST end
object_class(opts)
click to toggle source
# File lib/tr8n/api/client.rb, line 184 def object_class(opts) return unless opts[:class] opts[:class].is_a?(String) ? opts[:class].constantize : opts[:class] end
paginate(path, params = {}, opts = {}) { |result| ... }
click to toggle source
# File lib/tr8n/api/client.rb, line 108 def paginate(path, params = {}, opts = {}) data = get(path, params, opts.merge({'raw' => true})) while data if data['results'].is_a?(Array) data['results'].each do |result| yield(result) end else yield(data['results']) end if data['pagination'] and data['pagination']['links']['next'] data = get(data['pagination']['links']['next'], {}, opts.merge({'raw' => true})) else data = nil end end end
post(path, params = {}, opts = {})
click to toggle source
# File lib/tr8n/api/client.rb, line 65 def post(path, params = {}, opts = {}) api(path, params, opts.merge(:method => :post)) end
prepare_api_path(path)
click to toggle source
# File lib/tr8n/api/client.rb, line 128 def prepare_api_path(path) return path if path.index('oauth') return path if path.match(/^https?:\/\//) "#{API_PATH}#{path[0] == '/' ? '' : '/'}#{path}" end
process_response(data, opts)
click to toggle source
# File lib/tr8n/api/client.rb, line 189 def process_response(data, opts) return data if opts['raw'] if data.is_a?(Hash) and data['results'] #Tr8n.logger.debug("received #{data['results'].size} result(s)") return data['results'] unless object_class(opts) objects = [] data['results'].each do |data| objects << object_class(opts).new(data.merge(opts[:attributes] || {})) end return objects end return data unless object_class(opts) object_class(opts).new(data.merge(opts[:attributes] || {})) end
put(path, params = {}, opts = {})
click to toggle source
# File lib/tr8n/api/client.rb, line 69 def put(path, params = {}, opts = {}) api(path, params, opts.merge(:method => :put)) end
results(path, params = {}, opts = {})
click to toggle source
# File lib/tr8n/api/client.rb, line 57 def results(path, params = {}, opts = {}) get(path, params, opts)['results'] end