Class: TwitterApp

Inherits:
Twitter::Base
  • Object
show all
Defined in:
lib/twitter_app.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ TwitterApp

Returns a new instance of TwitterApp.



47
48
49
50
51
# File 'lib/twitter_app.rb', line 47

def initialize(config)
  oauth = Twitter::OAuth.new(config.ctoken, config.csecret)
  oauth.authorize_from_access(config.atoken, config.asecret)
  super(oauth)
end

Class Method Details

.authorize(config, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/twitter_app.rb', line 20

def self.authorize(config, &block)
  consumer = OAuth::Consumer.new(config.ctoken, config.csecret, { :site=>config.csite })
  request_token = consumer.get_request_token

  counter = 0
  loop do
    # ask the user to open the url in its browser and allow access
    begin
      oauth_verifier = yield request_token.authorize_url, counter
    rescue Exception => ex
      return false # something went wrong or the callback wants to exit
    end
    counter += 1

    # now that the request_token is authorized we can get an access token
    begin
      access_token = request_token.get_access_token(:oauth_verifier => oauth_verifier)
      config.atoken = access_token.token
      config.asecret = access_token.secret
      config.store
      return true # user authorized and it works
    rescue OAuth::Unauthorized => ex
      # ignore and try again
    end
  end
end

Instance Method Details

#last_status(user_id) ⇒ Object



53
54
55
56
# File 'lib/twitter_app.rb', line 53

def last_status(user_id)
  statuses = self.user_timeline({:id => user_id, :count => 1})
  statuses.first
end

#reply(status, message) ⇒ Object



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

def reply(status, message)
  self.update("@#{status.user.screen_name} #{message}", {:in_reply_to_status_id => status.id})
end