class Redd::Access

A container for the client's access to their account via OAuth2

Attributes

access_token[R]

@!attribute [r] access_token @return [String] The access token used to access the users account.

expires_at[R]

@!attribute [r] expires_at @return [Time] The time when the access token expires.

refresh_token[R]

@!attribute [r] refresh_token @return [String, nil] The refresh token, if the access was permanent.

scope[R]

@!attribute [r] scope @return [Array] The scope that the client is allowed to access.

Public Class Methods

from_json(json) click to toggle source

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
new(body) click to toggle source

@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

expired?() click to toggle source

@return [Boolean] Whether the access has expired.

# File lib/redd/access.rb, line 46
def expired?
  Time.now > (@expires_at + 60)
end
permanent?() click to toggle source

@return [Boolean] Whether the access is permanent.

# File lib/redd/access.rb, line 41
def permanent?
  !temporary?
end
refreshed!(body) click to toggle source

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
temporary?() click to toggle source

@return [Boolean] Whether the access is temporary.

# File lib/redd/access.rb, line 36
def temporary?
  !refresh_token
end
to_json() click to toggle source

@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