Skip to content

Commit

Permalink
Add example to fetch all tweets for a user [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Dec 1, 2013
1 parent b082ed0 commit 97ad35d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions .yardopts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG.md
CONTRIBUTING.md
LICENSE.md
README.md
examples/AllTweets.md
examples/Configuration.md
examples/RateLimiting.md
examples/Update.md
29 changes: 29 additions & 0 deletions examples/AllTweets.md
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")
```

0 comments on commit 97ad35d

Please sign in to comment.