Class: Twitter::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter/oauth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctoken, csecret, options = {}) ⇒ OAuth

Options

:sign_in => true to just sign in with twitter instead of doing oauth authorization
            (http://apiwiki.twitter.com/Sign-in-with-Twitter)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/twitter/oauth.rb', line 26

def initialize(ctoken, csecret, options={})
  @ctoken, @csecret, @consumer_options = ctoken, csecret, {}
  @api_endpoint = options[:api_endpoint] || 'http://api.twitter.com'
  @signing_endpoint = options[:signing_endpoint] || 'http://api.twitter.com'

  if options[:api_version]
    @api_version = "/#{options[:api_version].to_s}"
  else
    @api_version = ''
  end

  if options[:sign_in]
    @consumer_options[:authorize_path] =  '/oauth/authenticate'
  end
end

Instance Attribute Details

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint.



21
22
23
# File 'lib/twitter/oauth.rb', line 21

def api_endpoint
  @api_endpoint
end

#api_versionObject (readonly)

Returns the value of attribute api_version.



21
22
23
# File 'lib/twitter/oauth.rb', line 21

def api_version
  @api_version
end

#consumer_optionsObject (readonly)

Returns the value of attribute consumer_options.



21
22
23
# File 'lib/twitter/oauth.rb', line 21

def consumer_options
  @consumer_options
end

#csecretObject (readonly)

Returns the value of attribute csecret.



21
22
23
# File 'lib/twitter/oauth.rb', line 21

def csecret
  @csecret
end

#ctokenObject (readonly)

Returns the value of attribute ctoken.



21
22
23
# File 'lib/twitter/oauth.rb', line 21

def ctoken
  @ctoken
end

#signing_endpointObject (readonly)

Returns the value of attribute signing_endpoint.



21
22
23
# File 'lib/twitter/oauth.rb', line 21

def signing_endpoint
  @signing_endpoint
end

Instance Method Details

#access_tokenObject



70
71
72
# File 'lib/twitter/oauth.rb', line 70

def access_token
  @access_token ||= ::OAuth::AccessToken.new(signing_consumer, @atoken, @asecret)
end

#authorize_from_access(atoken, asecret) ⇒ Object



74
75
76
# File 'lib/twitter/oauth.rb', line 74

def authorize_from_access(atoken, asecret)
  @atoken, @asecret = atoken, asecret
end

#authorize_from_request(rtoken, rsecret, verifier_or_pin) ⇒ Object

For web apps use params, for desktop apps, use the verifier is the pin that twitter gives users.



64
65
66
67
68
# File 'lib/twitter/oauth.rb', line 64

def authorize_from_request(rtoken, rsecret, verifier_or_pin)
  request_token = ::OAuth::RequestToken.new(signing_consumer, rtoken, rsecret)
  access_token = request_token.get_access_token(:oauth_verifier => verifier_or_pin)
  @atoken, @asecret = access_token.token, access_token.secret
end

#consumerObject



42
43
44
# File 'lib/twitter/oauth.rb', line 42

def consumer
  @consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, {:site => api_endpoint}.merge(consumer_options))
end

#delete(path, headers = nil) ⇒ Object



4
5
6
# File 'lib/twitter/oauth.rb', line 4

def delete(path, headers=nil)
  access_token.delete(*[api_version + path, headers].compact)
end

#get(path, headers = nil) ⇒ Object



8
9
10
# File 'lib/twitter/oauth.rb', line 8

def get(path, headers=nil)
  access_token.get(*[api_version + path, headers].compact)
end

#post(path, data = nil, headers = nil) ⇒ Object



12
13
14
# File 'lib/twitter/oauth.rb', line 12

def post(path, data = nil, headers=nil)
  access_token.post(*[api_version + path, data, headers].compact)
end

#put(path, data = nil, headers = nil) ⇒ Object



16
17
18
# File 'lib/twitter/oauth.rb', line 16

def put(path, data = nil, headers=nil)
  access_token.put(*[api_version + path, data, headers].compact)
end

#request_token(options = {}) ⇒ Object

Note: If using oauth with a web app, be sure to provide :oauth_callback. Options:

:oauth_callback => String, url that twitter should redirect to


58
59
60
# File 'lib/twitter/oauth.rb', line 58

def request_token(options={})
  @request_token ||= signing_consumer.get_request_token(options)
end

#set_callback_url(url) ⇒ Object



50
51
52
53
# File 'lib/twitter/oauth.rb', line 50

def set_callback_url(url)
  clear_request_token
  request_token(:oauth_callback => url)
end

#signing_consumerObject



46
47
48
# File 'lib/twitter/oauth.rb', line 46

def signing_consumer
  @signing_consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, {:site => signing_endpoint, :request_endpoint => api_endpoint }.merge(consumer_options))
end