module Oauth::Models::Consumers::Token::ClassMethods

Public Instance Methods

build_user_from_token() click to toggle source
# File lib/oauth/models/consumers/token.rb, line 84
def build_user_from_token
end
consumer() click to toggle source
# File lib/oauth/models/consumers/token.rb, line 24
def consumer
  options = credentials[:options] || {}
  @consumer||=OAuth::Consumer.new credentials[:key],credentials[:secret],options
end
credentials() click to toggle source
# File lib/oauth/models/consumers/token.rb, line 87
def credentials
  @credentials||=OAUTH_CREDENTIALS[service_name]
end
find_or_create_from_access_token(user, access_token, new_token = nil) click to toggle source

Finds, creates or updates a ConsumerToken by finding the token or taking it when it’s given. It then updates the attributes and saves the changes/new record to a datastore. @param user [User] The user to which the access token should belong to @param access_token [AccessToken || Oauth2Token] Either a request token taken from the service or a ConsumerToken @param new_token [AccessToken] A new access token, used for refreshing the access token with OAuth 2.

Usage example: find_or_create_from_access_token(current_user, access_token) <– Find or create a new access token find_or_create_from_access-token(current_user, Oauth2Token.last, client.refresh!) <– Edits existing record with new refreshed information

# File lib/oauth/models/consumers/token.rb, line 50
def find_or_create_from_access_token(user, access_token, new_token = nil)
  if access_token.class.ancestors.include?(Oauth2Token)
    token = access_token
  else
    if user
      token = self.find_or_initialize_by_user_id_and_token(user.id, access_token.token)
    else
      token = self.find_or_initialize_by_token(access_token.token)
    end
  end

  token = if new_token then set_details(new_token, access_token) else set_details(access_token, token) end

  token.save! if token.new_record? or token.changed?

  token
end
find_or_create_from_request_token(user,token,secret,oauth_verifier) click to toggle source
# File lib/oauth/models/consumers/token.rb, line 33
def find_or_create_from_request_token(user,token,secret,oauth_verifier)
  request_token=OAuth::RequestToken.new consumer,token,secret
  options={}
  options[:oauth_verifier]=oauth_verifier if oauth_verifier
  access_token=request_token.get_access_token options
  find_or_create_from_access_token user, access_token
end
get_request_token(callback_url) click to toggle source
# File lib/oauth/models/consumers/token.rb, line 29
def get_request_token(callback_url)
  consumer.get_request_token(:oauth_callback=>callback_url)
end
service_name() click to toggle source
# File lib/oauth/models/consumers/token.rb, line 20
def service_name
  @service_name||=self.to_s.underscore.scan(/^(.*?)(_token)?$/)[0][0].to_sym
end
set_details(access_token, token) click to toggle source

Set the details such as the secret, refresh token and expiration time to an instance of ConsumerToken @return [ConsumerToken] A ConsumerToken

# File lib/oauth/models/consumers/token.rb, line 70
def set_details(access_token, token)
  secret = access_token.respond_to?(:secret) ? access_token.secret : nil
  refresh_token = access_token.respond_to?(:refresh_token) ? access_token.refresh_token : nil
  expires_in, expires_at = token.expiration_date(access_token) if token.class.ancestors.include?(Oauth2Token)

  token.token = access_token.token
  token.refresh_token = refresh_token
  token.secret = secret
  token.expires_at = expires_at
  token.expires_in = expires_in

  token
end