-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTimeline.rb
48 lines (39 loc) · 1015 Bytes
/
getTimeline.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
46
47
48
require 'rubygems'
require 'oauth'
require 'json'
# get timeline
baseurl = "https://api.twitter.com"
path = "/1.1/statuses/user_timeline.json"
query = URI.encode_www_form(
"screen_name" => "twitterapi",
"count" => 10,
)
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.request_uri
# method to print the timeline
def print_timeline(tweets)
# ADD CODE TO ITERATE THROUGH EACH TWEET AND PRINT ITS TEXT
tweets.each do |tweet| puts tweet['text'] end
end
# HTTP Setup
http = Net::HTTP.new address.host, address.port
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
#use your own keys
consumer_key = OAuth::Consumer.new(
"XYZ",
"-")
access_token = OAuth::Token.new(
"XYZ",
"ABC")
# request
request.oauth! http, consumer_key, access_token
http.start
response = http.request request
# check 200 response code
tweets = nil
if response.code == '200' then
tweets = JSON.parse(response.body)
print_timeline(tweets)
end
nil