Class: Booqable::RateLimit

Inherits:
Struct
  • Object
show all
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.

Examples:

Accessing rate limit info from an error

begin
  Booqable.orders.list
rescue Booqable::TooManyRequests => e
  rate_limit = e.context
  puts "Rate limit: #{rate_limit.remaining}/#{rate_limit.limit}"
  puts "Resets in: #{rate_limit.resets_in} seconds"
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#limit=(value) ⇒ Integer (writeonly)

Returns Max tries per rate limit period.

Returns:

  • (Integer)

    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.

Returns:

  • (Integer)

    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.

Returns:

  • (Integer)

    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.

Examples:

rate_limit = Booqable::RateLimit.from_response(response)
puts "#{rate_limit.remaining} requests remaining"

Parameters:

  • response (#headers, #response_headers)

    HTTP response object

Returns:

  • (RateLimit)

    Rate limit information object



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