Class: Tosuto::OauthToken

Inherits:
Resource show all
Defined in:
lib/tosuto/oauth_token.rb

Constant Summary collapse

ENDPOINT =
"/authentication/v1/authentication/login".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

attr_collections, attr_objects, encode_form_data, #parse_collection, #parse_object, #set_attribute, #set_attributes, #underscore

Constructor Details

#initialize(attributes = {}) ⇒ OauthToken

Returns a new instance of OauthToken.



19
20
21
22
23
24
25
26
27
# File 'lib/tosuto/oauth_token.rb', line 19

def initialize(attributes = {})
  return unless (token = attributes['token'])

  self.access_token = token['accessToken']
  self.expires_in = token['expiresIn'].to_i
  self.expires_at = Time.now + expires_in
  self.token_type = token['tokenType']
  self.scope = token['scope']
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



5
6
7
# File 'lib/tosuto/oauth_token.rb', line 5

def access_token
  @access_token
end

#expires_atObject

Returns the value of attribute expires_at.



5
6
7
# File 'lib/tosuto/oauth_token.rb', line 5

def expires_at
  @expires_at
end

#expires_inObject

Returns the value of attribute expires_in.



5
6
7
# File 'lib/tosuto/oauth_token.rb', line 5

def expires_in
  @expires_in
end

#scopeObject

Returns the value of attribute scope.



5
6
7
# File 'lib/tosuto/oauth_token.rb', line 5

def scope
  @scope
end

#token_typeObject

Returns the value of attribute token_type.



5
6
7
# File 'lib/tosuto/oauth_token.rb', line 5

def token_type
  @token_type
end

Class Method Details

.create(client_id, client_secret, user_access_type: "TOAST_MACHINE_CLIENT", api: API.new) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/tosuto/oauth_token.rb', line 7

def self.create(client_id, client_secret, user_access_type: "TOAST_MACHINE_CLIENT", api: API.new)
  body = {
    userAccessType: user_access_type,
    clientId: client_id,
    clientSecret: client_secret,
  }
  response = api.request(:post, ENDPOINT, body: body)
  raise response.error unless response.success?

  return new(response.json_body)
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/tosuto/oauth_token.rb', line 29

def expired?
  expires_at < Time.now
end

#inspectObject



33
34
35
# File 'lib/tosuto/oauth_token.rb', line 33

def inspect
  "#<#{self.class} #{access_token&.inspect}>"
end