Class: ADM::AccessToken
- Inherits:
-
Object
- Object
- ADM::AccessToken
- Defined in:
- lib/adm/access_token.rb
Defined Under Namespace
Classes: TokenGetError
Instance Attribute Summary collapse
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Class Method Summary collapse
- .fetch(client_id = nil, client_secret = nil) ⇒ Object
- .get_token ⇒ Object
- .request_data(client_id, client_secret) ⇒ Object
Instance Method Summary collapse
-
#initialize(opts) ⇒ AccessToken
constructor
A new instance of AccessToken.
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(opts) ⇒ AccessToken
Returns a new instance of AccessToken.
5 6 7 8 9 10 |
# File 'lib/adm/access_token.rb', line 5 def initialize opts @token = opts['access_token'] @expires_at = Time.now+opts['expires_in'].to_i @type = opts['token_type'] @scope = opts['scope'] end |
Instance Attribute Details
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
2 3 4 |
# File 'lib/adm/access_token.rb', line 2 def client_id @client_id end |
#client_secret ⇒ Object (readonly)
Returns the value of attribute client_secret.
2 3 4 |
# File 'lib/adm/access_token.rb', line 2 def client_secret @client_secret end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
3 4 5 |
# File 'lib/adm/access_token.rb', line 3 def token @token end |
Class Method Details
.fetch(client_id = nil, client_secret = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/adm/access_token.rb', line 20 def self.fetch client_id=nil, client_secret=nil client_id ||= ADM.client_id client_secret ||= ADM.client_secret data = request_data client_id, client_secret if data['error'] raise TokenGetError, response.body end new(data) end |
.get_token ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/adm/access_token.rb', line 51 def self.get_token if @token && @token.valid? return @token.token end @token = fetch @token.token end |
.request_data(client_id, client_secret) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/adm/access_token.rb', line 32 def self.request_data client_id, client_secret request = Typhoeus::Request.new( 'https://api.amazon.com/auth/O2/token', method: :post, body: { grant_type: :'client_credentials', scope: :'messaging:push', client_id: client_id, client_secret: client_secret }, headers: { :Accept => :'application/json' } ) response = request.run MultiJson.load(response.body) end |
Instance Method Details
#to_s ⇒ Object
12 13 14 |
# File 'lib/adm/access_token.rb', line 12 def to_s token end |
#valid? ⇒ Boolean
16 17 18 |
# File 'lib/adm/access_token.rb', line 16 def valid? @expires_at < Time.now end |