Class: Peddler::LWA

Inherits:
Object
  • Object
show all
Defined in:
lib/peddler/lwa.rb,
sig/peddler/lwa.rbs

Overview

Requests Login with Amazon (LWA) access tokens for SP-API authorization

Constant Summary collapse

URL =

Returns:

  • (String)
"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) ⇒ LWA

Returns a new instance of LWA.

Parameters:

  • client_id: (String, nil) (defaults to: ENV["LWA_CLIENT_ID"])
  • client_secret: (String, nil) (defaults to: ENV["LWA_CLIENT_SECRET"])
  • options (Object)


25
26
27
28
29
# File 'lib/peddler/lwa.rb', line 25

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_id ⇒ String? (readonly)

Returns the value of attribute client_id.

Returns:

  • (String, nil)


17
18
19
# File 'lib/peddler/lwa.rb', line 17

def client_id
  @client_id
end

#client_secret ⇒ String? (readonly)

Returns the value of attribute client_secret.

Returns:

  • (String, nil)


17
18
19
# File 'lib/peddler/lwa.rb', line 17

def client_secret
  @client_secret
end

#options ⇒ Hash[Symbol, untyped] (readonly)

Returns the value of attribute options.

Returns:

  • (Hash[Symbol, untyped])


17
18
19
# File 'lib/peddler/lwa.rb', line 17

def options
  @options
end

Class Method Details

.request ⇒ Response

Parameters:

  • client_id: (String, nil)
  • client_secret: (String, nil)
  • options (Object)

Returns:



20
21
22
# File 'lib/peddler/lwa.rb', line 20

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

Instance Method Details

#grant_type ⇒ String?

Returns:

  • (String, nil)


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/peddler/lwa.rb', line 36

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

#params ⇒ Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


50
51
52
53
54
55
56
# File 'lib/peddler/lwa.rb', line 50

def params
  {
    grant_type: grant_type,
    client_id: client_id,
    client_secret: client_secret,
  }.compact.merge(options)
end

#request ⇒ Response

Returns:



31
32
33
34
# File 'lib/peddler/lwa.rb', line 31

def request
  http_response = HTTP.post(URL, form: params)
  Response.wrap(http_response, parser: -> { LWAToken })
end