class Redd::Access
A container for the client's access to their account via OAuth2
Attributes
@!attribute [r] access_token
@return [String] The access token used to access the users account.
@!attribute [r] expires_at
@return [Time] The time when the access token expires.
@!attribute [r] refresh_token
@return [String, nil] The refresh token, if the access was permanent.
@!attribute [r] scope @return [Array] The scope that the client is allowed to access.
Public Class Methods
Create a new instance of the class from the JSON returned from to_json
@param [String] json @return [Access]
# File lib/redd/access.rb, line 71 def self.from_json(json) hash = MultiJson.load(json, symbolize_keys: true) new(hash) end
@param [String] body The response body containing the required info.
# 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
Public Instance Methods
@return [Boolean] Whether the access has expired.
# File lib/redd/access.rb, line 46 def expired? Time.now > (@expires_at + 60) end
@return [Boolean] Whether the access is permanent.
# File lib/redd/access.rb, line 41 def permanent? !temporary? end
Refresh the object with the new response body. This happens when a new access token is requested using a request token. @param [Hash] body The new response body.
# File lib/redd/access.rb, line 53 def refreshed!(body) @access_token = body[:access_token] @expires_at = Time.now + body[:expires_in] end
@return [Boolean] Whether the access is temporary.
# File lib/redd/access.rb, line 36 def temporary? !refresh_token end
@return [String] A JSON version of the data that can be loaded later.
# 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