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.



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

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.



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

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



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

def client_secret
  @client_secret
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.requestObject



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

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

Instance Method Details

#grant_typeObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/peddler/token.rb', line 38

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



33
34
35
36
# File 'lib/peddler/token.rb', line 33

def request
  http_response = HTTP.post(URL, form: params)
  Response.wrap(http_response)
end