Parent

OAuth2::AccessToken

Attributes

client[R]
expires_at[R]
expires_in[R]
options[RW]
params[R]
refresh_token[RW]
token[R]

Public Class Methods

from_hash(client, hash) click to toggle source

Initializes an AccessToken from a Hash

@param [Client] the OAuth2::Client instance @param [Hash] a hash of AccessToken property values @return [AccessToken] the initalized AccessToken

# File lib/oauth2/access_token.rb, line 12
def from_hash(client, hash)
  self.new(client, hash.delete('access_token') || hash.delete(:access_token), hash)
end
from_kvform(client, kvform) click to toggle source

Initializes an AccessToken from a key/value application/x-www-form-urlencoded string

@param [Client] client the OAuth2::Client instance @param [String] kvform the application/x-www-form-urlencoded string @return [AccessToken] the initalized AccessToken

# File lib/oauth2/access_token.rb, line 21
def from_kvform(client, kvform)
  from_hash(client, Rack::Utils.parse_query(kvform))
end
new(client, token, opts={}) click to toggle source

Initalize an AccessToken

@param [Client] client the OAuth2::Client instance @param [String] token the Access Token value @param [Hash] opts the options to create the Access Token with @option opts [String] :refresh_token (nil) the refresh_token value @option opts [FixNum, String] :expires_in (nil) the number of seconds in which the AccessToken will expire @option opts [FixNum, String] :expires_at (nil) the epoch time in seconds in which AccessToken will expire @option opts [Symbol] :mode (:header) the transmission mode of the Access Token parameter value

one of :header, :body or :query

@option opts [String] :header_format ('Bearer %s') the string format to use for the Authorization header @option opts [String] :param_name ('access_token') the parameter name to use for transmission of the

Access Token value in :body or :query transmission mode
# File lib/oauth2/access_token.rb, line 39
def initialize(client, token, opts={})
  @client = client
  @token = token.to_s
  [:refresh_token, :expires_in, :expires_at].each do |arg|
    instance_variable_set("@#{arg}", opts.delete(arg) || opts.delete(arg.to_s))
  end
  @expires_in ||= opts.delete('expires')
  @expires_in &&= @expires_in.to_i
  @expires_at &&= @expires_at.to_i
  @expires_at ||= Time.now.to_i + @expires_in if @expires_in
  @options = {:mode          => opts.delete(:mode) || :header,
              :header_format => opts.delete(:header_format) || 'Bearer %s',
              :param_name    => opts.delete(:param_name) || 'access_token'}
  @params = opts
end

Public Instance Methods

[](key) click to toggle source

Indexer to additional params present in token response

@param [String] key entry key to Hash

# File lib/oauth2/access_token.rb, line 58
def [](key)
  @params[key]
end
delete(path, opts={}, &block) click to toggle source

Make a DELETE request with the Access Token

@see AccessToken#request

# File lib/oauth2/access_token.rb, line 134
def delete(path, opts={}, &block)
  request(:delete, path, opts, &block)
end
expired?() click to toggle source

Whether or not the token is expired

@return [Boolean]

# File lib/oauth2/access_token.rb, line 72
def expired?
  expires? && (expires_at < Time.now.to_i)
end
expires?() click to toggle source

Whether or not the token expires

@return [Boolean]

# File lib/oauth2/access_token.rb, line 65
def expires?
  !!@expires_at
end
get(path, opts={}, &block) click to toggle source

Make a GET request with the Access Token

@see AccessToken#request

# File lib/oauth2/access_token.rb, line 106
def get(path, opts={}, &block)
  request(:get, path, opts, &block)
end
headers() click to toggle source

Get the headers hash (includes Authorization token)

# File lib/oauth2/access_token.rb, line 139
def headers
  { 'Authorization' => options[:header_format] % token }
end
patch(path, opts={}, &block) click to toggle source

Make a PATCH request with the Access Token

@see AccessToken#request

# File lib/oauth2/access_token.rb, line 127
def patch(path, opts={}, &block)
  request(:patch, path, opts, &block)
end
post(path, opts={}, &block) click to toggle source

Make a POST request with the Access Token

@see AccessToken#request

# File lib/oauth2/access_token.rb, line 113
def post(path, opts={}, &block)
  request(:post, path, opts, &block)
end
put(path, opts={}, &block) click to toggle source

Make a PUT request with the Access Token

@see AccessToken#request

# File lib/oauth2/access_token.rb, line 120
def put(path, opts={}, &block)
  request(:put, path, opts, &block)
end
refresh!(params={}) click to toggle source

Refreshes the current Access Token

@return [AccessToken] a new AccessToken @note options should be carried over to the new AccessToken

# File lib/oauth2/access_token.rb, line 80
def refresh!(params={})
  raise "A refresh_token is not available" unless refresh_token
  params.merge!(:client_id      => @client.id,
                :client_secret  => @client.secret,
                :grant_type     => 'refresh_token',
                :refresh_token  => refresh_token)
  new_token = @client.get_token(params)
  new_token.options = options
  new_token.refresh_token = refresh_token unless new_token.refresh_token
  new_token
end
request(verb, path, opts={}, &block) click to toggle source

Make a request with the Access Token

@param [Symbol] verb the HTTP request method @param [String] path the HTTP URL path of the request @param [Hash] opts the options to make the request with @see Client#request

# File lib/oauth2/access_token.rb, line 98
def request(verb, path, opts={}, &block)
  set_token(opts)
  @client.request(verb, path, opts, &block)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.