Class: TwitterConnection
- Inherits:
-
Object
- Object
- TwitterConnection
- Defined in:
- lib/twitter_kotoba/twitter_connection.rb
Instance Attribute Summary collapse
-
#rate_limit_exceeded ⇒ Object
readonly
Returns the value of attribute rate_limit_exceeded.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #get_latest_1000 ⇒ Object
-
#initialize(user) ⇒ TwitterConnection
constructor
A new instance of TwitterConnection.
- #process_requests(requests) ⇒ Object
- #run_requests ⇒ Object
Constructor Details
#initialize(user) ⇒ TwitterConnection
Returns a new instance of TwitterConnection.
9 10 11 12 |
# File 'lib/twitter_kotoba/twitter_connection.rb', line 9 def initialize(user) @user = user @rate_limit_exceeded = false end |
Instance Attribute Details
#rate_limit_exceeded ⇒ Object (readonly)
Returns the value of attribute rate_limit_exceeded.
7 8 9 |
# File 'lib/twitter_kotoba/twitter_connection.rb', line 7 def rate_limit_exceeded @rate_limit_exceeded end |
#user ⇒ Object
Returns the value of attribute user.
6 7 8 |
# File 'lib/twitter_kotoba/twitter_connection.rb', line 6 def user @user end |
Instance Method Details
#get_latest_1000 ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/twitter_kotoba/twitter_connection.rb', line 14 def get_latest_1000 requests = run_requests tweets = process_requests(requests) # if any of the requests return anything but 200 try again return tweets.flatten end |
#process_requests(requests) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/twitter_kotoba/twitter_connection.rb', line 43 def process_requests(requests) # array to hold all the tweets tweets = [] requests.each do |req| unless req.response.code == 400 begin resp = Oj.load(req.response.body) unless req.response.code != 200 rescue # occasionaly the response causes Oj.load problems # this needs to be looked at further end unless !resp || resp.empty? tweets << resp end else @rate_limit_exceeded = true end end return tweets end |
#run_requests ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/twitter_kotoba/twitter_connection.rb', line 24 def run_requests # You can only request 200 tweets per page # so we need to get 5 pages for a total of 1000 tweets. requests = [] # The typhoeus hydra will run all its queued requests in parallel. # Way faster than using Patron or HTTParty and running 5 requests in series twitter_hydra = Typhoeus::Hydra.new (0..4).each do |req| requests[req] = Typhoeus::Request.new("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=#{@user}&count=200&page=#{req+1}") requests[req].timeout = 10000 # 10 seconds max twitter_hydra.queue requests[req] end twitter_hydra.run return requests end |