Class: Transprt::RateLimiting

Inherits:
Object
  • Object
show all
Defined in:
lib/transprt/rate_limiting.rb

Instance Method Summary collapse

Constructor Details

#initialize(wait_for_quota = true) ⇒ RateLimiting

Returns a new instance of RateLimiting.

Parameters:

  • wait_for_quota (Boolean) (defaults to: true)

    whether to wait for quota reset and query again if the rate limit (300 requests/s) is exceeded.



7
8
9
# File 'lib/transprt/rate_limiting.rb', line 7

def initialize(wait_for_quota = true)
  @wait_for_quota = wait_for_quota
end

Instance Method Details

#get(url) ⇒ Object

Returns The HTTP response or nil if we’re hitting the rate limit and wait_for_quota is false (@see #initialize).

Returns:

  • The HTTP response or nil if we’re hitting the rate limit and wait_for_quota is false (@see #initialize).



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/transprt/rate_limiting.rb', line 13

def get(url)
  begin
    response = perform_get(url)
  rescue RestClient::TooManyRequests => e
    # API uses HTTP 429 to notify us,
    # @see https://github.com/OpendataCH/Transport/blob/master/lib/Transport/Application.php

    return nil unless wait_for_quota

    sleep_until_quota_reset(e.response)
    response = perform_get(url)
  end

  response
end