ibm_watson 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +258 -0
  3. data/bin/console +14 -0
  4. data/bin/setup +8 -0
  5. data/lib/ibm_watson.rb +16 -0
  6. data/lib/ibm_watson/assistant_v1.rb +1997 -0
  7. data/lib/ibm_watson/detailed_response.rb +21 -0
  8. data/lib/ibm_watson/discovery_v1.rb +2039 -0
  9. data/lib/ibm_watson/iam_token_manager.rb +166 -0
  10. data/lib/ibm_watson/language_translator_v3.rb +411 -0
  11. data/lib/ibm_watson/natural_language_classifier_v1.rb +309 -0
  12. data/lib/ibm_watson/natural_language_understanding_v1.rb +297 -0
  13. data/lib/ibm_watson/personality_insights_v3.rb +260 -0
  14. data/lib/ibm_watson/speech_to_text_v1.rb +2153 -0
  15. data/lib/ibm_watson/text_to_speech_v1.rb +716 -0
  16. data/lib/ibm_watson/tone_analyzer_v3.rb +287 -0
  17. data/lib/ibm_watson/version.rb +3 -0
  18. data/lib/ibm_watson/visual_recognition_v3.rb +579 -0
  19. data/lib/ibm_watson/watson_api_exception.rb +41 -0
  20. data/lib/ibm_watson/watson_service.rb +180 -0
  21. data/lib/ibm_watson/websocket/recognize_callback.rb +32 -0
  22. data/lib/ibm_watson/websocket/speech_to_text_websocket_listener.rb +162 -0
  23. data/rakefile +45 -0
  24. data/test/integration/test_assistant_v1.rb +645 -0
  25. data/test/integration/test_discovery_v1.rb +200 -0
  26. data/test/integration/test_iam_assistant_v1.rb +707 -0
  27. data/test/integration/test_language_translator_v3.rb +81 -0
  28. data/test/integration/test_natural_language_classifier_v1.rb +69 -0
  29. data/test/integration/test_natural_language_understanding_v1.rb +98 -0
  30. data/test/integration/test_personality_insights_v3.rb +95 -0
  31. data/test/integration/test_speech_to_text_v1.rb +187 -0
  32. data/test/integration/test_text_to_speech_v1.rb +81 -0
  33. data/test/integration/test_tone_analyzer_v3.rb +72 -0
  34. data/test/integration/test_visual_recognition_v3.rb +64 -0
  35. data/test/test_helper.rb +22 -0
  36. data/test/unit/test_assistant_v1.rb +1598 -0
  37. data/test/unit/test_discovery_v1.rb +1144 -0
  38. data/test/unit/test_iam_token_manager.rb +165 -0
  39. data/test/unit/test_language_translator_v3.rb +461 -0
  40. data/test/unit/test_natural_language_classifier_v1.rb +187 -0
  41. data/test/unit/test_natural_language_understanding_v1.rb +132 -0
  42. data/test/unit/test_personality_insights_v3.rb +172 -0
  43. data/test/unit/test_speech_to_text_v1.rb +755 -0
  44. data/test/unit/test_text_to_speech_v1.rb +336 -0
  45. data/test/unit/test_tone_analyzer_v3.rb +200 -0
  46. data/test/unit/test_vcap_using_personality_insights.rb +150 -0
  47. data/test/unit/test_visual_recognition_v3.rb +345 -0
  48. metadata +302 -0
@@ -0,0 +1,336 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("json")
4
+ require_relative("./../test_helper.rb")
5
+ require("webmock/minitest")
6
+
7
+ WebMock.disable_net_connect!(allow_localhost: true)
8
+
9
+ # Unit tests for the Text to Speech V1 Service
10
+ class TextToSpeechV1Test < Minitest::Test
11
+ def test_success
12
+ voices_response = {
13
+ "voices" => [
14
+ {
15
+ "url" => "https://stream.watsonplatform.net/text-to-speech/api/v1/voices/VoiceEnUsLisa",
16
+ "gender" => "female",
17
+ "name" => "VoiceEnUsLisa",
18
+ "language" => "en-US"
19
+ }, {
20
+ "url" => "https://stream.watsonplatform.net/text-to-speech/api/v1/voices/VoiceEsEsEnrique",
21
+ "gender" => "male",
22
+ "name" => "VoiceEsEsEnrique",
23
+ "language" => "es-ES"
24
+ }, {
25
+ "url" => "https://stream.watsonplatform.net/text-to-speech/api/v1/voices/VoiceEnUsMichael",
26
+ "gender" => "male",
27
+ "name" => "VoiceEnUsMichael",
28
+ "language" => "en-US"
29
+ }, {
30
+ "url" => "https://stream.watsonplatform.net/text-to-speech/api/v1/voices/VoiceEnUsAllison",
31
+ "gender" => "female",
32
+ "name" => "VoiceEnUsAllison",
33
+ "language" => "en-US"
34
+ }
35
+ ]
36
+ }
37
+ voice_response = {
38
+ "url" => "https://stream.watsonplatform.net/text-to-speech/api/v1/voices/en-US_AllisonVoice",
39
+ "name" => "en-US_AllisonVoice",
40
+ "language" => "en-US",
41
+ "customizable" => true,
42
+ "gender" => "female",
43
+ "description" => "Allison: American English female voice.",
44
+ "supported_features" => {
45
+ "custom_pronunciation" => true,
46
+ "voice_transformation" => true
47
+ }
48
+ }
49
+ synthesize_response_body = "<binary response>"
50
+ stub_request(:get, "https://stream.watsonplatform.net/text-to-speech/api/v1/voices")
51
+ .with(
52
+ headers: {
53
+ "Accept" => "application/json",
54
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
55
+ "Host" => "stream.watsonplatform.net"
56
+ }
57
+ ).to_return(status: 200, body: voices_response.to_json, headers: { "Content-Type" => "application/json" })
58
+ service = IBMWatson::TextToSpeechV1.new(
59
+ username: "username",
60
+ password: "password"
61
+ )
62
+ service_response = service.list_voices
63
+ assert_equal(voices_response, service_response.result)
64
+
65
+ stub_request(:get, "https://stream.watsonplatform.net/text-to-speech/api/v1/voices/en-us_AllisonVoice")
66
+ .with(
67
+ headers: {
68
+ "Accept" => "application/json",
69
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
70
+ "Host" => "stream.watsonplatform.net"
71
+ }
72
+ ).to_return(status: 200, body: voice_response.to_json, headers: { "Content-Type" => "application/json" })
73
+ service_response = service.get_voice(
74
+ voice: "en-us_AllisonVoice"
75
+ )
76
+ assert_equal(voice_response, service_response.result)
77
+
78
+ stub_request(:post, "https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize")
79
+ .with(
80
+ body: "{\"text\":\"hello\"}",
81
+ headers: {
82
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
83
+ "Content-Type" => "application/json",
84
+ "Host" => "stream.watsonplatform.net"
85
+ }
86
+ ).to_return(status: 200, body: synthesize_response_body, headers: {})
87
+ service_response = service.synthesize(
88
+ text: "hello"
89
+ )
90
+ assert_equal(synthesize_response_body, service_response.result)
91
+ end
92
+
93
+ def test_get_pronunciation
94
+ response = {
95
+ "pronunciation" => "pronunciation info"
96
+ }
97
+ service = IBMWatson::TextToSpeechV1.new(
98
+ username: "username",
99
+ password: "password"
100
+ )
101
+ stub_request(:get, "https://stream.watsonplatform.net/text-to-speech/api/v1/pronunciation?text=this%20is%20some%20text")
102
+ .with(
103
+ headers: {
104
+ "Accept" => "application/json",
105
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
106
+ "Host" => "stream.watsonplatform.net"
107
+ }
108
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
109
+ service_response = service.get_pronunciation(
110
+ text: "this is some text"
111
+ )
112
+ assert_equal(response, service_response.result)
113
+
114
+ stub_request(:get, "https://stream.watsonplatform.net/text-to-speech/api/v1/pronunciation?text=yo&voice=VoiceEnUsLisa")
115
+ .with(
116
+ headers: {
117
+ "Accept" => "application/json",
118
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
119
+ "Host" => "stream.watsonplatform.net"
120
+ }
121
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
122
+ service_response = service.get_pronunciation(
123
+ text: "yo",
124
+ voice: "VoiceEnUsLisa"
125
+ )
126
+ assert_equal(response, service_response.result)
127
+
128
+ stub_request(:get, "https://stream.watsonplatform.net/text-to-speech/api/v1/pronunciation?format=ipa&text=yo&voice=VoiceEnUsLisa")
129
+ .with(
130
+ headers: {
131
+ "Accept" => "application/json",
132
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
133
+ "Host" => "stream.watsonplatform.net"
134
+ }
135
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
136
+ service_response = service.get_pronunciation(
137
+ text: "yo",
138
+ voice: "VoiceEnUsLisa",
139
+ format: "ipa"
140
+ )
141
+ assert_equal(response, service_response.result)
142
+ end
143
+
144
+ def test_custom_voice_models
145
+ response = { "customizations" => "yep" }
146
+ service = IBMWatson::TextToSpeechV1.new(
147
+ username: "username",
148
+ password: "password"
149
+ )
150
+ stub_request(:get, "https://stream.watsonplatform.net/text-to-speech/api/v1/customizations")
151
+ .with(
152
+ headers: {
153
+ "Accept" => "application/json",
154
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
155
+ "Host" => "stream.watsonplatform.net"
156
+ }
157
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
158
+ service_response = service.list_voice_models
159
+ assert_equal(response, service_response.result)
160
+
161
+ stub_request(:get, "https://stream.watsonplatform.net/text-to-speech/api/v1/customizations?language=en-US")
162
+ .with(
163
+ headers: {
164
+ "Accept" => "application/json",
165
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
166
+ "Host" => "stream.watsonplatform.net"
167
+ }
168
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
169
+ service_response = service.list_voice_models(
170
+ language: "en-US"
171
+ )
172
+ assert_equal(response, service_response.result)
173
+
174
+ stub_request(:post, "https://stream.watsonplatform.net/text-to-speech/api/v1/customizations")
175
+ .with(
176
+ body: "{\"name\":\"name\",\"description\":\"description\"}",
177
+ headers: {
178
+ "Accept" => "application/json",
179
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
180
+ "Content-Type" => "application/json",
181
+ "Host" => "stream.watsonplatform.net"
182
+ }
183
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
184
+ service_response = service.create_voice_model(
185
+ name: "name",
186
+ description: "description"
187
+ )
188
+ assert_equal(response, service_response.result)
189
+
190
+ stub_request(:get, "https://stream.watsonplatform.net/text-to-speech/api/v1/customizations/custid")
191
+ .with(
192
+ headers: {
193
+ "Accept" => "application/json",
194
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
195
+ "Host" => "stream.watsonplatform.net"
196
+ }
197
+ ).to_return(status: 200, body: { "customization" => "yep, just one" }.to_json, headers: { "Content-Type" => "application/json" })
198
+ service_response = service.get_voice_model(
199
+ customization_id: "custid"
200
+ )
201
+ assert_equal({ "customization" => "yep, just one" }, service_response.result)
202
+
203
+ stub_request(:post, "https://stream.watsonplatform.net/text-to-speech/api/v1/customizations/custid")
204
+ .with(
205
+ body: "{\"name\":\"name\",\"description\":\"description\"}",
206
+ headers: {
207
+ "Accept" => "application/json",
208
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
209
+ "Content-Type" => "application/json",
210
+ "Host" => "stream.watsonplatform.net"
211
+ }
212
+ ).to_return(status: 200, body: "", headers: {})
213
+ service_response = service.update_voice_model(
214
+ customization_id: "custid",
215
+ name: "name",
216
+ description: "description"
217
+ )
218
+ assert(service_response.nil?)
219
+
220
+ stub_request(:delete, "https://stream.watsonplatform.net/text-to-speech/api/v1/customizations/custid")
221
+ .with(
222
+ headers: {
223
+ "Accept" => "application/json",
224
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
225
+ "Host" => "stream.watsonplatform.net"
226
+ }
227
+ ).to_return(status: 200, body: "", headers: {})
228
+ service_response = service.delete_voice_model(
229
+ customization_id: "custid"
230
+ )
231
+ assert(service_response.nil?)
232
+ end
233
+
234
+ def test_custom_words
235
+ response = { "customizations" => "yep" }
236
+ service = IBMWatson::TextToSpeechV1.new(
237
+ username: "username",
238
+ password: "password"
239
+ )
240
+ stub_request(:get, "https://stream.watsonplatform.net/text-to-speech/api/v1/customizations/custid/words")
241
+ .with(
242
+ headers: {
243
+ "Accept" => "application/json",
244
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
245
+ "Host" => "stream.watsonplatform.net"
246
+ }
247
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
248
+ service_response = service.list_words(
249
+ customization_id: "custid"
250
+ )
251
+ assert_equal(response, service_response.result)
252
+
253
+ stub_request(:post, "https://stream.watsonplatform.net/text-to-speech/api/v1/customizations/custid/words")
254
+ .with(
255
+ body: "{\"words\":[{\"word\":\"one\",\"translation\":\"one\"},{\"word\":\"two\",\"translation\":\"two\"}]}",
256
+ headers: {
257
+ "Accept" => "application/json",
258
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
259
+ "Content-Type" => "application/json",
260
+ "Host" => "stream.watsonplatform.net"
261
+ }
262
+ ).to_return(status: 200, body: "", headers: { "Content-Type" => "application/json" })
263
+ service_response = service.add_words(
264
+ customization_id: "custid",
265
+ words: [
266
+ { "word" => "one", "translation" => "one" },
267
+ { "word" => "two", "translation" => "two" }
268
+ ]
269
+ )
270
+ assert(service_response.nil?)
271
+
272
+ stub_request(:get, "https://stream.watsonplatform.net/text-to-speech/api/v1/customizations/custid/words/word")
273
+ .with(
274
+ headers: {
275
+ "Accept" => "application/json",
276
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
277
+ "Host" => "stream.watsonplatform.net"
278
+ }
279
+ ).to_return(status: 200, body: { "customization" => "yep, just one" }.to_json, headers: { "Content-Type" => "application/json" })
280
+ service_response = service.get_word(
281
+ customization_id: "custid",
282
+ word: "word"
283
+ )
284
+ assert_equal({ "customization" => "yep, just one" }, service_response.result)
285
+
286
+ stub_request(:put, "https://stream.watsonplatform.net/text-to-speech/api/v1/customizations/custid/words/word")
287
+ .with(
288
+ body: "{\"translation\":\"I'm translated\"}",
289
+ headers: {
290
+ "Accept" => "application/json",
291
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
292
+ "Content-Type" => "application/json",
293
+ "Host" => "stream.watsonplatform.net"
294
+ }
295
+ ).to_return(status: 200, body: "", headers: { "Content-Type" => "application/json" })
296
+ service_response = service.add_word(
297
+ customization_id: "custid",
298
+ word: "word",
299
+ translation: "I'm translated"
300
+ )
301
+ assert(service_response.nil?)
302
+
303
+ stub_request(:delete, "https://stream.watsonplatform.net/text-to-speech/api/v1/customizations/custid/words/word")
304
+ .with(
305
+ headers: {
306
+ "Accept" => "application/json",
307
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
308
+ "Host" => "stream.watsonplatform.net"
309
+ }
310
+ ).to_return(status: 200, body: "", headers: { "Content-Type" => "application/json" })
311
+ service_response = service.delete_word(
312
+ customization_id: "custid",
313
+ word: "word"
314
+ )
315
+ assert(service_response.nil?)
316
+ end
317
+
318
+ def test_delete_user_data
319
+ service = IBMWatson::TextToSpeechV1.new(
320
+ username: "username",
321
+ password: "password"
322
+ )
323
+ stub_request(:delete, "https://stream.watsonplatform.net/text-to-speech/api/v1/user_data?customer_id=id")
324
+ .with(
325
+ headers: {
326
+ "Accept" => "application/json",
327
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
328
+ "Host" => "stream.watsonplatform.net"
329
+ }
330
+ ).to_return(status: 200, body: "", headers: {})
331
+ service_response = service.delete_user_data(
332
+ customer_id: "id"
333
+ )
334
+ assert(service_response.nil?)
335
+ end
336
+ end
@@ -0,0 +1,200 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative("./../../lib/ibm_watson/watson_api_exception.rb")
4
+ require("json")
5
+ require_relative("./../test_helper.rb")
6
+ require("webmock/minitest")
7
+
8
+ WebMock.disable_net_connect!(allow_localhost: true)
9
+
10
+ # Unit tests for the Tone Analyzer V3 Service
11
+ class ToneAnalyzerV3Test < Minitest::Test
12
+ def test_tone
13
+ tone_response = JSON.parse(File.read(Dir.getwd + "/resources/tone-v3-expect1.json"))
14
+ headers = {
15
+ "Content-Type" => "application/json"
16
+ }
17
+ expected_response = DetailedResponse.new(status: 200, headers: headers, body: tone_response)
18
+ tone_text = File.read(Dir.getwd + "/resources/personality.txt")
19
+ stub_request(:post, "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21")
20
+ .with(
21
+ body: tone_text,
22
+ headers: {
23
+ "Accept" => "application/json",
24
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
25
+ "Content-Type" => "application/json",
26
+ "Host" => "gateway.watsonplatform.net"
27
+ }
28
+ ).to_return(status: 200, body: tone_response.to_json, headers: headers)
29
+ service = IBMWatson::ToneAnalyzerV3.new(
30
+ version: "2017-09-21",
31
+ username: "username",
32
+ password: "password"
33
+ )
34
+ service_response = service.tone(tone_input: tone_text, content_type: "application/json")
35
+ assert_equal(expected_response.status, service_response.status)
36
+ assert_equal(expected_response.result, service_response.result)
37
+ expected_response.headers.each_key do |key|
38
+ assert(service_response.headers.key?(key))
39
+ assert(expected_response.headers[key] == service_response.headers[key])
40
+ end
41
+ end
42
+
43
+ def test_tone_with_args
44
+ tone_response = JSON.parse(File.read(Dir.getwd + "/resources/tone-v3-expect1.json"))
45
+ headers = {
46
+ "Content-Type" => "application/json"
47
+ }
48
+ tone_text = File.read(Dir.getwd + "/resources/personality.txt")
49
+ expected_response = DetailedResponse.new(status: 200, headers: headers, body: tone_response)
50
+ stub_request(:post, "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?sentences=false&version=2017-09-21")
51
+ .with(
52
+ body: tone_text,
53
+ headers: {
54
+ "Accept" => "application/json",
55
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
56
+ "Content-Type" => "application/json",
57
+ "Host" => "gateway.watsonplatform.net"
58
+ }
59
+ ).to_return(status: 200, body: tone_response.to_json, headers: headers)
60
+ service = IBMWatson::ToneAnalyzerV3.new(
61
+ version: "2017-09-21",
62
+ username: "username",
63
+ password: "password"
64
+ )
65
+ service_response = service.tone(tone_input: tone_text, content_type: "application/json", sentences: false)
66
+ assert_equal(expected_response.status, service_response.status)
67
+ assert_equal(expected_response.result, service_response.result)
68
+ expected_response.headers.each_key do |key|
69
+ assert(service_response.headers.key?(key))
70
+ assert(expected_response.headers[key] == service_response.headers[key])
71
+ end
72
+ end
73
+
74
+ def test_tone_chat
75
+ tone_response = JSON.parse(File.read(Dir.getwd + "/resources/tone-v3-expect2.json"))
76
+ headers = {
77
+ "Content-Type" => "application/json"
78
+ }
79
+ expected_response = DetailedResponse.new(body: tone_response, status: 200, headers: headers)
80
+ stub_request(:post, "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone_chat?version=2017-09-21")
81
+ .with(
82
+ body: "{\"utterances\":[{\"text\":\"I am very happy\",\"user\":\"glenn\"}]}",
83
+ headers: {
84
+ "Accept" => "application/json",
85
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
86
+ "Content-Type" => "application/json",
87
+ "Host" => "gateway.watsonplatform.net"
88
+ }
89
+ ).to_return(status: 200, body: tone_response.to_json, headers: headers)
90
+ service = IBMWatson::ToneAnalyzerV3.new(
91
+ version: "2017-09-21",
92
+ username: "username",
93
+ password: "password"
94
+ )
95
+ utterances = [
96
+ {
97
+ "text" => "I am very happy",
98
+ "user" => "glenn"
99
+ }
100
+ ]
101
+ service_response = service.tone_chat(utterances: utterances)
102
+ assert_equal(expected_response.status, service_response.status)
103
+ assert_equal(expected_response.result, service_response.result)
104
+ expected_response.headers.each_key do |key|
105
+ assert(service_response.headers.key?(key))
106
+ assert(expected_response.headers[key] == service_response.headers[key])
107
+ end
108
+ end
109
+
110
+ def test_error
111
+ error_code = 400
112
+ error_message = "Invalid Json at line 2, column 12"
113
+ headers = {
114
+ "Content-Type" => "application/json"
115
+ }
116
+ tone_response = {
117
+ "code" => error_code,
118
+ "sub_code" => "C00012",
119
+ "error" => error_message
120
+ }
121
+ text = "Team, I know that times are tough!"
122
+ stub_request(:post, "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21")
123
+ .with(
124
+ body: text,
125
+ headers: {
126
+ "Accept" => "application/json",
127
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
128
+ "Content-Type" => "application/json",
129
+ "Host" => "gateway.watsonplatform.net"
130
+ }
131
+ ).to_return(status: 400, body: tone_response.to_json, headers: headers)
132
+ service = IBMWatson::ToneAnalyzerV3.new(
133
+ version: "2017-09-21",
134
+ username: "username",
135
+ password: "password"
136
+ )
137
+ begin
138
+ service.tone(tone_input: text, content_type: "application/json")
139
+ rescue WatsonApiException => e
140
+ assert_equal(error_code, e.code)
141
+ assert_equal(error_message, e.error)
142
+ assert_equal("C00012", e.info["sub_code"])
143
+ end
144
+ end
145
+
146
+ # Test to ensure that custom headers are sent
147
+ def test_tone_with_custom_headers
148
+ tone_response = JSON.parse(File.read(Dir.getwd + "/resources/tone-v3-expect1.json"))
149
+ headers = {
150
+ "Content-Type" => "application/json"
151
+ }
152
+ tone_text = File.read(Dir.getwd + "/resources/personality.txt")
153
+ stub_request(:post, "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21")
154
+ .with(
155
+ body: tone_text,
156
+ headers: {
157
+ "Accept" => "application/json",
158
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
159
+ "Content-Type" => "Custom/Type",
160
+ "Host" => "gateway.watsonplatform.net",
161
+ "Custom-Header-One" => "yes"
162
+ }
163
+ ).to_return(status: 200, body: tone_response.to_json, headers: headers)
164
+ service = IBMWatson::ToneAnalyzerV3.new(
165
+ version: "2017-09-21",
166
+ username: "username",
167
+ password: "password"
168
+ )
169
+ service_response = service.headers(
170
+ "Custom-Header-One" => "yes",
171
+ "Content-Type" => "Custom/Type"
172
+ ).tone(tone_input: tone_text, content_type: "application/json")
173
+ assert_equal(tone_response, service_response.result)
174
+ end
175
+
176
+ def test_tone_with_application_json
177
+ tone_response = JSON.parse(File.read(Dir.getwd + "/resources/tone-v3-expect1.json"))
178
+ headers = {
179
+ "Content-Type" => "application/json"
180
+ }
181
+ tone_text = { "text" => "This is the text to be analyzed" }
182
+ stub_request(:post, "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21")
183
+ .with(
184
+ body: tone_text,
185
+ headers: {
186
+ "Accept" => "application/json",
187
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
188
+ "Content-Type" => "application/json",
189
+ "Host" => "gateway.watsonplatform.net"
190
+ }
191
+ ).to_return(status: 200, body: tone_response.to_json, headers: headers)
192
+ service = IBMWatson::ToneAnalyzerV3.new(
193
+ version: "2017-09-21",
194
+ username: "username",
195
+ password: "password"
196
+ )
197
+ service_response = service.tone(tone_input: tone_text, content_type: "application/json")
198
+ assert_equal(tone_response, service_response.result)
199
+ end
200
+ end