Class: Ruqqus::Token
- Inherits:
-
Object
- Object
- Ruqqus::Token
- Defined in:
- lib/ruqqus/token.rb
Overview
Represents a Ruqqus OAuth2 access token.
Constant Summary collapse
- REFRESH_THRESHOLD =
The minimum number of seconds that can remain before the token refreshes itself.
60
Instance Attribute Summary collapse
-
#access_token ⇒ String
readonly
The access token value.
-
#expires ⇒ Time
readonly
The time the token expires and will require a refresh.
-
#refresh_token ⇒ String
readonly
The refresh token value.
-
#scopes ⇒ Array<Symbol>
readonly
An array of scopes this token authorizes.
-
#type ⇒ String
readonly
The token type to specify in the HTTP header.
Class Method Summary collapse
-
.from_json(payload) ⇒ Object
Loads the object from a JSON-formatted string.
-
.load_json(filename) ⇒ Token
Loads a token in JSON format from a file.
Instance Method Summary collapse
-
#expired? ⇒ Boolean
true
if token is expired, otherwisefalse
. -
#initialize(client_id, client_secret, code, persist = true) ⇒ Token
constructor
Grants access to a user account and returns an a newly created Token to use as authentication for it.
-
#need_refresh? ⇒ Boolean
true
if remaining lifetime is within the REFRESH_THRESHOLD, otherwisefalse
. -
#refresh(client_id, client_secret) ⇒ void
Refreshes the access token and resets its time of expiration.
-
#save_json(filename) ⇒ Integer
Saves this token in JSON format to the specified file.
-
#to_json(*_unused_) ⇒ String
The object as a JSON-formatted string.
Constructor Details
#initialize(client_id, client_secret, code, persist = true) ⇒ Token
Grants access to a user account and returns an a newly created Ruqqus::Token to use as authentication for it.
40 41 42 43 44 45 46 47 |
# File 'lib/ruqqus/token.rb', line 40 def initialize(client_id, client_secret, code, persist = true) headers = { 'User-Agent': Client::USER_AGENT, 'Accept': 'application/json', 'Content-Type': 'application/json' } params = { code: code, client_id: client_id, client_secret: client_secret, grant_type: 'code', permanent: persist } resp = RestClient.post('https://ruqqus.com/oauth/grant', params, headers ) @data = JSON.parse(resp.body, symbolize_names: true) raise(Ruqqus::Error, 'failed to grant access for token') if @data[:oauth_error] end |
Instance Attribute Details
#access_token ⇒ String (readonly)
Returns the access token value.
|
# File 'lib/ruqqus/token.rb', line 11
|
#expires ⇒ Time (readonly)
Returns the time the token expires and will require a refresh.
|
# File 'lib/ruqqus/token.rb', line 19
|
#refresh_token ⇒ String (readonly)
Returns the refresh token value.
|
# File 'lib/ruqqus/token.rb', line 15
|
#scopes ⇒ Array<Symbol> (readonly)
Returns an array of scopes this token authorizes.
|
# File 'lib/ruqqus/token.rb', line 27
|
#type ⇒ String (readonly)
Returns the token type to specify in the HTTP header.
|
# File 'lib/ruqqus/token.rb', line 23
|
Class Method Details
.from_json(payload) ⇒ Object
Loads the object from a JSON-formatted string.
128 129 130 131 132 133 |
# File 'lib/ruqqus/token.rb', line 128 def self.from_json(payload) data = payload.is_a?(Hash) ? payload: JSON.parse(payload, symbolize_names: true) token = allocate token.instance_variable_set(:@data, data) token end |
.load_json(filename) ⇒ Token
Loads a token in JSON format from a file.
118 119 120 |
# File 'lib/ruqqus/token.rb', line 118 def self.load_json(filename) from_json(File.read(filename)) end |
Instance Method Details
#expired? ⇒ Boolean
Returns true
if token is expired, otherwise false
.
86 87 88 |
# File 'lib/ruqqus/token.rb', line 86 def expired? expires <= Time.now end |
#need_refresh? ⇒ Boolean
Returns true
if remaining lifetime is within the REFRESH_THRESHOLD, otherwise false
.
92 93 94 |
# File 'lib/ruqqus/token.rb', line 92 def need_refresh? (expires - Time.now) < REFRESH_THRESHOLD end |
#refresh(client_id, client_secret) ⇒ void
This method returns an undefined value.
Refreshes the access token and resets its time of expiration.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ruqqus/token.rb', line 73 def refresh(client_id, client_secret) headers = { 'User-Agent': Client::USER_AGENT, Authorization: "Bearer #{access_token}" } params = { client_id: client_id, client_secret: client_secret, refresh_token: refresh_token, grant_type: 'refresh' } resp = RestClient.post('https://ruqqus.com/oauth/grant', params, headers ) data = JSON.parse(resp.body, symbolize_names: true) raise(Ruqqus::Error, 'failed to refresh authentication token') unless resp.code == 200 || data[:oauth_error] @data.merge!(data) sleep(1) # TODO: Test. Get internment 401 error when token needs refreshed end |
#save_json(filename) ⇒ Integer
Security Alert: The token is essentially the equivalent to login credentials in regards to security, so it is important to not share or store it somewhere that it can be easily compromised.
Saves this token in JSON format to the specified file.
109 110 111 |
# File 'lib/ruqqus/token.rb', line 109 def save_json(filename) File.open(filename, 'wb') { |io| io.write(to_json) } end |
#to_json(*_unused_) ⇒ String
Returns the object as a JSON-formatted string.
98 99 100 |
# File 'lib/ruqqus/token.rb', line 98 def to_json(*_unused_) @data.to_json end |