Class: X::RateLimit

Inherits:
Object
  • Object
show all
Defined in:
x-core/lib/x/core/rate_limit.rb

Overview

Represents rate limit information from an API response

Constant Summary collapse

RATE_LIMIT_TYPE =

Rate limit type identifier

"rate-limit"
APP_LIMIT_TYPE =

App limit type identifier

"app-limit-24hour"
USER_LIMIT_TYPE =

User limit type identifier

"user-limit-24hour"
TYPES =

All supported rate limit types

[RATE_LIMIT_TYPE, APP_LIMIT_TYPE, USER_LIMIT_TYPE].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#type ⇒ String (readonly)

The type of rate limit

Examples:

Get the rate limit type

rate_limit.type # => "rate-limit"

Returns:

  • (String) —

    the type of rate limit



27
28
29
# File 'x-core/lib/x/core/rate_limit.rb', line 27

def type
  @type
end

Instance Method Details

#exhausted? ⇒ Boolean

Check whether the limit has no requests left

Examples:

Wait for a limit that is used up

sleep rate_limit.reset_in if rate_limit.exhausted?

Returns:

  • (Boolean) —

    true if no requests remain in the window



99
# File 'x-core/lib/x/core/rate_limit.rb', line 99

def exhausted? = remaining.zero?

#limit ⇒ Integer

Get the rate limit maximum

Examples:

Get the rate limit

rate_limit.limit

Returns:

  • (Integer) —

    the maximum number of requests allowed



83
# File 'x-core/lib/x/core/rate_limit.rb', line 83

def limit = field("limit")

#remaining ⇒ Integer

Get the remaining requests

Examples:

Get the remaining requests

rate_limit.remaining

Returns:

  • (Integer) —

    the number of requests remaining



91
# File 'x-core/lib/x/core/rate_limit.rb', line 91

def remaining = field("remaining")

#reset_at ⇒ Time

Get the time when the rate limit resets

Examples:

Get the reset time

rate_limit.reset_at

Returns:

  • (Time) —

    the time when the rate limit resets



107
# File 'x-core/lib/x/core/rate_limit.rb', line 107

def reset_at = Time.at(field("reset"))

#reset_in ⇒ Integer

Get the seconds until the rate limit resets

Examples:

Get the reset time in seconds

rate_limit.reset_in

Returns:

  • (Integer) —

    the seconds until the rate limit resets



115
116
117
# File 'x-core/lib/x/core/rate_limit.rb', line 115

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