ibm-watson-ruby 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile +1 -1
- data/lib/ibm_watson/base_service.rb +34 -12
- data/lib/ibm_watson/errors.rb +13 -0
- data/lib/ibm_watson/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6b4b50c8ea12f50a97ee0b4ae90dda7421fb43a2653dd57fe85339ee13ce3461
|
4
|
+
data.tar.gz: cd90a74e6e70b997e2ab0d8eb31fbc9a69f3db56d363f05c1d4a216972ad0ead
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 659a5f47d074988c8625073cfc9a22f63f47bb969035db21587c19a68b1dbc38d375660fb7ace89cfa3a0487e02dddec0c9bc8e4e8f965001ab51cd12a6e1b60
|
7
|
+
data.tar.gz: '01848851c458b159a1353acebf3451208751ee2da1db787154f3f8e99e04fd0dd182f1d3c6278e9d104f1843c17b1587bb3a93820ca3d47afec3c21e7a1b53b4'
|
data/Gemfile
CHANGED
@@ -31,7 +31,7 @@ module IBMWatson
|
|
31
31
|
attr_reader :username, :password, :timeouts
|
32
32
|
|
33
33
|
def default_timeouts
|
34
|
-
TimeoutConfig.new(connect:
|
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
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
59
|
-
|
60
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
-
|
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
|
data/lib/ibm_watson/errors.rb
CHANGED
@@ -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
|
|
data/lib/ibm_watson/version.rb
CHANGED
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.
|
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:
|
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.
|
259
|
+
rubygems_version: 2.7.3
|
260
260
|
signing_key:
|
261
261
|
specification_version: 4
|
262
262
|
summary: IBM Watson services in ruby
|