Class: Incognia::Client
- Inherits:
-
Object
- Object
- Incognia::Client
- Defined in:
- lib/incognia_api/client.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
TODO: (ok) http/adapter specific code (ok) raises network/authentication errors (ok) handles token refreshing ok future: handles retrying.
Instance Method Summary collapse
- #credentials ⇒ Object
-
#initialize(client_id:, client_secret:, host:) ⇒ Client
constructor
A new instance of Client.
- #request(method, endpoint = nil, data = nil, headers = {}) ⇒ Object
Constructor Details
#initialize(client_id:, client_secret:, host:) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/incognia_api/client.rb', line 12 def initialize(client_id:, client_secret:, host:) @client_id = client_id @client_secret = client_secret @host = host headers = { 'User-Agent' => "incognia-ruby/#{Incognia::VERSION} " \ "({#{RbConfig::CONFIG['host']}}) " \ "{#{RbConfig::CONFIG['arch']}} " \ "Ruby/#{RbConfig::CONFIG['ruby_version']}" } @connection = Faraday.new(host, headers: headers) do |faraday| faraday.request :json faraday.response :json, content_type: /\bjson$/ faraday.response :raise_error faraday.adapter Faraday.default_adapter end end |
Instance Attribute Details
#connection ⇒ Object (readonly)
TODO: (ok) http/adapter specific code (ok) raises network/authentication errors (ok) handles token refreshing ok future: handles retrying
10 11 12 |
# File 'lib/incognia_api/client.rb', line 10 def connection @connection end |
Instance Method Details
#credentials ⇒ Object
45 46 47 48 49 |
# File 'lib/incognia_api/client.rb', line 45 def credentials @credentials = request_credentials if @credentials.nil? || @credentials.stale? @credentials end |
#request(method, endpoint = nil, data = nil, headers = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/incognia_api/client.rb', line 31 def request(method, endpoint = nil, data = nil, headers = {}) json_data = JSON.generate(data) if data connection.send(method, endpoint, json_data, headers) do |r| r.headers[Faraday::Request::Authorization::KEY] ||= Faraday::Request .lookup_middleware(:authorization) .header(:Bearer, credentials.access_token) end rescue Faraday::ClientError, Faraday::ServerError => e raise APIError.new(e.to_s, e.response) rescue Faraday::Error => e raise APIError.new(e.to_s) end |