-
-
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 rate limiting examples [ci skip]
- Loading branch information
Showing
2 changed files
with
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ CONTRIBUTING.md | |
LICENSE.md | ||
README.md | ||
examples/Configuration.md | ||
examples/RateLimiting.md | ||
examples/Update.md |
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,23 @@ | ||
# Rate Limits | ||
|
||
Here's an example of how to handle rate limits: | ||
|
||
```ruby | ||
require 'twitter' | ||
|
||
MAX_ATTEMPTS = 3 | ||
num_attempts = 0 | ||
begin | ||
num_attempts += 1 | ||
retweets = client.retweeted_by_user("sferik") | ||
rescue Twitter::Error::TooManyRequests => error | ||
if num_attempts <= MAX_ATTEMPTS | ||
# NOTE: Your process could go to sleep for up to 15 minutes but if you | ||
# retry any sooner, it will almost certainly fail with the same exception. | ||
sleep error.rate_limit.reset_in | ||
retry | ||
else | ||
raise | ||
end | ||
end | ||
``` |