Class: Ruqqus::Token

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.

Raises:



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_tokenString (readonly)



# File 'lib/ruqqus/token.rb', line 11


#expiresTime (readonly)



# File 'lib/ruqqus/token.rb', line 19


#refresh_tokenString (readonly)



# File 'lib/ruqqus/token.rb', line 15


#scopesArray<Symbol> (readonly)



# File 'lib/ruqqus/token.rb', line 27


#typeString (readonly)



# 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



86
87
88
# File 'lib/ruqqus/token.rb', line 86

def expired?
  expires <= Time.now
end

#need_refresh?Boolean



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.

Raises:



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

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.

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



98
99
100
# File 'lib/ruqqus/token.rb', line 98

def to_json(*_unused_)
  @data.to_json
end