Class: X::RateLimit
- Inherits:
-
Object
- Object
- X::RateLimit
- Defined in:
- lib/x/rate_limit.rb
Constant Summary collapse
- RATE_LIMIT_TYPE =
"rate-limit".freeze
- APP_LIMIT_TYPE =
"app-limit-24hour".freeze
- USER_LIMIT_TYPE =
"user-limit-24hour".freeze
- TYPES =
[RATE_LIMIT_TYPE, APP_LIMIT_TYPE, USER_LIMIT_TYPE].freeze
Instance Attribute Summary collapse
-
#response ⇒ Object
Returns the value of attribute response.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(type:, response:) ⇒ RateLimit
constructor
A new instance of RateLimit.
- #limit ⇒ Object
- #remaining ⇒ Object
- #reset_at ⇒ Object
- #reset_in ⇒ Object (also: #retry_after)
Constructor Details
#initialize(type:, response:) ⇒ RateLimit
Returns a new instance of RateLimit.
10 11 12 13 |
# File 'lib/x/rate_limit.rb', line 10 def initialize(type:, response:) @type = type @response = response end |
Instance Attribute Details
#response ⇒ Object
Returns the value of attribute response.
8 9 10 |
# File 'lib/x/rate_limit.rb', line 8 def response @response end |
#type ⇒ Object
Returns the value of attribute type.
8 9 10 |
# File 'lib/x/rate_limit.rb', line 8 def type @type end |
Instance Method Details
#limit ⇒ Object
15 16 17 |
# File 'lib/x/rate_limit.rb', line 15 def limit Integer(response.fetch("x-#{type}-limit")) end |
#remaining ⇒ Object
19 20 21 |
# File 'lib/x/rate_limit.rb', line 19 def remaining Integer(response.fetch("x-#{type}-remaining")) end |
#reset_at ⇒ Object
23 24 25 |
# File 'lib/x/rate_limit.rb', line 23 def reset_at Time.at(Integer(response.fetch("x-#{type}-reset"))) end |
#reset_in ⇒ Object Also known as: retry_after
27 28 29 |
# File 'lib/x/rate_limit.rb', line 27 def reset_in [(reset_at - Time.now).ceil, 0].max end |