Class: Ebay::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ebay/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = nil) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
# File 'lib/ebay/client.rb', line 9

def initialize(configuration = nil)
  @configuration = configuration || Ebay.configuration
  @access_token = nil
  @token_expires_at = nil
  validate_configuration!
end

Instance Attribute Details

#configuration ⇒ Object (readonly)

Returns the value of attribute configuration.



7
8
9
# File 'lib/ebay/client.rb', line 7

def configuration
  @configuration
end

Instance Method Details

#authenticate! ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ebay/client.rb', line 16

def authenticate!
  credentials = Base64.strict_encode64("#{configuration.client_id}:#{configuration.client_secret}")

  response = self.class.post(
    "#{configuration.base_url}/identity/v1/oauth2/token",
    headers: {
      'Authorization' => "Basic #{credentials}",
      'Content-Type' => 'application/x-www-form-urlencoded'
    },
    body: "grant_type=client_credentials&scope=https://api.ebay.com/oauth/api_scope",
    timeout: configuration.timeout
  )

  case response.code
  when 200
    data = response.parsed_response
    @access_token = data['access_token']
    @token_expires_at = Time.now + (data['expires_in'] || 7200).to_i - 60
    @access_token
  else
    raise AuthenticationError, "OAuth token request failed: #{response.body}"
  end
end

#get(path, params = {}) ⇒ Object



40
41
42
# File 'lib/ebay/client.rb', line 40

def get(path, params = {})
  request(:get, path, query: params)
end

#post(path, data = {}) ⇒ Object



44
45
46
# File 'lib/ebay/client.rb', line 44

def post(path, data = {})
  request(:post, path, body: data.to_json)
end