option :count ignored in Twitter::REST::Search#search

51 views
Skip to first unread message

Carlos A. Henríquez Q.

unread,
Jan 15, 2015, 8:30:01 AM1/15/15
to twitter-...@googlegroups.com
Hi all. I'm trying to do a standard search but the param :count seems to get ignore and the search does not stop.

This is my code:

client = Twitter::REST::Client.new(CONFIG) # with proper key and secret
SEARCH_OPTIONS
= {:result_type => 'recent', :count => 100}
count
= 0
client
.search("#venezuela", SEARCH_OPTIONS).each { |t| count+=1 } # this does not stop unless I press Ctrl+C
count
> 100 # after Ctrl+C


This search, on the other hand, works as expected

count = 0
client
.search("#venezuela", SEARCH_OPTIONS).take(SEARCH_OPTIONS[:count]).each { |t| count+=1 }
count
== 100

Thanks.

Carlos A. Henríquez Q.

unread,
Jan 15, 2015, 12:21:43 PM1/15/15
to twitter-...@googlegroups.com
Problem solved.

I misunderstood the concept of :count. It does not relate to the total number of tweets you'll get in a search, but only to the number of tweets per page in the internal REST call.

Considering that SearchResults#each iterates over all tweets found (paginating internally every :count if there are too many and only stoping after going through all possible pages), I found the :count parameter of little value.

Because the Gem always goes through all elements (even if more pages are needed) we can only ask for a specific amount using #take afterwards. So if you only want 10 tweets, you need something like search(q, :count=10).take(10).

Moreover, #take uses #each internally, so if you do something like 

search(q, :count=n).take(n).each { ... }

you end up iterating twice over the collection.

What about an option :max_count to indicate the total number of tweets you really want, so #each iterates up to :max_count elements?
Reply all
Reply to author
Forward
0 new messages