Class: Redd::Access
- Inherits:
-
Object
- Object
- Redd::Access
- Defined in:
- lib/redd/access.rb
Overview
A container for the client’s access to their account via OAuth2
Instance Attribute Summary collapse
-
#access_token ⇒ String
readonly
The access token used to access the users account.
-
#expires_at ⇒ Time
readonly
The time when the access token expires.
-
#refresh_token ⇒ String?
readonly
The refresh token, if the access was permanent.
-
#scope ⇒ Array
readonly
The scope that the client is allowed to access.
Class Method Summary collapse
-
.from_json(json) ⇒ Access
Create a new instance of the class from the JSON returned from #to_json.
Instance Method Summary collapse
-
#expired? ⇒ Boolean
Whether the access has expired.
-
#initialize(body) ⇒ Access
constructor
A new instance of Access.
-
#permanent? ⇒ Boolean
Whether the access is permanent.
-
#refreshed!(body) ⇒ Object
Refresh the object with the new response body.
-
#temporary? ⇒ Boolean
Whether the access is temporary.
-
#to_json ⇒ String
A JSON version of the data that can be loaded later.
Constructor Details
#initialize(body) ⇒ Access
Returns a new instance of Access.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/redd/access.rb', line 23 def initialize(body) @access_token = body[:access_token] @refresh_token = body[:refresh_token] @scope = (body[:scope] ? body[:scope].split(',') : []) @expires_at = if body[:expires_at] Time.at(body[:expires_at]) else Time.now + (body[:expires_in] || 0) end end |
Instance Attribute Details
#access_token ⇒ String (readonly)
Returns The access token used to access the users account.
8 9 10 |
# File 'lib/redd/access.rb', line 8 def access_token @access_token end |
#expires_at ⇒ Time (readonly)
Returns The time when the access token expires.
20 21 22 |
# File 'lib/redd/access.rb', line 20 def expires_at @expires_at end |
#refresh_token ⇒ String? (readonly)
Returns The refresh token, if the access was permanent.
12 13 14 |
# File 'lib/redd/access.rb', line 12 def refresh_token @refresh_token end |
#scope ⇒ Array (readonly)
Returns The scope that the client is allowed to access.
16 17 18 |
# File 'lib/redd/access.rb', line 16 def scope @scope end |
Class Method Details
.from_json(json) ⇒ Access
Create a new instance of the class from the JSON returned from #to_json
71 72 73 74 |
# File 'lib/redd/access.rb', line 71 def self.from_json(json) hash = MultiJson.load(json, symbolize_keys: true) new(hash) end |
Instance Method Details
#expired? ⇒ Boolean
Returns Whether the access has expired.
46 47 48 |
# File 'lib/redd/access.rb', line 46 def expired? Time.now > (@expires_at + 60) end |
#permanent? ⇒ Boolean
Returns Whether the access is permanent.
41 42 43 |
# File 'lib/redd/access.rb', line 41 def permanent? !temporary? end |
#refreshed!(body) ⇒ Object
Refresh the object with the new response body. This happens when a new access token is requested using a request token.
53 54 55 56 |
# File 'lib/redd/access.rb', line 53 def refreshed!(body) @access_token = body[:access_token] @expires_at = Time.now + body[:expires_in] end |
#temporary? ⇒ Boolean
Returns Whether the access is temporary.
36 37 38 |
# File 'lib/redd/access.rb', line 36 def temporary? !refresh_token end |
#to_json ⇒ String
Returns A JSON version of the data that can be loaded later.
59 60 61 62 63 64 65 66 |
# File 'lib/redd/access.rb', line 59 def to_json MultiJson.dump( access_token: @access_token, refresh_token: @refresh_token, scope: @scope.join(','), expires_at: @expires_at.to_i ) end |