Exception: X::TooManyRequests

Inherits:
ClientError show all
Defined in:
lib/x/errors/too_many_requests.rb

Overview

Error raised when rate limit is exceeded (HTTP 429)

Constant Summary

Constants inherited from HTTPError

HTTPError::JSON_CONTENT_TYPE_REGEXP

Instance Attribute Summary

Attributes inherited from HTTPError

#code, #response

Instance Method Summary collapse

Methods inherited from HTTPError

#error_message, #initialize, #json?, #message_from_json_response

Constructor Details

This class inherits a constructor from X::HTTPError

Instance Method Details

#rate_limitRateLimit?

Get the most restrictive rate limit

Examples:

Get the rate limit

error.rate_limit

Returns:

  • (RateLimit, nil)

    the rate limit with the latest reset time



14
15
16
# File 'lib/x/errors/too_many_requests.rb', line 14

def rate_limit
  rate_limits.max_by(&:reset_at)
end

#rate_limitsArray<RateLimit>

Get all rate limits from the response

Examples:

Get all rate limits

error.rate_limits

Returns:

  • (Array<RateLimit>)

    the rate limits that are exhausted



24
25
26
27
28
# File 'lib/x/errors/too_many_requests.rb', line 24

def rate_limits
  @rate_limits ||= RateLimit::TYPES.filter_map do |type|
    RateLimit.new(type:, response:) if response["x-#{type}-remaining"].eql?("0")
  end
end

#reset_atTime

Get the time when the rate limit resets

Examples:

Get the reset time

error.reset_at

Returns:

  • (Time)

    the reset time



36
37
38
# File 'lib/x/errors/too_many_requests.rb', line 36

def reset_at
  rate_limit&.reset_at || Time.at(0)
end

#reset_inInteger Also known as: retry_after

Get the seconds until the rate limit resets

Examples:

Get the time until reset

error.reset_in

Returns:

  • (Integer)

    the seconds until reset



46
47
48
# File 'lib/x/errors/too_many_requests.rb', line 46

def reset_in
  [(reset_at - Time.now).ceil, 0].max
end