Class: Flagit::TwitterOAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/flagit/twitter_oauth.rb

Instance Method Summary collapse

Constructor Details

#initializeTwitterOAuth

Returns a new instance of TwitterOAuth.



6
7
8
9
10
11
12
13
# File 'lib/flagit/twitter_oauth.rb', line 6

def initialize
  @consumer_key = Flagit.configuration.consumer_key
  @consumer_secret = Flagit.configuration.consumer_secret
  @token = Flagit.configuration.access_token
  @secret = Flagit.configuration.access_token_secret
  @api_version = '1.1'
  @api_host = 'api.twitter.com'
end

Instance Method Details

#authentication_request_token(options = {}) ⇒ Object



45
46
47
48
# File 'lib/flagit/twitter_oauth.rb', line 45

def authentication_request_token(options={})
  consumer.options[:authorize_path] = '/oauth/authenticate'
  request_token(options)
end

#authorize(token, secret, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/flagit/twitter_oauth.rb', line 30

def authorize(token, secret, options = {})
  request_token = OAuth::RequestToken.new(
    consumer, token, secret
  )
  @access_token = request_token.get_access_token(options)
  @token = @access_token.token
  @secret = @access_token.secret
  Flagit.configure do |config|
    config.access_token = @token
    config.access_token_secret = @secret
  end
  Flagit.save!
  @access_token
end

#authorized?Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/flagit/twitter_oauth.rb', line 54

def authorized?
  oauth_response = access_token.get("/#{@api_version}/account/verify_credentials.json")
  return oauth_response.class == Net::HTTPOK
end

#request_token(options = {}) ⇒ Object



50
51
52
# File 'lib/flagit/twitter_oauth.rb', line 50

def request_token(options={})
  consumer.get_request_token(options)
end

#run_auth_processObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flagit/twitter_oauth.rb', line 15

def run_auth_process
  get_consumer_info if (@consumer_key.nil? || @consumer_secret.nil?)
  return true if authorized?
  request_token = authentication_request_token(oauth_callback: 'oob')
  open_using_browser request_token.authorize_url
  puts 'Enter the supplied PIN:'
  pin = STDIN.gets.chomp
  access_token = authorize(
    request_token.token,
    request_token.secret,
    oauth_verifier: pin
  )
  puts "#{authorized? ? 'Authorization successful.' : 'Authorization failed.'}"
end