class Ruqqus::Token

Represents a Ruqqus [OAuth2](oauth.net/2/) access token.

Constants

REFRESH_THRESHOLD

The minimum number of seconds that can remain before the token refreshes itself.

Public Class Methods

from_json(payload) click to toggle source

Loads the object from a JSON-formatted string.

@param json [String,Hash] a JSON string representing the object, or the parsed Hash of the JSON (symbol keys).

@return [Object] the loaded object.

# 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) click to toggle source

Loads a token in JSON format from a file.

@param filename [String] the path to a file where the token is written to. @return [Token] a newly created {Token} instance.

# File lib/ruqqus/token.rb, line 118
def self.load_json(filename)
  from_json(File.read(filename))
end
new(client_id, client_secret, code, persist = true) click to toggle source

Grants access to a user account and returns an a newly created {Token} to use as authentication for it.

@param client_id [String] the ID of client application. @param client_secret [String] the secret of the client application. @param code [String] the code received in the redirect response when the user requested API access. @param persist [Boolean] `true` if token will be reusable, otherwise `false`.

@return [Token] a newly created {Token} object.

# 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

Public Instance Methods

access_token() click to toggle source
# File lib/ruqqus/token.rb, line 49
def access_token
  @data[:access_token]
end
expired?() click to toggle source

@return [Boolean] `true` if token is expired, otherwise `false`.

# File lib/ruqqus/token.rb, line 86
def expired?
  expires <= Time.now
end
expires() click to toggle source
# File lib/ruqqus/token.rb, line 61
def expires
  Time.at(@data[:expires_at])
end
need_refresh?() click to toggle source

@return [Boolean] `true` if remaining lifetime is within the {REFRESH_THRESHOLD}, otherwise `false`.

# File lib/ruqqus/token.rb, line 92
def need_refresh?
  (expires - Time.now) < REFRESH_THRESHOLD
end
refresh(client_id, client_secret) click to toggle source

Refreshes the access token and resets its time of expiration.

@return [void]

# 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
refresh_token() click to toggle source
# File lib/ruqqus/token.rb, line 53
def refresh_token
  @data[:refresh_token]
end
save_json(filename) click to toggle source

Saves this token in JSON format to the specified file.

@param filename [String] the path to a file where the token will be written to. @return [Integer] the number of bytes written. @note **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.
# File lib/ruqqus/token.rb, line 109
def save_json(filename)
  File.open(filename, 'wb') { |io| io.write(to_json) }
end
scopes() click to toggle source
# File lib/ruqqus/token.rb, line 65
def scopes
  @data[:scopes].split(',').map(&:to_sym)
end
to_json(*_unused_) click to toggle source

@return [String] the object as a JSON-formatted string.

# File lib/ruqqus/token.rb, line 98
def to_json(*_unused_)
  @data.to_json
end
type() click to toggle source
# File lib/ruqqus/token.rb, line 57
def type
  @data[:token_type]
end