Class: Booqable::RateLimit

Inherits:
Struct
  • Object
show all
Defined in:
lib/booqable/rate_limit.rb,
sig/booqable.rbs

Overview

Rate limit information from API responses

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#limitInteger?

Returns the value of attribute limit.

Returns:

  • (Integer, nil)


178
179
180
# File 'sig/booqable.rbs', line 178

def limit
  @limit
end

#remainingInteger?

Returns the value of attribute remaining.

Returns:

  • (Integer, nil)


179
180
181
# File 'sig/booqable.rbs', line 179

def remaining
  @remaining
end

#resets_inInteger?

Returns the value of attribute resets_in.

Returns:

  • (Integer, nil)


180
181
182
# File 'sig/booqable.rbs', line 180

def resets_in
  @resets_in
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