Class: Medidata::API::Response::Ratelimit
- Inherits:
-
Object
- Object
- Medidata::API::Response::Ratelimit
- Defined in:
- lib/medidata/api/http.rb
Overview
Provide useful functionality around API rate limiting.
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#remaining ⇒ Object
readonly
Returns the value of attribute remaining.
-
#reset ⇒ Object
readonly
Returns the value of attribute reset.
Instance Method Summary collapse
- #exceeded? ⇒ Boolean
-
#initialize(limit, remaining, reset) ⇒ Ratelimit
constructor
-
Args : -
limit-> The total number of requests allowed within a rate limit window -remaining-> The number of requests that have been processed within this current rate limit window -reset-> The time (in seconds since Unix Epoch) when the rate limit will reset.
-
-
#used ⇒ Object
-
Returns : - The number of requests that have been used out of this rate limit window.
-
-
#wait! ⇒ Object
Sleep until the reset time arrives.
Constructor Details
#initialize(limit, remaining, reset) ⇒ Ratelimit
-
Args :
-
limit-> The total number of requests allowed within a rate limit window -
remaining-> The number of requests that have been processed within this current rate limit window -
reset-> The time (in seconds since Unix Epoch) when the rate limit will reset
-
17 18 19 20 21 |
# File 'lib/medidata/api/http.rb', line 17 def initialize(limit, remaining, reset) @limit = limit.to_i @remaining = remaining.to_i @reset = Time.at reset.to_i end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
11 12 13 |
# File 'lib/medidata/api/http.rb', line 11 def limit @limit end |
#remaining ⇒ Object (readonly)
Returns the value of attribute remaining.
11 12 13 |
# File 'lib/medidata/api/http.rb', line 11 def remaining @remaining end |
#reset ⇒ Object (readonly)
Returns the value of attribute reset.
11 12 13 |
# File 'lib/medidata/api/http.rb', line 11 def reset @reset end |
Instance Method Details
#exceeded? ⇒ Boolean
23 24 25 |
# File 'lib/medidata/api/http.rb', line 23 def exceeded? remaining <= 0 end |
#used ⇒ Object
-
Returns :
-
The number of requests that have been used out of this rate limit window
-
30 31 32 |
# File 'lib/medidata/api/http.rb', line 30 def used limit - remaining end |
#wait! ⇒ Object
Sleep until the reset time arrives. If given a block, it will be called after sleeping is finished.
-
Returns :
-
The amount of time (in seconds) that the rate limit slept for.
-
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/medidata/api/http.rb', line 40 def wait! now = Time.now.utc.to_i duration = (reset.to_i - now) + 1 sleep duration if duration >= 0 yield if block_given? duration end |