Module: OAuth::CLI::Twitter

Extended by:
Twitter
Included in:
Twitter
Defined in:
lib/oauth/cli/twitter.rb

Constant Summary collapse

CONSUMER_TOKEN =
'UWhf189fDdtCZz6rq8Q5gA'
CONSUMER_SECRET =
'QQHee9yFJqWeztLvGRj5A552gCGIJ7N0yLtpsJPZeU'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#extenderObject

Returns the value of attribute extender.



24
25
26
# File 'lib/oauth/cli/twitter.rb', line 24

def extender
  @extender
end

#includerObject

Returns the value of attribute includer.



24
25
26
# File 'lib/oauth/cli/twitter.rb', line 24

def includer
  @includer
end

Class Method Details

.extended(extender) ⇒ Object



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

def self.extended(extender)
  @extender = extender
end

.included(includer) ⇒ Object



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

def self.included(includer)
  @includer = includer
end

Instance Method Details

#authorizeObject



94
95
96
97
98
99
100
101
102
# File 'lib/oauth/cli/twitter.rb', line 94

def authorize
  if @options[:browser]
    Kernel.send(:open_browser, request_token.authorize_url)
  else
    STDOUT.puts 'Visit URL below to allow this application.'
    STDOUT.puts request_token.authorize_url
  end
  request_token.get_access_token(:oauth_verifier => prompt_pin)
end

#consumerObject



108
109
110
111
112
113
114
115
# File 'lib/oauth/cli/twitter.rb', line 108

def consumer
  @consumer ||= OAuth::Consumer.new(
    @consumer_token,
    @consumer_secret,
    :site => 'http://api.twitter.com',
    :proxy => ENV['http_proxy']
  )
end

#executeObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/oauth/cli/twitter.rb', line 56

def execute
  config = load_config
  if config && config['oauth_token'] && config['oauth_token_secret']
    @access_token = OAuth::AccessToken.new(
      consumer,
      config['oauth_token'],
      config['oauth_token_secret']
    )
    @access_token.params = config
  else
    @access_token = authorize
    save_config(@access_token)
  end
  @access_token
end

#get_access_token(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/oauth/cli/twitter.rb', line 26

def get_access_token(*args)
  case args.size
  when 1
    @options = args[0]
    if Twitter.extender &&
       Twitter.extender.const_defined?(:CONSUMER_TOKEN) &&
       Twitter.extender.const_defined?(:CONSUMER_SECRET)
      @consumer_token  = Twitter.extender::CONSUMER_TOKEN
      @consumer_secret = Twitter.extender::CONSUMER_SECRET
    elsif Twitter.includer &&
       Twitter.includer.const_defined?(:CONSUMER_TOKEN) &&
       Twitter.includer.const_defined?(:CONSUMER_SECRET)
      @consumer_token  = Twitter.includer::CONSUMER_TOKEN
      @consumer_secret = Twitter.includer::CONSUMER_SECRET
    elsif Object.const_defined?(:CONSUMER_TOKEN) &&
          Object.const_defined?(:CONSUMER_SECRET)
      @consumer_token  = Object::CONSUMER_TOKEN
      @consumer_secret = Object::CONSUMER_SECRET
    else
      @consumer_token  = CONSUMER_TOKEN
      @consumer_secret = CONSUMER_SECRET
    end
  when 2, 3
    @consumer_token, @consumer_secret, @options = args
  else
    raise ArgumentError
  end
  execute
end

#load_configObject



72
73
74
75
76
77
78
# File 'lib/oauth/cli/twitter.rb', line 72

def load_config
  if @options[:pit]
    Pit.get(@options[:pit])
  elsif @options[:file] && File.exist?(@options[:file])
    YAML.load(File.read(@options[:file]))
  end
end

#prompt_pinObject



117
118
119
120
121
# File 'lib/oauth/cli/twitter.rb', line 117

def prompt_pin
  while pin = Readline.readline('Enter pin > ')
    break pin if pin =~ /^\d+$/
  end
end

#request_tokenObject



104
105
106
# File 'lib/oauth/cli/twitter.rb', line 104

def request_token
  @request_token ||= consumer.get_request_token
end

#save_config(access_token) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/oauth/cli/twitter.rb', line 80

def save_config(access_token)
  config = {}
  access_token.params.each do |k, v|
    config[k] = v if k.is_a?(String)
  end
  if @options[:pit]
    Pit.set(@options[:pit], :data => config)
  elsif @options[:file]
    File.open(@options[:file], 'w') do |f|
      f.write(config.to_yaml)
    end
  end
end