Class: Attune::Client
- Inherits:
-
Object
- Object
- Attune::Client
- Includes:
- Configurable
- Defined in:
- lib/attune/client.rb
Constant Summary
Constants included from Configurable
Instance Attribute Summary collapse
-
#anonymous ⇒ Object
readonly
Returns the value of attribute anonymous.
-
#entities ⇒ Object
readonly
Returns the value of attribute entities.
Attributes included from Configurable
#auth_token, #disabled, #endpoint, #exception_handler, #middleware, #timeout
Instance Method Summary collapse
- #adapter ⇒ Object
-
#get_auth_token(client_id, client_secret) ⇒ Object
Request an auth token.
- #handle_exception(e) ⇒ Object
-
#initialize(options = {}) ⇒ Object
constructor
Initializes a new Client.
- #post_form(path, params = {}) ⇒ Object
- #request(http_method, path, opts) ⇒ Object
Methods included from Configurable
Constructor Details
#initialize(options = {}) ⇒ Object
Initializes a new Client
30 31 32 33 34 35 36 |
# File 'lib/attune/client.rb', line 30 def initialize(={}) Attune::Configurable::KEYS.each do |key| send("#{key}=", [key] || Attune::Default.send(key)) end @entities = Attune::Api::Entities.new(self) @anonymous = Attune::Api::Anonymous.new(self) end |
Instance Attribute Details
#anonymous ⇒ Object (readonly)
Returns the value of attribute anonymous.
19 20 21 |
# File 'lib/attune/client.rb', line 19 def anonymous @anonymous end |
#entities ⇒ Object (readonly)
Returns the value of attribute entities.
18 19 20 |
# File 'lib/attune/client.rb', line 18 def entities @entities end |
Instance Method Details
#adapter ⇒ Object
101 102 103 104 105 106 |
# File 'lib/attune/client.rb', line 101 def adapter raise DisabledException if disabled? Faraday.new(url: endpoint, builder: middleware, request: {timeout: timeout}) do |connection| connection. :Bearer, auth_token unless !auth_token end end |
#get_auth_token(client_id, client_secret) ⇒ Object
Request an auth token
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/attune/client.rb', line 48 def get_auth_token(client_id, client_secret) raise ArgumentError, "client_id required" unless client_id raise ArgumentError, "client_secret required" unless client_secret params = {client_id: client_id, client_secret: client_secret, grant_type: :client_credentials} response = post_form("oauth/token", params) if response body = JSON.parse(response.body) if body['error'] raise AuthenticationException, body['error_description'] end body['access_token'] else # Return a new UUID if there was an exception and we're in mock mode SecureRandom.uuid end end |
#handle_exception(e) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/attune/client.rb', line 87 def handle_exception e if exception_handler == :mock nil else if e.is_a? Errno::ENOENT raise Faraday::Error::ConnectionFailed, e elsif e.response && e.response[:status] == 401 raise AuthenticationException, e else raise e end end end |
#post_form(path, params = {}) ⇒ Object
81 82 83 84 85 |
# File 'lib/attune/client.rb', line 81 def post_form(path, params={}) adapter.post(path, params) rescue Errno::ENOENT, Faraday::Error::ClientError => e handle_exception(e) end |
#request(http_method, path, opts) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/attune/client.rb', line 68 def request(http_method, path, opts) adapter_method = adapter.method(http_method.downcase) adapter_method.call do |req| req.url path req.headers['Content-Type'] = 'application/json' req.headers.merge! opts[:headers] if opts[:headers] req.params = opts[:params] if opts[:params] req.body = ::JSON.dump(opts[:body]) if opts[:body] end rescue Errno::ENOENT, Faraday::Error::ClientError => e handle_exception(e) end |