Class: Peddler::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/peddler/token.rb

Overview

Requests refresh and access tokens that authorize your application to take actions on behalf of a selling partner.

The refresh token allows you to generate access tokens. Access tokens expire one hour after they are issued.

Constant Summary collapse

Error =
Class.new(Peddler::Error)
URL =
"https://api.amazon.com/auth/o2/token"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id: ENV["LWA_CLIENT_ID"], client_secret: ENV["LWA_CLIENT_SECRET"], **options) ⇒ Token

Returns a new instance of Token.



26
27
28
29
30
# File 'lib/peddler/token.rb', line 26

def initialize(client_id: ENV["LWA_CLIENT_ID"], client_secret: ENV["LWA_CLIENT_SECRET"], **options)
  @client_id = client_id
  @client_secret = client_secret
  @options = options
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



18
19
20
# File 'lib/peddler/token.rb', line 18

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



18
19
20
# File 'lib/peddler/token.rb', line 18

def client_secret
  @client_secret
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/peddler/token.rb', line 18

def options
  @options
end

Class Method Details

.requestObject



21
22
23
# File 'lib/peddler/token.rb', line 21

def request(...)
  new(...).request
end

Instance Method Details

#grant_typeObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/peddler/token.rb', line 43

def grant_type
  if options.key?(:grant_type)
    options[:grant_type]
  elsif options.key?(:refresh_token)
    "refresh_token"
  elsif options.key?(:scope)
    "client_credentials"
  elsif options.key?(:code)
    "authorization_code"
  end
end

#requestObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/peddler/token.rb', line 32

def request
  response = HTTP.post(URL, form: params)

  if response.status.client_error?
    error = Error.build(response)
    raise error if error
  end

  response
end