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, #logger, #logging_enabled, #middleware, #timeout
Instance Method Summary collapse
- #adapter ⇒ Object
- #default_middleware ⇒ 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
102 103 104 105 106 107 108 |
# File 'lib/attune/client.rb', line 102 def adapter raise DisabledException if disabled? effective_middleware = middleware || default_middleware Faraday.new(url: endpoint, builder: effective_middleware, request: {timeout: timeout}) do |connection| connection. :Bearer, auth_token unless !auth_token end end |
#default_middleware ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/attune/client.rb', line 110 def default_middleware Faraday::Builder.new do |builder| # Needed for encoding of BATCH GET requests builder.use Attune::ParamFlattener builder.use Attune::CallDropping builder.request :url_encoded builder.use Attune::JsonLogger, logger if logging_enabled # Gzip requests, Faraday handles responses automatically builder.use Attune::Gzip # Allow one retry per request builder.request :retry, 1 # Raise exceptions for HTTP 4xx/5xx builder.response :raise_error builder.adapter :attune_http_persistent 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
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/attune/client.rb', line 88 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
82 83 84 85 86 |
# File 'lib/attune/client.rb', line 82 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 80 |
# 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['User-Agent'] = 'Attune RClient ' + Attune::VERSION 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 |