Module: RelatonW3c::RateLimitHandler

Included in:
DataFetcher, DataParser
Defined in:
lib/relaton_w3c/rate_limit_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fetched_objectsObject



3
4
5
# File 'lib/relaton_w3c/rate_limit_handler.rb', line 3

def self.fetched_objects
  @fetched_objects ||= {}
end

Instance Method Details

#realize(obj) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/relaton_w3c/rate_limit_handler.rb', line 7

def realize(obj)
  href = obj.href || obj.links.self.href
  return RateLimitHandler.fetched_objects[href] if RateLimitHandler.fetched_objects.key?(href)

  n = 1
  begin
    RateLimitHandler.fetched_objects[href] = obj.realize
  rescue NameError, # NameError caused by lutaml-hal-0.1.7/lib/lutaml/hal/client.rb:51:in `rescue in get': uninitialized constant Lutaml::Hal::Client::ConnectionError
      Faraday::ConnectionFailed, Net::OpenTimeout => e
    if n < 5
      sleep_time = n * n
      n += 1
      Util.warn "Rate limit exceeded for #{href}, retrying in #{sleep_time} seconds..."
      sleep sleep_time
      retry
    else
      Util.warn "Failed to realize object: #{href}"
      raise e
    end
  rescue Lutaml::Hal::NotFoundError
    Util.warn "Object not found: #{href}"
    RateLimitHandler.fetched_objects[href] = nil
  end
end