cielo24 0.0.16 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
- module Cielo24
2
- VERSION = '0.0.16'
3
- end
1
+ module Cielo24
2
+ VERSION = '0.0.17'
3
+ end
@@ -1,75 +1,102 @@
1
- module Cielo24
2
- class WebUtils
3
-
4
- require 'json'
5
- require 'httpclient'
6
- require 'timeout'
7
- require 'logger'
8
- require 'tzinfo'
9
- include JSON
10
-
11
- BASIC_TIMEOUT = 60 # seconds (1 minute)
12
- DOWNLOAD_TIMEOUT = 300 # seconds (5 minutes)
13
- UPLOAD_TIMEOUT = 60*60*24*7 # seconds (1 week)
14
- SERVER_TZ = 'America/Los_Angeles'
15
- LOGGER = Logger.new(STDOUT)
16
-
17
- def self.get_json(uri, method, timeout, query=nil, headers=nil, body=nil)
18
- response = http_request(uri, method, timeout, query, headers, body)
19
- return JSON.parse(response)
20
- end
21
-
22
- def self.http_request(uri, method, timeout, query=nil, headers=nil, body=nil)
23
- http_client = HTTPClient.new
24
- http_client.cookie_manager = nil
25
- http_client.send_timeout = UPLOAD_TIMEOUT
26
- LOGGER.info(uri + (query.nil? ? '' : '?' + URI.encode_www_form(query)))
27
-
28
- # Timeout block:
29
- begin
30
- # Error is raised if the following block fails to execute in 'timeout' sec:
31
- Timeout.timeout(timeout) { # nil timeout = infinite
32
-
33
- response = http_client.request(method, uri, query, body, headers, nil)
34
- # Handle web errors
35
- if response.status_code == 200 or response.status_code == 204
36
- return response.body
37
- else
38
- json = JSON.parse(response.body)
39
- raise WebError.new(json['ErrorType'], json['ErrorComment'])
40
- end
41
-
42
- }
43
- rescue Timeout::Error
44
- raise TimeoutError.new('The HTTP session has timed out.')
45
- end
46
- end
47
-
48
- def self.to_utc(s)
49
- return s if s.empty?
50
- tz = TZInfo::Timezone.get(SERVER_TZ)
51
- local = DateTime.iso8601(s)
52
- utc = tz.local_to_utc local
53
- format = '%Y-%m-%dT%H:%M:%S.%L%z' # iso8601 with milliseconds
54
- utc.strftime(format)
55
- end
56
- end
57
-
58
- class WebError < StandardError
59
- attr_reader :type
60
- def initialize(type, comment)
61
- super(comment)
62
- @type = type
63
- end
64
-
65
- def to_s
66
- return @type + ' - ' + super.to_s
67
- end
68
- end
69
-
70
- class TimeoutError < StandardError
71
- def initialize(message)
72
- super(message)
73
- end
74
- end
1
+ module Cielo24
2
+ class WebUtils
3
+
4
+ require 'json'
5
+ require 'httpclient'
6
+ require 'timeout'
7
+ require 'logger'
8
+ require 'tzinfo'
9
+ include JSON
10
+ include TZInfo
11
+
12
+ BASIC_TIMEOUT = 60 # seconds (1 minute)
13
+ DOWNLOAD_TIMEOUT = 300 # seconds (5 minutes)
14
+ UPLOAD_TIMEOUT = 60*60*24*7 # seconds (1 week)
15
+ SERVER_TZ = 'America/Los_Angeles'
16
+ LOGGER = Logger.new(STDOUT)
17
+
18
+ def self.get_json(uri, method, timeout, query=nil, headers=nil, body=nil)
19
+ response = http_request(uri, method, timeout, query, headers, body)
20
+ return json_parse(response)
21
+ end
22
+
23
+ def self.http_request(uri, method, timeout, query=nil, headers=nil, body=nil)
24
+ http_client = HTTPClient.new
25
+ http_client.cookie_manager = nil
26
+ http_client.send_timeout = UPLOAD_TIMEOUT
27
+ LOGGER.info(uri + (query.nil? ? '' : '?' + URI.encode_www_form(query)))
28
+
29
+ # Timeout block:
30
+ begin
31
+ # Error is raised if the following block fails to execute in 'timeout' sec:
32
+ Timeout.timeout(timeout) { # nil timeout = infinite
33
+
34
+ response = http_client.request(method, uri, query, body, headers, nil)
35
+ # Handle web errors
36
+ if response.status_code == 200 or response.status_code == 204
37
+ return response.body
38
+ else
39
+ json = json_parse(response.body)
40
+ raise WebError.new(response.status_code, json['ErrorType'], json['ErrorComment'])
41
+ end
42
+
43
+ }
44
+ rescue Timeout::Error
45
+ raise TimeoutError.new('The HTTP session has timed out.')
46
+ end
47
+ end
48
+
49
+ def self.to_utc(s)
50
+ return s if s.empty?
51
+ tz = Timezone.get(SERVER_TZ)
52
+ local = DateTime.iso8601(s)
53
+ utc = tz.local_to_utc local
54
+ format = '%Y-%m-%dT%H:%M:%S.%L%z' # iso8601 with milliseconds
55
+ utc.strftime(format)
56
+ end
57
+
58
+ def self.json_parse(s)
59
+ begin
60
+ return JSON.parse(s)
61
+ rescue JSON::ParserError
62
+ raise ParsingError.new("Bad JSON String: \"#{s}\"")
63
+ end
64
+ end
65
+ end
66
+
67
+ class CieloError < StandardError
68
+ attr_reader :type
69
+ def initialize(type, comment)
70
+ super(comment)
71
+ @type = type
72
+ end
73
+
74
+ def to_s
75
+ "#{@type} - #{super.to_s}"
76
+ end
77
+ end
78
+
79
+ class WebError < CieloError
80
+ attr_reader :status_code
81
+ def initialize(status_code, type, comment)
82
+ super(type, comment)
83
+ @status_code = status_code
84
+ end
85
+
86
+ def to_s
87
+ "#{@status_code}:#{super.to_s}"
88
+ end
89
+ end
90
+
91
+ class TimeoutError < CieloError
92
+ def initialize(message)
93
+ super('TIMEOUT_ERROR', message)
94
+ end
95
+ end
96
+
97
+ class ParsingError < CieloError
98
+ def initialize(message)
99
+ super('PARSING_ERROR', message)
100
+ end
101
+ end
75
102
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cielo24
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - cielo24
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-04 00:00:00.000000000 Z
11
+ date: 2015-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 1.2.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: tzinfo-data
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.2015.7
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.2015.7
69
83
  description: This gem allows you to interact with the cielo24 public REST API.
70
84
  email:
71
85
  - support@cielo24.com
@@ -98,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
112
  version: '0'
99
113
  requirements: []
100
114
  rubyforge_project:
101
- rubygems_version: 2.4.6
115
+ rubygems_version: 2.4.5.1
102
116
  signing_key:
103
117
  specification_version: 4
104
118
  summary: Cielo24 API gem.