Class: Booqable::RateLimit
- Inherits:
-
Struct
- Object
- Struct
- Booqable::RateLimit
- Defined in:
- lib/booqable/rate_limit.rb
Overview
Rate limit information from API responses
Contains rate limiting information extracted from HTTP response headers. This information is available when rate limit errors occur or can be accessed from successful responses to monitor API usage.
Instance Attribute Summary collapse
-
#limit ⇒ Integer
writeonly
Max tries per rate limit period.
-
#remaining ⇒ Integer
writeonly
Remaining tries per rate limit period.
-
#resets_in ⇒ Integer
writeonly
Number of seconds when rate limit resets.
Class Method Summary collapse
-
.from_response(response) ⇒ RateLimit
Extract rate limit information from HTTP response.
Instance Attribute Details
#limit=(value) ⇒ Integer (writeonly)
Returns Max tries per rate limit period.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/booqable/rate_limit.rb', line 25 class RateLimit < Struct.new(:limit, :remaining, :resets_in) # Extract rate limit information from HTTP response # # Parses standard rate limit headers from the HTTP response and creates # a RateLimit object with the extracted information. Falls back to default # values if headers are missing. # # @param response [#headers, #response_headers] HTTP response object # @return [RateLimit] Rate limit information object # # @example # rate_limit = Booqable::RateLimit.from_response(response) # puts "#{rate_limit.remaining} requests remaining" def self.from_response(response) info = new headers = response.headers if response.respond_to?(:headers) && !response.headers.nil? headers ||= response.response_headers if response.respond_to?(:response_headers) && !response.response_headers.nil? if headers info.limit = (headers["X-RateLimit-Limit"] || 1).to_i info.remaining = (headers["X-RateLimit-Remaining"] || 1).to_i info.resets_in = (headers["X-RateLimit-Period"] || 1).to_i end info end end |
#remaining=(value) ⇒ Integer (writeonly)
Returns Remaining tries per rate limit period.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/booqable/rate_limit.rb', line 25 class RateLimit < Struct.new(:limit, :remaining, :resets_in) # Extract rate limit information from HTTP response # # Parses standard rate limit headers from the HTTP response and creates # a RateLimit object with the extracted information. Falls back to default # values if headers are missing. # # @param response [#headers, #response_headers] HTTP response object # @return [RateLimit] Rate limit information object # # @example # rate_limit = Booqable::RateLimit.from_response(response) # puts "#{rate_limit.remaining} requests remaining" def self.from_response(response) info = new headers = response.headers if response.respond_to?(:headers) && !response.headers.nil? headers ||= response.response_headers if response.respond_to?(:response_headers) && !response.response_headers.nil? if headers info.limit = (headers["X-RateLimit-Limit"] || 1).to_i info.remaining = (headers["X-RateLimit-Remaining"] || 1).to_i info.resets_in = (headers["X-RateLimit-Period"] || 1).to_i end info end end |
#resets_in=(value) ⇒ Integer (writeonly)
Returns Number of seconds when rate limit resets.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/booqable/rate_limit.rb', line 25 class RateLimit < Struct.new(:limit, :remaining, :resets_in) # Extract rate limit information from HTTP response # # Parses standard rate limit headers from the HTTP response and creates # a RateLimit object with the extracted information. Falls back to default # values if headers are missing. # # @param response [#headers, #response_headers] HTTP response object # @return [RateLimit] Rate limit information object # # @example # rate_limit = Booqable::RateLimit.from_response(response) # puts "#{rate_limit.remaining} requests remaining" def self.from_response(response) info = new headers = response.headers if response.respond_to?(:headers) && !response.headers.nil? headers ||= response.response_headers if response.respond_to?(:response_headers) && !response.response_headers.nil? if headers info.limit = (headers["X-RateLimit-Limit"] || 1).to_i info.remaining = (headers["X-RateLimit-Remaining"] || 1).to_i info.resets_in = (headers["X-RateLimit-Period"] || 1).to_i end info end end |
Class Method Details
.from_response(response) ⇒ RateLimit
Extract rate limit information from HTTP response
Parses standard rate limit headers from the HTTP response and creates a RateLimit object with the extracted information. Falls back to default values if headers are missing.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/booqable/rate_limit.rb', line 38 def self.from_response(response) info = new headers = response.headers if response.respond_to?(:headers) && !response.headers.nil? headers ||= response.response_headers if response.respond_to?(:response_headers) && !response.response_headers.nil? if headers info.limit = (headers["X-RateLimit-Limit"] || 1).to_i info.remaining = (headers["X-RateLimit-Remaining"] || 1).to_i info.resets_in = (headers["X-RateLimit-Period"] || 1).to_i end info end |