Class: Matroid::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/matroid/connection.rb

Overview

Represents an OAuth access token

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Token

Returns a new instance of Token.



110
111
112
113
114
115
# File 'lib/matroid/connection.rb', line 110

def initialize(options = {})
  @token_type = options['token_type']
  @access_token = options['access_token']
  @born = DateTime.now
  @lifetime = options['expires_in']
end

Instance Attribute Details

#acces_tokenObject (readonly)

Returns the value of attribute acces_token.



109
110
111
# File 'lib/matroid/connection.rb', line 109

def acces_token
  @acces_token
end

#bornDateTime

When the token was created

Returns:

  • (DateTime)

    the current value of born



108
109
110
# File 'lib/matroid/connection.rb', line 108

def born
  @born
end

#lifetimeString

Seconds until token expired

Returns:

  • (String)

    the current value of lifetime



108
109
110
# File 'lib/matroid/connection.rb', line 108

def lifetime
  @lifetime
end

#token_strString

The actual access token

Returns:

  • (String)

    the current value of token_str



108
109
110
# File 'lib/matroid/connection.rb', line 108

def token_str
  @token_str
end

#token_typeString

ex: “Bearer”

Returns:

  • (String)

    the current value of token_type



108
109
110
# File 'lib/matroid/connection.rb', line 108

def token_type
  @token_type
end

Instance Method Details

#authorization_headerObject



117
118
119
# File 'lib/matroid/connection.rb', line 117

def authorization_header
  "#{@token_type} #{@access_token}"
end

#expired?Boolean

Checks if the current token is expired

Returns:

  • (Boolean)


123
124
125
126
# File 'lib/matroid/connection.rb', line 123

def expired?
  lifetime_in_days = time_in_seconds(@lifetime)
  @born + lifetime_in_days < DateTime.now
end

#time_in_seconds(t) ⇒ Object



136
137
138
# File 'lib/matroid/connection.rb', line 136

def time_in_seconds(t)
  t * 24.0 * 60 * 60
end

#time_remainingNumeric

Returns Time left before token expires (in seconds).

Returns:

  • (Numeric)

    Time left before token expires (in seconds).



130
131
132
133
134
# File 'lib/matroid/connection.rb', line 130

def time_remaining
  lifetime_in_days = time_in_seconds(@lifetime)
  remaining = lifetime_in_days - (DateTime.now - @born)
  remaining > 0 ? time_in_seconds(remaining) : 0
end

#to_sObject



140
141
142
143
144
145
146
# File 'lib/matroid/connection.rb', line 140

def to_s
  JSON.pretty_generate({
    access_token: @access_token,
    born: @born,
    lifetime: @lifetime
  })
end