Class: Lago::Api::RateLimitInfo
- Inherits:
-
Object
- Object
- Lago::Api::RateLimitInfo
- Defined in:
- lib/lago/api/rate_limit_info.rb
Overview
Parsed rate limit headers from a Lago API response.
Delivered to the on_rate_limit_info callback after every successful request so callers can build observability around the rate limit (warn at thresholds, emit metrics, etc.).
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#remaining ⇒ Object
readonly
Returns the value of attribute remaining.
-
#reset ⇒ Object
readonly
Returns the value of attribute reset.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
-
.parse(response, method:, url:) ⇒ Object
Parses x-ratelimit-* headers from a Net::HTTPResponse-like object.
Instance Method Summary collapse
-
#initialize(limit:, remaining:, reset:, method:, url:) ⇒ RateLimitInfo
constructor
A new instance of RateLimitInfo.
-
#usage_pct ⇒ Object
Returns the fraction of the rate limit currently used as a Float in [0.0, 1.0], or
nilwhen the headers aren’t usable (missing limit, zero limit, missing remaining).
Constructor Details
#initialize(limit:, remaining:, reset:, method:, url:) ⇒ RateLimitInfo
Returns a new instance of RateLimitInfo.
31 32 33 34 35 36 37 |
# File 'lib/lago/api/rate_limit_info.rb', line 31 def initialize(limit:, remaining:, reset:, method:, url:) @limit = limit @remaining = remaining @reset = reset @method = method @url = url end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
11 12 13 |
# File 'lib/lago/api/rate_limit_info.rb', line 11 def limit @limit end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
11 12 13 |
# File 'lib/lago/api/rate_limit_info.rb', line 11 def method @method end |
#remaining ⇒ Object (readonly)
Returns the value of attribute remaining.
11 12 13 |
# File 'lib/lago/api/rate_limit_info.rb', line 11 def remaining @remaining end |
#reset ⇒ Object (readonly)
Returns the value of attribute reset.
11 12 13 |
# File 'lib/lago/api/rate_limit_info.rb', line 11 def reset @reset end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
11 12 13 |
# File 'lib/lago/api/rate_limit_info.rb', line 11 def url @url end |
Class Method Details
.parse(response, method:, url:) ⇒ Object
Parses x-ratelimit-* headers from a Net::HTTPResponse-like object. Returns nil when no rate limit headers are present.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lago/api/rate_limit_info.rb', line 15 def self.parse(response, method:, url:) limit = response['x-ratelimit-limit'] remaining = response['x-ratelimit-remaining'] reset = response['x-ratelimit-reset'] return nil if limit.nil? && remaining.nil? && reset.nil? new( limit: limit&.to_i, remaining: remaining&.to_i, reset: reset&.to_i, method:, url:, ) end |
Instance Method Details
#usage_pct ⇒ Object
Returns the fraction of the rate limit currently used as a Float in [0.0, 1.0], or nil when the headers aren’t usable (missing limit, zero limit, missing remaining).
42 43 44 45 46 |
# File 'lib/lago/api/rate_limit_info.rb', line 42 def usage_pct return nil if limit.nil? || remaining.nil? || limit.to_i <= 0 1.0 - (remaining.to_f / limit) end |