class Allegro::Client

Attributes

http_agent[R]

Public Class Methods

new(client_id, secret, _options = {}) click to toggle source
# File lib/allegro/client.rb, line 8
def initialize(client_id, secret, _options = {})
  @http_agent = Http::Agent.new(_options)
  @authorized = false
  authorize(client_id, secret)
end

Public Instance Methods

api_url(url) click to toggle source
# File lib/allegro/client.rb, line 45
def api_url(url)
  [API_URI, url.to_s].join('/')
end
auth_url(url) click to toggle source
# File lib/allegro/client.rb, line 49
def auth_url(url)
  [AUTH_URI, url.to_s].join('/')
end
authorize(client_id, secret) click to toggle source
# File lib/allegro/client.rb, line 18
def authorize(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?() click to toggle source
# File lib/allegro/client.rb, line 14
def authorized?
  @authorized
end
default_headers() click to toggle source
# 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() click to toggle source
# File lib/allegro/client.rb, line 53
def default_params
  {
    method: :get, headers: default_headers
  }
end
get(resource, params) click to toggle source
# File lib/allegro/client.rb, line 33
def get(resource, params)
  @http_agent.fetch(
    api_url(resource), default_params.merge(params)
  )
end