Class: Allegro::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/allegro/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, secret, _options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
# 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

Instance Attribute Details

#http_agentObject (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 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?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/allegro/client.rb', line 14

def authorized?
  @authorized
end

#default_headersObject



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_paramsObject



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