Class: Cielo24::WebUtils
- Inherits:
-
Object
- Object
- Cielo24::WebUtils
- Includes:
- JSON
- Defined in:
- lib/cielo24/web_utils.rb
Constant Summary collapse
- BASIC_TIMEOUT =
seconds
60- DOWNLOAD_TIMEOUT =
seconds
300- @@LAST_URL =
For logging purposes
"none"
Class Method Summary collapse
- .get_json(uri, method, timeout, query = nil, headers = nil, body = nil) ⇒ Object
- .http_request(uri, method, timeout, query = nil, headers = nil, body = nil) ⇒ Object
- .LAST_URL ⇒ Object
Class Method Details
.get_json(uri, method, timeout, query = nil, headers = nil, body = nil) ⇒ Object
17 18 19 20 |
# File 'lib/cielo24/web_utils.rb', line 17 def self.get_json(uri, method, timeout, query=nil, headers=nil, body=nil) response = http_request(uri, method, timeout, query, headers, body) return JSON.parse(response) end |
.http_request(uri, method, timeout, query = nil, headers = nil, body = nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cielo24/web_utils.rb', line 22 def self.http_request(uri, method, timeout, query=nil, headers=nil, body=nil) http_client = HTTPClient.new http_client. = nil http_client.send_timeout = 60*60*24*7 # HTTPClient default timeout set to 7 days, our own timeout handler is down below @@LAST_URL = uri + (query.nil? ? "" : "?" + URI.encode_www_form(query)) # Timeout block: begin # Error is raised if the following block fails to execute in 'timeout' sec: Timeout.timeout(timeout) { # nil timeout = infinite response = http_client.request(method, uri, query, body, headers, nil) # Handle web errors if response.status_code == 200 or response.status_code == 204 return response.body else json = JSON.parse(response.body) raise WebError.new(json["ErrorType"], json["ErrorComment"]) end } rescue Timeout::Error raise TimeoutError.new("The HTTP session has timed out.") end end |
.LAST_URL ⇒ Object
13 14 15 |
# File 'lib/cielo24/web_utils.rb', line 13 def self.LAST_URL return @@LAST_URL end |