Class: Retjilp::Twitter

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

Constant Summary collapse

TWITTER_URI =
"https://api.twitter.com"
API_VERSION =
"1.1"
ACCESS_TOKEN_FILENAME =
File.join(File.expand_path("~/.retjilp"), "access_token")

Instance Method Summary collapse

Constructor Details

#initialize(consumer_key, consumer_secret) ⇒ Twitter

Returns a new instance of Twitter.


9
10
11
12
# File 'lib/retjilp/twitter.rb', line 9

def initialize(consumer_key, consumer_secret)
	@consumer_key = consumer_key
	@consumer_secret = consumer_secret
end

Instance Method Details

#home_timeline(options) ⇒ Object


25
26
27
28
# File 'lib/retjilp/twitter.rb', line 25

def home_timeline(options)
	since_id = options[:since_id] ? "&since_id=#{options[:since_id]}" : ""
	get "/statuses/home_timeline.json?trim_user=true" + since_id
end

#list_statuses(list, options) ⇒ Object


19
20
21
22
23
# File 'lib/retjilp/twitter.rb', line 19

def list_statuses(list, options)
	Retjilp::log.info("Fetching list tweets of #{list}")
	since_id = options[:since_id] ? "&since_id=#{options[:since_id]}" : ""
	get "/lists/statuses.json?slug=#{list}&owner_screen_name=#{@user_info['screen_name']}&include_rts=true" + since_id
end

#loginObject


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/retjilp/twitter.rb', line 35

def 
	# Request the token if the cached access token does not exist
	@access_token, @user_info = cached_access_token
	unless @access_token
		STDIN.tty? or raise "This script must be run interactively the first time to be able to authenticate."
		Retjilp::log.info("Requesting new access token")
		consumer = OAuth::Consumer.new(
			@consumer_key,
			@consumer_secret,
			:site => TWITTER_URI,
			:scheme => :header,
			:request_token_path => "/oauth/request_token",
			:authorize_path => "/oauth/authorize",
			:@access_token_path => "/oauth/@access_token",
			:http_method => :post)
		request_token = consumer.get_request_token(:oauth_callback => "oob")

		puts "Please open #{request_token.authorize_url} in your browser, authorize Retjilp, and enter the PIN code below:"
		verifier = STDIN.gets.chomp 

		begin
			@access_token = request_token.get_access_token(:oauth_verifier => verifier)
		rescue OAuth::Unauthorized
			raise "Invalid PIN verification!"
		end
		@user_info = verify_token(@access_token) or raise "Access token not authorized!"
		self.cached_access_token = @access_token
	end
	Retjilp::log.info("Logged in as #{@user_info["screen_name"]}")
end

#retweet(id) ⇒ Object


30
31
32
33
# File 'lib/retjilp/twitter.rb', line 30

def retweet(id)
	result = @access_token.post("/#{API_VERSION}/statuses/retweet/#{id}.json")
	result.class == Net::HTTPOK or Retjilp::log.error("Error retweeting #{result.body}")
end

#user_timelineObject


14
15
16
17
# File 'lib/retjilp/twitter.rb', line 14

def user_timeline
	Retjilp::log.info("Fetching own tweets")
	get "/statuses/user_timeline.json?trim_user=true&include_rts=true"
end