class SendGrid::Response::Ratelimit

Provide useful functionality around API rate limiting.

Attributes

limit[R]
remaining[R]
reset[R]

Public Class Methods

new(limit, remaining, reset) click to toggle source
  • Args :

    • limit -> The total number of requests allowed within a rate limit window

    • remaining -> The number of requests that have been processed within this current rate limit window

    • reset -> The time (in seconds since Unix Epoch) when the rate limit will reset

# File lib/ruby_http_client.rb, line 17
def initialize(limit, remaining, reset)
  @limit = limit.to_i
  @remaining = remaining.to_i
  @reset = Time.at reset.to_i
end

Public Instance Methods

exceeded?() click to toggle source
# File lib/ruby_http_client.rb, line 23
def exceeded?
  remaining <= 0
end
used() click to toggle source
  • Returns :

    • The number of requests that have been used out of this rate limit window

# File lib/ruby_http_client.rb, line 30
def used
  limit - remaining
end
wait!() { || ... } click to toggle source

Sleep until the reset time arrives. If given a block, it will be called after sleeping is finished.

  • Returns :

    • The amount of time (in seconds) that the rate limit slept for.

# File lib/ruby_http_client.rb, line 40
def wait!
  now = Time.now.utc.to_i
  duration = (reset.to_i - now) + 1

  sleep duration if duration >= 0

  yield if block_given?

  duration
end