Module: RestClientExt
- Includes:
- RestClient
- Defined in:
- lib/conan/application_helper.rb
Constant Summary collapse
- RETRIES =
3
Class Method Summary collapse
- .delete(url, headers = {}, &block) ⇒ Object
- .execute_request(verb, url, headers, payload = nil, tries = RETRIES, &block) ⇒ Object
- .get(url, headers = {}, &block) ⇒ Object
- .handle_network_error(verb, err, url, tries) ⇒ Object
- .post(url, payload, headers = {}, &block) ⇒ Object
- .put(url, payload, headers = {}, &block) ⇒ Object
- .request_options(verb, url, headers, payload = nil) ⇒ Object
Class Method Details
.delete(url, headers = {}, &block) ⇒ Object
55 56 57 |
# File 'lib/conan/application_helper.rb', line 55 def self.delete(url, headers={}, &block) execute_request("DELETE", url, headers, &block) end |
.execute_request(verb, url, headers, payload = nil, tries = RETRIES, &block) ⇒ Object
37 38 39 40 41 |
# File 'lib/conan/application_helper.rb', line 37 def self.execute_request(verb, url, headers, payload = nil, tries = RETRIES, &block) Request.execute((verb, url, headers, payload),&block) rescue SystemCallError, Exception => err retry unless (tries = handle_network_error(verb, err, url, tries)).zero? end |
.get(url, headers = {}, &block) ⇒ Object
43 44 45 |
# File 'lib/conan/application_helper.rb', line 43 def self.get(url, headers={}, &block) execute_request("GET", url, headers, &block) end |
.handle_network_error(verb, err, url, tries) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/conan/application_helper.rb', line 10 def self.handle_network_error(verb, err, url, tries) additional_tries = tries - 1 = "#{verb} to #{url} failed with (#{err.ai}), retrying #{additional_tries} more times" case err when Errno::ETIMEDOUT, Errno::ECONNREFUSED, RestClient::ServerBrokeConnection, RestClient::RequestTimeout puts sleep 1 else raise err end additional_tries end |
.post(url, payload, headers = {}, &block) ⇒ Object
47 48 49 |
# File 'lib/conan/application_helper.rb', line 47 def self.post(url, payload, headers={}, &block) execute_request("POST", url, headers, payload, &block) end |
.put(url, payload, headers = {}, &block) ⇒ Object
51 52 53 |
# File 'lib/conan/application_helper.rb', line 51 def self.put(url, payload, headers={}, &block) execute_request("PUT", url, headers, payload, &block) end |
.request_options(verb, url, headers, payload = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/conan/application_helper.rb', line 25 def self.(verb, url, headers, payload = nil) opts = { :method => verb.downcase.to_sym, :url => url, :headers => headers.clone, :timeout => headers[:timeout] || 10, :open_timeout => headers[:open_timeout] || 10 } payload ? opts.merge(:payload => payload) : opts end |