class Matroid::Token

Represents an OAuth access token @attr [String] token_type ex: “Bearer” @attr [String] token_str The actual access token @attr [DateTime] born When the token was created @attr [String] lifetime Seconds until token expired

Attributes

acces_token[R]
born[R]
lifetime[R]

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

authorization_header() click to toggle source
# File lib/matroid/connection.rb, line 117
def authorization_header
  "#{@token_type} #{@access_token}"
end
expired?() click to toggle source

Checks if the current token is expired @return [Boolean]

# 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) click to toggle source
# File lib/matroid/connection.rb, line 136
def time_in_seconds(t)
  t * 24.0 * 60 * 60
end
time_remaining() click to toggle source

@return [Numeric] Time left before token expires (in seconds).

# 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_s() click to toggle source
# File lib/matroid/connection.rb, line 140
def to_s
  JSON.pretty_generate({
    access_token: @access_token,
    born: @born,
    lifetime: @lifetime
  })
end