Class: Medidata::API::Response::Ratelimit

Inherits:
Object
  • Object
show all
Defined in:
lib/medidata/api/http.rb

Overview

Provide useful functionality around API rate limiting.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limit, remaining, reset) ⇒ Ratelimit

  • 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



17
18
19
20
21
# File 'lib/medidata/api/http.rb', line 17

def initialize(limit, remaining, reset)
  @limit = limit.to_i
  @remaining = remaining.to_i
  @reset = Time.at reset.to_i
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



11
12
13
# File 'lib/medidata/api/http.rb', line 11

def limit
  @limit
end

#remainingObject (readonly)

Returns the value of attribute remaining.



11
12
13
# File 'lib/medidata/api/http.rb', line 11

def remaining
  @remaining
end

#resetObject (readonly)

Returns the value of attribute reset.



11
12
13
# File 'lib/medidata/api/http.rb', line 11

def reset
  @reset
end

Instance Method Details

#exceeded?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/medidata/api/http.rb', line 23

def exceeded?
  remaining <= 0
end

#usedObject

  • Returns :

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



30
31
32
# File 'lib/medidata/api/http.rb', line 30

def used
  limit - remaining
end

#wait!Object

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.



40
41
42
43
44
45
46
47
48
49
# File 'lib/medidata/api/http.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