-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example to fetch all tweets for a user [ci skip]
- Loading branch information
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# All Tweets | ||
|
||
You can fetch up to 3,200 tweets for a user, 200 at a time (up to 16 HTTP | ||
requests). **Note: This may result in [rate limiting][].** | ||
|
||
[rate limiting]: https://github.com/sferik/twitter/blob/master/examples/RateLimiting.md | ||
|
||
Here is an example of recursively fetching pages of 200 Tweets until you | ||
receive an empty response. | ||
|
||
```ruby | ||
require 'twitter' | ||
|
||
def collect_with_max_id(collection=[], max_id=nil, &block) | ||
response = yield max_id | ||
collection += response | ||
response.empty? ? collection.flatten : collect_with_max_id(collection, response.last.id - 1, &block) | ||
end | ||
|
||
def fetch_all_tweets(user) | ||
collect_with_max_id do |max_id| | ||
options = {:count => 200, :include_rts => true} | ||
options[:max_id] = max_id unless max_id.nil? | ||
@client.user_timeline(user, options) | ||
end | ||
end | ||
|
||
fetch_all_tweets("sferik") | ||
``` |