Class: Cielo24::WebUtils
- Inherits:
-
Object
- Object
- Cielo24::WebUtils
- Includes:
- JSON, TZInfo
- Defined in:
- lib/cielo24/web_utils.rb
Constant Summary collapse
- BASIC_TIMEOUT =
seconds (1 minute)
60
- DOWNLOAD_TIMEOUT =
seconds (5 minutes)
300
- UPLOAD_TIMEOUT =
seconds (1 week)
60*60*24*7
- SERVER_TZ =
'America/Los_Angeles'
- LOGGER =
Logger.new(STDOUT)
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
- .json_parse(s) ⇒ Object
- .to_utc(s) ⇒ Object
Class Method Details
.get_json(uri, method, timeout, query = nil, headers = nil, body = nil) ⇒ Object
18 19 20 21 |
# File 'lib/cielo24/web_utils.rb', line 18 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
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cielo24/web_utils.rb', line 23 def self.http_request(uri, method, timeout, query=nil, headers=nil, body=nil) http_client = HTTPClient.new http_client. = nil http_client.send_timeout = UPLOAD_TIMEOUT LOGGER.info(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(response.status_code, json['ErrorType'], json['ErrorComment']) end } rescue Timeout::Error raise TimeoutError.new('The HTTP session has timed out.') end end |
.json_parse(s) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/cielo24/web_utils.rb', line 58 def self.json_parse(s) begin return JSON.parse(s) rescue JSON::ParserError raise ParsingError.new("Bad JSON String: \"#{s}\"") end end |
.to_utc(s) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/cielo24/web_utils.rb', line 49 def self.to_utc(s) return s if s.empty? tz = Timezone.get(SERVER_TZ) local = DateTime.iso8601(s) utc = tz.local_to_utc local format = '%Y-%m-%dT%H:%M:%S.%L%z' # iso8601 with milliseconds utc.strftime(format) end |