Class: Matroid::Token
- Inherits:
-
Object
- Object
- Matroid::Token
- Defined in:
- lib/matroid/connection.rb
Overview
Represents an OAuth access token
Instance Attribute Summary collapse
-
#acces_token ⇒ Object
readonly
Returns the value of attribute acces_token.
-
#born ⇒ DateTime
When the token was created.
-
#lifetime ⇒ String
Seconds until token expired.
-
#token_str ⇒ String
The actual access token.
-
#token_type ⇒ String
ex: “Bearer”.
Instance Method Summary collapse
- #authorization_header ⇒ Object
-
#expired? ⇒ Boolean
Checks if the current token is expired.
-
#initialize(options = {}) ⇒ Token
constructor
A new instance of Token.
- #time_in_seconds(t) ⇒ Object
-
#time_remaining ⇒ Numeric
Time left before token expires (in seconds).
- #to_s ⇒ Object
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( = {}) @token_type = ['token_type'] @access_token = ['access_token'] @born = DateTime.now @lifetime = ['expires_in'] end |
Instance Attribute Details
#acces_token ⇒ Object (readonly)
Returns the value of attribute acces_token.
109 110 111 |
# File 'lib/matroid/connection.rb', line 109 def acces_token @acces_token end |
#born ⇒ DateTime
When the token was created
108 109 110 |
# File 'lib/matroid/connection.rb', line 108 def born @born end |
#lifetime ⇒ String
Seconds until token expired
108 109 110 |
# File 'lib/matroid/connection.rb', line 108 def lifetime @lifetime end |
#token_str ⇒ String
The actual access token
108 109 110 |
# File 'lib/matroid/connection.rb', line 108 def token_str @token_str end |
#token_type ⇒ String
ex: “Bearer”
108 109 110 |
# File 'lib/matroid/connection.rb', line 108 def token_type @token_type end |
Instance Method Details
#authorization_header ⇒ Object
117 118 119 |
# File 'lib/matroid/connection.rb', line 117 def "#{@token_type} #{@access_token}" end |
#expired? ⇒ Boolean
Checks if the current token is expired
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_remaining ⇒ Numeric
Returns 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_s ⇒ Object
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 |