Class: GenericDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/cartocss_helper/util/generic_downloader.rb

Instance Method Summary collapse

Constructor Details

#initialize(timeout: 20, error_message: nil, stop_on_timeout: true, wrapper: RestClientWrapper.new) ⇒ GenericDownloader

Returns a new instance of GenericDownloader.



22
23
24
25
26
27
28
# File 'lib/cartocss_helper/util/generic_downloader.rb', line 22

def initialize(timeout: 20, error_message: nil, stop_on_timeout: true, wrapper: RestClientWrapper.new)
  @request_timeout = timeout
  @retry_count = 0
  @error_message = error_message
  @stop_on_timeout = stop_on_timeout
  @wrapper = wrapper
end

Instance Method Details

#get_specified_resource(url, description: nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cartocss_helper/util/generic_downloader.rb', line 55

def get_specified_resource(url, description: nil)
  Log.info description if description
  return @wrapper.fetch_data_from_url(url, @request_timeout)
rescue ExceptionWithResponse => e
  output_shared_error_part(url, e)
  Log.warn e.response
  Log.warn e.http_code
  if retry_allowed(e)
    get_specified_resource(url)
  else
    raise e
  end
rescue ExceptionWithoutResponse => e
  output_shared_error_part(url, e)
  raise e
end

#max_retry_countObject



30
31
32
# File 'lib/cartocss_helper/util/generic_downloader.rb', line 30

def max_retry_count
  5
end

#output_shared_error_part(url, e) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/cartocss_helper/util/generic_downloader.rb', line 34

def output_shared_error_part(url, e)
  Log.warn @error_message unless @error_message.nil?
  Log.warn
  Log.warn url
  Log.warn
  Log.warn e.class
  Log.warn e
end

#retry_allowed(exception_with_response) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cartocss_helper/util/generic_downloader.rb', line 43

def retry_allowed(exception_with_response)
  raise exception_with_response if @retry_count > max_retry_count
  if @stop_on_timeout
    raise exception_with_response if exception_with_response.is_a?(RequestTimeout)
  end
  if [429, 503, 504].include?(exception_with_response.http_code)
    @retry_count += 1
    return true
  end
  raise exception_with_response
end