Advertisement
Guest User

k

a guest
May 11th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. require 'rubygems'
  2. require 'oauth'
  3. require 'json'
  4.  
  5. # Now you will fetch /1.1/statuses/user_timeline.json,
  6. # returns a list of public Tweets from the specified
  7. # account.
  8. baseurl = "https://api.twitter.com"
  9. path = "/1.1/statuses/user_timeline.json"
  10. query = URI.encode_www_form(
  11. "screen_name" => "twitterapi",
  12. "count" => 10,
  13. )
  14. address = URI("#{baseurl}#{path}?#{query}")
  15. request = Net::HTTP::Get.new address.request_uri
  16.  
  17. # Print data about a list of Tweets
  18. def print_timeline(tweets)
  19. # ADD CODE TO ITERATE THROUGH EACH TWEET AND PRINT ITS TEXT
  20. tweets.each do |tweet|
  21. puts tweet["text"]
  22. end
  23.  
  24. # Set up HTTP.
  25. http = Net::HTTP.new address.host, address.port
  26. http.use_ssl = true
  27. http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  28.  
  29. # If you entered your credentials in the first
  30. # exercise, no need to enter them again here. The
  31. # ||= operator will only assign these values if
  32. # they are not already set.
  33. consumer_key ||= OAuth::Consumer.new "ENTER IN EXERCISE 1", ""
  34. access_token ||= OAuth::Token.new "ENTER IN EXERCISE 1", ""
  35.  
  36. # Issue the request.
  37. request.oauth! http, consumer_key, access_token
  38. http.start
  39. response = http.request request
  40.  
  41. # Parse and print the Tweet if the response code was 200
  42. tweets = nil
  43. if response.code == '200' then
  44. tweets = JSON.parse(response.body)
  45. print_timeline(tweets)
  46. end
  47. nil
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement