Class: Allegro::Client
- Inherits:
-
Object
- Object
- Allegro::Client
- Defined in:
- lib/allegro/client.rb
Instance Attribute Summary collapse
-
#http_agent ⇒ Object
readonly
Returns the value of attribute http_agent.
Instance Method Summary collapse
- #api_url(url) ⇒ Object
- #auth_url(url) ⇒ Object
- #authorize(client_id, secret) ⇒ Object
- #authorized? ⇒ Boolean
- #default_headers ⇒ Object
- #default_params ⇒ Object
- #get(resource, params) ⇒ Object
-
#initialize(client_id, secret, _options = {}) ⇒ Client
constructor
A new instance of Client.
- #search(params) ⇒ Object
Constructor Details
Instance Attribute Details
#http_agent ⇒ Object (readonly)
Returns the value of attribute http_agent.
6 7 8 |
# File 'lib/allegro/client.rb', line 6 def http_agent @http_agent end |
Instance Method Details
#api_url(url) ⇒ Object
45 46 47 |
# File 'lib/allegro/client.rb', line 45 def api_url(url) [API_URI, url.to_s].join('/') end |
#auth_url(url) ⇒ Object
49 50 51 |
# File 'lib/allegro/client.rb', line 49 def auth_url(url) [AUTH_URI, url.to_s].join('/') end |
#authorize(client_id, secret) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/allegro/client.rb', line 18 def (client_id, secret) auth_token = Base64.strict_encode64("#{client_id}:#{secret}") response = @http_agent.fetch( auth_url('token'), method: :post, grant_type: 'client_credentials', headers: default_headers.merge( authorization: "Basic #{auth_token}" ) ) @access_token = response['access_token'] @token_type = response['token_type'] @authorized = true if response && @access_token end |
#authorized? ⇒ Boolean
14 15 16 |
# File 'lib/allegro/client.rb', line 14 def @authorized end |
#default_headers ⇒ Object
59 60 61 62 63 64 |
# File 'lib/allegro/client.rb', line 59 def default_headers { accept: 'application/vnd.allegro.public.v1+json', authorization: "#{@token_type} #{@access_token}" } end |
#default_params ⇒ Object
53 54 55 56 57 |
# File 'lib/allegro/client.rb', line 53 def default_params { method: :get, headers: default_headers } end |
#get(resource, params) ⇒ Object
33 34 35 36 37 |
# File 'lib/allegro/client.rb', line 33 def get(resource, params) @http_agent.fetch( api_url(resource), default_params.merge(params) ) end |
#search(params) ⇒ Object
39 40 41 42 43 |
# File 'lib/allegro/client.rb', line 39 def search(params) @http_agent.fetch( api_url('offers/listing'), default_params.merge(params) ) end |