Class: SecApi::RateLimitState
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- SecApi::RateLimitState
- Defined in:
- lib/sec_api/rate_limit_state.rb
Overview
Immutable value object representing rate limit state from sec-api.io response headers.
This class uses Dry::Struct for type safety and immutability, ensuring thread-safe access to rate limit information. The state is extracted from HTTP response headers:
-
X-RateLimit-Limit: Total requests allowed per time window
-
X-RateLimit-Remaining: Requests remaining in current window
-
X-RateLimit-Reset: Unix timestamp when the limit resets
Instance Method Summary collapse
-
#available? ⇒ Boolean
Checks if requests are available (not exhausted).
-
#exhausted? ⇒ Boolean
Checks if the rate limit has been exhausted.
-
#initialize(attributes = {}) ⇒ RateLimitState
constructor
Override constructor to ensure immutability.
-
#limit ⇒ Integer?
Total requests allowed per time window (from X-RateLimit-Limit header).
-
#percentage_remaining ⇒ Float?
Calculates the percentage of rate limit quota remaining.
-
#remaining ⇒ Integer?
Requests remaining in current time window (from X-RateLimit-Remaining header).
-
#reset_at ⇒ Time?
Time when the rate limit window resets (from X-RateLimit-Reset header).
Constructor Details
#initialize(attributes = {}) ⇒ RateLimitState
Override constructor to ensure immutability
125 126 127 128 |
# File 'lib/sec_api/rate_limit_state.rb', line 125 def initialize(attributes = {}) super freeze end |
Instance Method Details
#available? ⇒ Boolean
Checks if requests are available (not exhausted).
Returns true if remaining is greater than zero OR if remaining is unknown. This conservative approach assumes requests are available when state is unknown.
91 92 93 |
# File 'lib/sec_api/rate_limit_state.rb', line 91 def available? !exhausted? end |
#exhausted? ⇒ Boolean
Checks if the rate limit has been exhausted.
Returns true only when we know for certain that remaining requests is zero. Returns false if remaining is unknown (nil) or greater than zero.
70 71 72 |
# File 'lib/sec_api/rate_limit_state.rb', line 70 def exhausted? remaining == 0 end |
#limit ⇒ Integer?
Total requests allowed per time window (from X-RateLimit-Limit header).
43 |
# File 'lib/sec_api/rate_limit_state.rb', line 43 attribute? :limit, Types::Coercible::Integer.optional |
#percentage_remaining ⇒ Float?
Calculates the percentage of rate limit quota remaining.
Returns nil if either limit or remaining is unknown, as percentage cannot be calculated without both values.
117 118 119 120 121 122 |
# File 'lib/sec_api/rate_limit_state.rb', line 117 def percentage_remaining return nil if limit.nil? || remaining.nil? return 0.0 if limit.zero? (remaining.to_f / limit * 100).round(1) end |
#remaining ⇒ Integer?
Requests remaining in current time window (from X-RateLimit-Remaining header).
47 |
# File 'lib/sec_api/rate_limit_state.rb', line 47 attribute? :remaining, Types::Coercible::Integer.optional |
#reset_at ⇒ Time?
Time when the rate limit window resets (from X-RateLimit-Reset header).
51 |
# File 'lib/sec_api/rate_limit_state.rb', line 51 attribute? :reset_at, Types::Strict::Time.optional |