Class: MxitApi::AuthToken

Inherits:
Object
  • Object
show all
Defined in:
lib/mxit_api/auth_token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token_response) ⇒ AuthToken

Returns a new instance of AuthToken.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/mxit_api/auth_token.rb', line 6

def initialize(token_response)
  @access_token = token_response['access_token']
  @type = token_response['type']
  @expires_in = token_response['expires_in']
  @refresh_token = token_response['refresh_token']
  @scope = token_response['scope'].split

  @expires_at = Time.now + expires_in.seconds
  # If there isn't a refresh token `has_refresh_token_expired?` must always return true.
  @refresh_token_expires_at = @refresh_token ? Time.now + 24.hours : Time.now
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



3
4
5
# File 'lib/mxit_api/auth_token.rb', line 3

def access_token
  @access_token
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



3
4
5
# File 'lib/mxit_api/auth_token.rb', line 3

def expires_at
  @expires_at
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



3
4
5
# File 'lib/mxit_api/auth_token.rb', line 3

def expires_in
  @expires_in
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



3
4
5
# File 'lib/mxit_api/auth_token.rb', line 3

def refresh_token
  @refresh_token
end

#refresh_token_expires_atObject (readonly)

Returns the value of attribute refresh_token_expires_at.



3
4
5
# File 'lib/mxit_api/auth_token.rb', line 3

def refresh_token_expires_at
  @refresh_token_expires_at
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/mxit_api/auth_token.rb', line 3

def type
  @type
end

Instance Method Details

#has_expired?Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/mxit_api/auth_token.rb', line 22

def has_expired?
  # For extreme latency check within 3 seconds.
  @expires_at - Time.now <= 3.0
end

#has_refresh_token_expired?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/mxit_api/auth_token.rb', line 27

def has_refresh_token_expired?
  @refresh_token_expires_at - Time.now <= 3.0
end

#has_scopes?(scopes) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/mxit_api/auth_token.rb', line 31

def has_scopes?(scopes)
  (scopes - @scope).empty?
end

#scopeObject



18
19
20
# File 'lib/mxit_api/auth_token.rb', line 18

def scope
  @scope.join(' ')
end