Class: X::RateLimit
- Inherits:
-
Object
- Object
- X::RateLimit
- Defined in:
- lib/x/rate_limit.rb
Overview
Represents rate limit information from an API response
Constant Summary collapse
- RATE_LIMIT_TYPE =
Rate limit type identifier
"rate-limit".freeze
- APP_LIMIT_TYPE =
App limit type identifier
"app-limit-24hour".freeze
- USER_LIMIT_TYPE =
User limit type identifier
"user-limit-24hour".freeze
- TYPES =
All supported rate limit types
[RATE_LIMIT_TYPE, APP_LIMIT_TYPE, USER_LIMIT_TYPE].freeze
Instance Attribute Summary collapse
-
#response ⇒ Net::HTTPResponse
The HTTP response containing rate limit headers.
-
#type ⇒ String
The type of rate limit.
Instance Method Summary collapse
-
#initialize(type:, response:) ⇒ RateLimit
constructor
Initialize a new RateLimit.
-
#limit ⇒ Integer
Get the rate limit maximum.
-
#remaining ⇒ Integer
Get the remaining requests.
-
#reset_at ⇒ Time
Get the time when the rate limit resets.
-
#reset_in ⇒ Integer
(also: #retry_after)
Get the seconds until the rate limit resets.
Constructor Details
#initialize(type:, response:) ⇒ RateLimit
Initialize a new RateLimit
36 37 38 39 |
# File 'lib/x/rate_limit.rb', line 36 def initialize(type:, response:) @type = type @response = response end |
Instance Attribute Details
#response ⇒ Net::HTTPResponse
The HTTP response containing rate limit headers
26 27 28 |
# File 'lib/x/rate_limit.rb', line 26 def response @response end |
#type ⇒ String
The type of rate limit
19 20 21 |
# File 'lib/x/rate_limit.rb', line 19 def type @type end |
Instance Method Details
#limit ⇒ Integer
Get the rate limit maximum
47 48 49 |
# File 'lib/x/rate_limit.rb', line 47 def limit Integer(response.fetch("x-#{type}-limit")) end |
#remaining ⇒ Integer
Get the remaining requests
57 58 59 |
# File 'lib/x/rate_limit.rb', line 57 def remaining Integer(response.fetch("x-#{type}-remaining")) end |
#reset_at ⇒ Time
Get the time when the rate limit resets
67 68 69 |
# File 'lib/x/rate_limit.rb', line 67 def reset_at Time.at(Integer(response.fetch("x-#{type}-reset"))) end |
#reset_in ⇒ Integer Also known as: retry_after
Get the seconds until the rate limit resets
77 78 79 |
# File 'lib/x/rate_limit.rb', line 77 def reset_in [(reset_at - Time.now).ceil, 0].max end |