Class: Sorcery::Providers::Twitter

Inherits:
Base
  • Object
show all
Includes:
Sorcery::Protocols::Oauth
Defined in:
lib/sorcery/providers/twitter.rb

Overview

This class adds support for OAuth with Twitter.com.

config.twitter.key = <key>
config.twitter.secret = <secret>
...

Instance Attribute Summary collapse

Attributes inherited from Base

#access_token, #callback_url, #key, #original_callback_url, #secret, #site, #user_info_mapping

Instance Method Summary collapse

Methods included from Sorcery::Protocols::Oauth

#authorize_url, #get_access_token, #get_request_token, #oauth_version

Methods inherited from Base

descendants, #has_callback?, name

Constructor Details

#initializeTwitter

Returns a new instance of Twitter.



15
16
17
18
19
20
# File 'lib/sorcery/providers/twitter.rb', line 15

def initialize
  super

  @site           = 'https://api.twitter.com'
  @user_info_path = '/1.1/account/verify_credentials.json'
end

Instance Attribute Details

#stateObject

Returns the value of attribute state.



13
14
15
# File 'lib/sorcery/providers/twitter.rb', line 13

def state
  @state
end

#user_info_pathObject

Returns the value of attribute user_info_path.



13
14
15
# File 'lib/sorcery/providers/twitter.rb', line 13

def 
  @user_info_path
end

Instance Method Details

#get_consumerObject

Override included get_consumer method to provide authorize_path



23
24
25
# File 'lib/sorcery/providers/twitter.rb', line 23

def get_consumer
  ::OAuth::Consumer.new(@key, secret, site: site, authorize_path: '/oauth/authenticate')
end

#get_user_hash(access_token) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/sorcery/providers/twitter.rb', line 27

def get_user_hash(access_token)
  response = access_token.get()

  {}.tap do |h|
    h[:user_info] = JSON.parse(response.body)
    h[:uid] = h[:user_info]['id'].to_s
  end
end

#login_url(params, session) ⇒ Object

calculates and returns the url to which the user should be redirected, to get authenticated at the external provider’s site.



38
39
40
41
42
43
# File 'lib/sorcery/providers/twitter.rb', line 38

def (params, session)
  req_token = self.get_request_token
  session[:request_token]         = req_token.token
  session[:request_token_secret]  = req_token.secret
  self.authorize_url({ request_token: req_token.token, request_token_secret: req_token.secret })
end

#process_callback(params, session) ⇒ Object

tries to login the user from access token



46
47
48
49
50
51
52
53
54
55
# File 'lib/sorcery/providers/twitter.rb', line 46

def process_callback(params, session)
  args = {
    oauth_verifier:       params[:oauth_verifier],
    request_token:        session[:request_token],
    request_token_secret: session[:request_token_secret]
  }

  args.merge!({ code: params[:code] }) if params[:code]
  get_access_token(args)
end