ibm-watson-ruby 0.3.3 → 0.4.0

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
2
  SHA1:
3
- metadata.gz: 3645dac214a7d7ed40ca2847c208b071e04d6cb3
4
- data.tar.gz: efef17701273dfa9cedf423a22510933d55e4c57
3
+ metadata.gz: 1029012b09d5ac7420c3e9ea683e1df46bf217d2
4
+ data.tar.gz: f49da85fe6b30c62bb9493e69ec39e1d0e57723f
5
5
  SHA512:
6
- metadata.gz: 4fecd39864a899f4e60c4a952e459db6a8fb63a79da90d013032f860b73a95f0563e8196acdfec0ec00fe639f2fb41819978526ff075844cf774c049a9af72e8
7
- data.tar.gz: 410346775517131ce05cfd41dffe74069f9aefba26b00da124aabfc4ac0a93da8c960a33aa21a345cb84d7aa80fd6ec455e1539f45f2ab1c80d74c3a976b1d01
6
+ metadata.gz: 87c5ba161f125abcab2e2bf2584bde03508b93ed14e400c703e045eea4efba23d1b5ee275eb3c118f87464f35af486697705b1a3ee4dad872847dc9d63643df4
7
+ data.tar.gz: 2406952ba20140fa9b4e01bfc142e0a6b4f5a4478e04367dfcf144eb71d8d25a43ff3e07f4340cfecdd6501e344b4bd725b11ab1cc186309f10778b5e6286adb
@@ -14,9 +14,14 @@ module IBMWatson
14
14
  end
15
15
  end
16
16
 
17
- def initialize(username:, password:)
17
+ # @param [String] username
18
+ # @param [String] password
19
+ # @param [Array] timeouts
20
+ # See https://github.com/httprb/http/wiki/Timeouts for how this argument is specified.
21
+ def initialize(username:, password:, timeouts: [:global, {write: 2, connect: 5, read: 5}])
18
22
  @username = username
19
23
  @password = password
24
+ @timeouts = timeouts
20
25
  end
21
26
 
22
27
  private
@@ -46,6 +51,10 @@ module IBMWatson
46
51
  http_chain.headers(accept: "application/json")
47
52
  end
48
53
 
54
+ def handle_timeout_error(timeout_error)
55
+ raise IBMWatson::Errors::WatsonTimeoutError, timeout_error
56
+ end
57
+
49
58
  def verify_http_result(result, verify_json: true)
50
59
  if result.status.between?(200, 299)
51
60
  if verify_json
@@ -58,12 +67,12 @@ module IBMWatson
58
67
  end
59
68
  end
60
69
 
61
- def generic_error_handler(erorr_klass, result)
70
+ def generic_error_handler(error_klass, result)
62
71
  if result.content_type.mime_type == 'application/json'
63
72
  json_data = JSON.parse(result.body)
64
- raise erorr_klass, "Server returned #{result.status} : #{json_data['error']}"
73
+ raise error_klass, "Server returned #{result.status} : #{json_data['error']}"
65
74
  else
66
- raise erorr_klass, "Server returned #{result.status} : #{result.reason}"
75
+ raise error_klass, "Server returned #{result.status} : #{result.reason}"
67
76
  end
68
77
  end
69
78
 
@@ -75,7 +84,7 @@ module IBMWatson
75
84
  end
76
85
 
77
86
  def basic_auth
78
- HTTP.basic_auth(user: username, pass: password)
87
+ HTTP.timeout(*@timeouts).basic_auth(user: username, pass: password)
79
88
  end
80
89
  end
81
90
  end
@@ -12,8 +12,11 @@ module IBMWatson
12
12
  IBMWatson::Conversation::Workspace.new.tap do |result_object|
13
13
  result_object.from_json(result)
14
14
  end
15
+ rescue HTTP::TimeoutError => error
16
+ handle_timeout_error(error)
15
17
  end
16
18
 
19
+
17
20
  def list_workspaces
18
21
  url = build_url('workspaces', query: { version: QUERY_VERSION })
19
22
  result = accept_json(basic_auth).get(url)
@@ -22,22 +25,30 @@ module IBMWatson
22
25
  json_result['workspaces'].map do |workspace_json|
23
26
  IBMWatson::Conversation::Workspace.new(workspace_json)
24
27
  end
28
+ rescue HTTP::TimeoutError => error
29
+ handle_timeout_error(error)
25
30
  end
26
31
 
27
32
  def create_workspace(workspace_data:)
28
33
  url = build_url('workspaces', query: { version: QUERY_VERSION })
29
34
  upload_workspace(url, workspace_data)
35
+ rescue HTTP::TimeoutError => error
36
+ handle_timeout_error(error)
30
37
  end
31
38
 
32
39
  def delete_workspace(workspace_id:)
33
40
  url = build_url('workspaces', workspace_id, query: { version: QUERY_VERSION })
34
41
  result = accept_json(basic_auth).delete(url)
35
42
  verify_http_result(result)
43
+ rescue HTTP::TimeoutError => error
44
+ handle_timeout_error(error)
36
45
  end
37
46
 
38
47
  def update_workspace(workspace_id:, workspace_data:)
39
48
  url = build_url('workspaces', workspace_id, query: { version: QUERY_VERSION })
40
49
  upload_workspace(url, workspace_data)
50
+ rescue HTTP::TimeoutError => error
51
+ handle_timeout_error(error)
41
52
  end
42
53
 
43
54
  def message(workspace_id:, input:, context:, alternate_intents: false)
@@ -52,6 +63,8 @@ module IBMWatson
52
63
  IBMWatson::Conversation::MessageResponse.new.tap do |result_object|
53
64
  result_object.from_json(result)
54
65
  end
66
+ rescue HTTP::TimeoutError => error
67
+ handle_timeout_error(error)
55
68
  end
56
69
 
57
70
  private
@@ -8,5 +8,8 @@ module IBMWatson
8
8
 
9
9
  class WatsonServerError < WatsonError
10
10
  end
11
+
12
+ class WatsonTimeoutError < WatsonServerError
13
+ end
11
14
  end
12
15
  end
@@ -1,3 +1,3 @@
1
1
  module IBMWatson
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4.0"
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.3.3
4
+ version: 0.4.0
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-04-19 00:00:00.000000000 Z
12
+ date: 2017-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: http
@@ -239,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
239
239
  version: '0'
240
240
  requirements: []
241
241
  rubyforge_project:
242
- rubygems_version: 2.5.2
242
+ rubygems_version: 2.6.11
243
243
  signing_key:
244
244
  specification_version: 4
245
245
  summary: IBM Watson services in ruby