forked from samsymons/RedditKit.rb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply rate limiting PR from avidw (samsymons#24)
- Loading branch information
Marty Dill
authored and
Marty Dill
committed
Oct 16, 2014
1 parent
91787b5
commit ea99451
Showing
3 changed files
with
27 additions
and
2 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
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,19 @@ | ||
module RedditKit | ||
|
||
# The class to rate-limit requests to reddit. | ||
class RateLimit | ||
# Time the last request was made. | ||
attr_reader :last_request | ||
|
||
def initialize | ||
@last_request = Time.at(0) | ||
end | ||
|
||
# Sleep until a given number of seconds have passed since the last request | ||
def wait(gap = 2) | ||
wait_time = @last_request + gap - Time.now | ||
sleep(wait_time) if wait_time > 0 | ||
@last_request = Time.now | ||
end | ||
end | ||
end |