Class: RestClientWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/cartocss_helper/util/rest-client_wrapper.rb

Instance Method Summary collapse

Constructor Details

#initializeRestClientWrapper

Returns a new instance of RestClientWrapper.



7
8
9
# File 'lib/cartocss_helper/util/rest-client_wrapper.rb', line 7

def initialize
  @last_url_fetched = nil
end

Instance Method Details

#execute_fetch_data_from_url(url, request_timeout) ⇒ Object



31
32
33
34
35
36
# File 'lib/cartocss_helper/util/rest-client_wrapper.rb', line 31

def execute_fetch_data_from_url(url, request_timeout)
  data = RestClient::Request.execute(method: :get, url: url, timeout: request_timeout)
  # see https://github.com/rest-client/rest-client/issues/370, will be fixed in 2.0
  # published versions listed on https://rubygems.org/gems/rest-client/versions/
  return String.new(data)
end

#fetch_data_from_url(url, request_timeout) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cartocss_helper/util/rest-client_wrapper.rb', line 11

def fetch_data_from_url(url, request_timeout)
  wait if url == @last_url_fetched
  @last_url_fetched = url
  return execute_fetch_data_from_url(url, request_timeout)
rescue RestClient::RequestTimeout => e
  raise RequestTimeout, e.to_s
rescue RestClient::ExceptionWithResponse => e
  raise_exception_about_returned_response(e)
rescue RestClient::SSLCertificateNotVerified, RestClient::ServerBrokeConnection, SocketError, URI::InvalidURIError
  raise ExceptionWithoutResponse.new(e), e.to_s
rescue ArgumentError => e
  puts "url:"
  puts url
  raise_issue_359_exception(e)
rescue => e
  puts 'unhandled exception! It is a clear bug!'
  puts "<#{e.class} error happened>"
  raise e, e.to_s
end

#raise_exception_about_returned_response(e) ⇒ Object

Raises:

  • (failure)


42
43
44
45
46
47
# File 'lib/cartocss_helper/util/rest-client_wrapper.rb', line 42

def raise_exception_about_returned_response(e)
  failure = ExceptionWithResponse.new(e)
  failure.http_code = e.http_code
  failure.response = e.response
  raise failure, e.to_s
end

#raise_issue_359_exception(e) ⇒ Object

Raises:



38
39
40
# File 'lib/cartocss_helper/util/rest-client_wrapper.rb', line 38

def raise_issue_359_exception(e)
  raise BuggyRestClient, "ArgumentError from rest-client, most likely caused by https://github.com/rest-client/rest-client/issues/359 (requesting GBs of data) [#{e}]"
end

#waitObject



49
50
51
52
53
54
55
# File 'lib/cartocss_helper/util/rest-client_wrapper.rb', line 49

def wait
  progressbar = ProgressBar.create
  100.times do
    sleep 4
    progressbar.increment
  end
end