Class: Redd::OAuth2Access
- Inherits:
-
Object
- Object
- Redd::OAuth2Access
- Defined in:
- lib/redd/oauth2_access.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
Instance Method Summary collapse
- #expired? ⇒ Boolean
-
#initialize(response) ⇒ OAuth2Access
constructor
A new instance of OAuth2Access.
- #refresh(response) ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(response) ⇒ OAuth2Access
Returns a new instance of OAuth2Access.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/redd/oauth2_access.rb', line 15 def initialize(response) @access_token = response[:access_token] @refresh_token = response[:refresh_token] @scope = response[:scope].split(",").map { |s| s.to_sym } @duration = @refresh_token ? :permanent : :temporary @expires_at = if response[:expires_at] Time.at(response[:expires_at]) else Time.now + response[:expires_in] end end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
5 6 7 |
# File 'lib/redd/oauth2_access.rb', line 5 def access_token @access_token end |
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
11 12 13 |
# File 'lib/redd/oauth2_access.rb', line 11 def duration @duration end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
13 14 15 |
# File 'lib/redd/oauth2_access.rb', line 13 def expires_at @expires_at end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
7 8 9 |
# File 'lib/redd/oauth2_access.rb', line 7 def refresh_token @refresh_token end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
9 10 11 |
# File 'lib/redd/oauth2_access.rb', line 9 def scope @scope end |
Class Method Details
.from_json(json) ⇒ Object
47 48 49 50 |
# File 'lib/redd/oauth2_access.rb', line 47 def self.from_json(json) hash = MultiJson.load(json, symbolize_keys: true) new(hash) end |
Instance Method Details
#expired? ⇒ Boolean
34 35 36 |
# File 'lib/redd/oauth2_access.rb', line 34 def expired? Time.now > @expires_at end |
#refresh(response) ⇒ Object
28 29 30 31 32 |
# File 'lib/redd/oauth2_access.rb', line 28 def refresh(response) @access_token = response[:access_token] @expires_at = Time.now + response[:expires_in] self end |
#to_json ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/redd/oauth2_access.rb', line 38 def to_json MultiJson.dump( access_token: @access_token, refresh_token: @refresh_token, scope: @scope.join(","), expires_at: @expires_at.to_i ) end |