ibm-watson-ruby 0.7.0 → 0.7.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4ad6ca10f77eed8a2386f95b7ac76c1be0c0c1fc
4
- data.tar.gz: 6e1dade8c85bd7aaff082706b26e93d3530f93a3
2
+ SHA256:
3
+ metadata.gz: 6b4b50c8ea12f50a97ee0b4ae90dda7421fb43a2653dd57fe85339ee13ce3461
4
+ data.tar.gz: cd90a74e6e70b997e2ab0d8eb31fbc9a69f3db56d363f05c1d4a216972ad0ead
5
5
  SHA512:
6
- metadata.gz: 42686ba010e5d89b984f00038c08c0cb748ace6256ce543d55327320ccd044493a8676407f181fffc8007f5cdb2b5a3a7c32d676c493a92da1166ac66f03b3d7
7
- data.tar.gz: 511df1c6c12f0a8bdf39868a602dde5e813caabd30df15e5ac58f4d3caa233c6d31813a746384c91237181f9dc5dd46dd8a672e9ccf5e80b63e90f963b92d8ea
6
+ metadata.gz: 659a5f47d074988c8625073cfc9a22f63f47bb969035db21587c19a68b1dbc38d375660fb7ace89cfa3a0487e02dddec0c9bc8e4e8f965001ab51cd12a6e1b60
7
+ data.tar.gz: '01848851c458b159a1353acebf3451208751ee2da1db787154f3f8e99e04fd0dd182f1d3c6278e9d104f1843c17b1587bb3a93820ca3d47afec3c21e7a1b53b4'
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
- ruby "~> 2.3.0"
2
+ ruby "~> 2.5.0"
3
3
 
4
4
  gemspec
@@ -31,7 +31,7 @@ module IBMWatson
31
31
  attr_reader :username, :password, :timeouts
32
32
 
33
33
  def default_timeouts
34
- TimeoutConfig.new(connect: 5, read: 5)
34
+ TimeoutConfig.new(connect: 30, read: 30)
35
35
  end
36
36
 
37
37
  def connection
@@ -49,22 +49,28 @@ module IBMWatson
49
49
  end
50
50
 
51
51
  def get(path, *args)
52
- result = connection.get(path, *args)
53
- verify_http_result(result)
54
- JSON.parse(result.body)
52
+ retry_maybe do
53
+ result = connection.get(path, *args)
54
+ verify_http_result(result)
55
+ JSON.parse(result.body)
56
+ end
55
57
  end
56
58
 
57
59
  def delete(path, *args)
58
- result = connection.delete(path, *args)
59
- verify_http_result(result)
60
- JSON.parse(result.body)
60
+ retry_maybe do
61
+ result = connection.delete(path, *args)
62
+ verify_http_result(result)
63
+ JSON.parse(result.body)
64
+ end
61
65
  end
62
66
 
63
67
  def post(path, body, *args)
64
- body = JSON.generate(body) unless body.kind_of?(String)
65
- result = connection.post(path, body, *args)
66
- verify_http_result(result)
67
- JSON.parse(result.body)
68
+ retry_maybe do
69
+ body = JSON.generate(body) unless body.kind_of?(String)
70
+ result = connection.post(path, body, *args)
71
+ verify_http_result(result)
72
+ JSON.parse(result.body)
73
+ end
68
74
  end
69
75
 
70
76
  def parse_array(json_string, model_class, root_key)
@@ -87,6 +93,8 @@ module IBMWatson
87
93
  if verify_json
88
94
  verify_no_json_failure(result)
89
95
  end
96
+ elsif result.status == 429
97
+ generic_error_handler(IBMWatson::Errors::RateLimitReached, result)
90
98
  elsif result.status.between?(400, 499)
91
99
  generic_error_handler(IBMWatson::Errors::WatsonRequestError, result)
92
100
  elsif result.status.between?(500, 599)
@@ -97,7 +105,11 @@ module IBMWatson
97
105
  def generic_error_handler(error_klass, result)
98
106
  if result.headers[:content_type].split(';').first == 'application/json'
99
107
  json_data = JSON.parse(result.body)
100
- raise error_klass, "Server returned #{result.status} : #{json_data['error']}"
108
+ if error_klass.respond_to?(:from_data)
109
+ raise error_klass.new("Server returned #{result.status} : #{json_data['error']}", json_data, result.headers)
110
+ else
111
+ raise error_klass, "Server returned #{result.status} : #{json_data['error']}"
112
+ end
101
113
  else
102
114
  raise error_klass, "Server returned #{result.status} : #{result.reason}"
103
115
  end
@@ -112,8 +124,18 @@ module IBMWatson
112
124
 
113
125
  def handle_timeout
114
126
  yield
127
+ rescue Net::OpenTimeout => error
128
+ handle_timeout_error(error)
115
129
  rescue Faraday::Error::TimeoutError => error
116
130
  handle_timeout_error(error)
117
131
  end
132
+
133
+ def retry_maybe
134
+ yield
135
+ rescue Errors::RateLimitReached => e
136
+ warn("Rate Limit Reached, waiting for #{e.retry_after} seconds ...")
137
+ sleep e.retry_after
138
+ retry
139
+ end
118
140
  end
119
141
  end
@@ -6,6 +6,19 @@ module IBMWatson
6
6
  class WatsonRequestError < WatsonError
7
7
  end
8
8
 
9
+ class RateLimitReached < WatsonRequestError
10
+ def self.from_data
11
+ # does nothing, just a marker
12
+ end
13
+
14
+ def initialize(message, data, headers)
15
+ @retry_after = headers['retry-after'].to_f
16
+ super(message)
17
+ end
18
+
19
+ attr_reader :retry_after
20
+ end
21
+
9
22
  class WatsonServerError < WatsonError
10
23
  end
11
24
 
@@ -1,3 +1,3 @@
1
1
  module IBMWatson
2
- VERSION = "0.7.0"
2
+ VERSION = "0.7.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibm-watson-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bram Whillock
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-11-27 00:00:00.000000000 Z
12
+ date: 2018-03-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -256,7 +256,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
256
256
  version: '0'
257
257
  requirements: []
258
258
  rubyforge_project:
259
- rubygems_version: 2.6.11
259
+ rubygems_version: 2.7.3
260
260
  signing_key:
261
261
  specification_version: 4
262
262
  summary: IBM Watson services in ruby