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,165 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative("./../../lib/ibm_watson/iam_token_manager.rb")
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 IAM Token Manager
10
+ class IAMTokenManagerTest < Minitest::Test
11
+ def test_request_token
12
+ iam_url = "https://iam.bluemix.net/identity/token"
13
+ response = {
14
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
15
+ "token_type" => "Bearer",
16
+ "expires_in" => 3600,
17
+ "expiration" => 1_524_167_011,
18
+ "refresh_token" => "jy4gl91BQ"
19
+ }
20
+
21
+ token_manager = IAMTokenManager.new(
22
+ iam_api_key: "iam_api_key",
23
+ iam_access_token: "iam_access_token",
24
+ iam_url: iam_url
25
+ )
26
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
27
+ .with(
28
+ body: { "apikey" => "iam_api_key", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
29
+ headers: {
30
+ "Accept" => "application/json",
31
+ "Authorization" => "Basic Yng6Yng=",
32
+ "Content-Type" => "application/x-www-form-urlencoded",
33
+ "Host" => "iam.bluemix.net"
34
+ }
35
+ ).to_return(status: 200, body: response.to_json, headers: {})
36
+ token_response = token_manager._request_token
37
+ assert_equal(response, token_response)
38
+ end
39
+
40
+ def test_refresh_token
41
+ iam_url = "https://iam.bluemix.net/identity/token"
42
+ response = {
43
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
44
+ "token_type" => "Bearer",
45
+ "expires_in" => 3600,
46
+ "expiration" => 1_524_167_011,
47
+ "refresh_token" => "jy4gl91BQ"
48
+ }
49
+ token_manager = IAMTokenManager.new(
50
+ iam_api_key: "iam_api_key",
51
+ iam_access_token: "iam_access_token",
52
+ iam_url: iam_url
53
+ )
54
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
55
+ .with(
56
+ body: { "grant_type" => "refresh_token", "refresh_token" => "" },
57
+ headers: {
58
+ "Accept" => "application/json",
59
+ "Authorization" => "Basic Yng6Yng=",
60
+ "Content-Type" => "application/x-www-form-urlencoded",
61
+ "Host" => "iam.bluemix.net"
62
+ }
63
+ ).to_return(status: 200, body: response.to_json, headers: {})
64
+ token_response = token_manager._refresh_token
65
+ assert_equal(response, token_response)
66
+ end
67
+
68
+ def test_is_token_expired
69
+ token_manager = IAMTokenManager.new(
70
+ iam_api_key: "iam_api_key",
71
+ iam_access_token: "iam_access_token",
72
+ iam_url: "iam_url"
73
+ )
74
+ token_manager.token_info = {
75
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
76
+ "token_type" => "Bearer",
77
+ "expires_in" => 3600,
78
+ "expiration" => Time.now.to_i + 6000,
79
+ "refresh_token" => "jy4gl91BQ"
80
+ }
81
+
82
+ refute(token_manager._is_token_expired?)
83
+ token_manager.token_info["expiration"] = Time.now.to_i - 3600
84
+ assert(token_manager._is_token_expired?)
85
+ end
86
+
87
+ def test_is_refresh_token_expired
88
+ token_manager = IAMTokenManager.new(
89
+ iam_api_key: "iam_api_key",
90
+ iam_access_token: "iam_access_token",
91
+ iam_url: "iam_url"
92
+ )
93
+ token_manager.token_info = {
94
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
95
+ "token_type" => "Bearer",
96
+ "expires_in" => 3600,
97
+ "expiration" => Time.now.to_i,
98
+ "refresh_token" => "jy4gl91BQ"
99
+ }
100
+
101
+ refute(token_manager._is_refresh_token_expired?)
102
+ token_manager.token_info["expiration"] = Time.now.to_i - (8 * 24 * 3600)
103
+ assert(token_manager._is_token_expired?)
104
+ end
105
+
106
+ def test_get_token
107
+ iam_url = "https://iam.bluemix.net/identity/token"
108
+ token_manager = IAMTokenManager.new(
109
+ iam_api_key: "iam_api_key",
110
+ iam_url: iam_url
111
+ )
112
+ token_manager.user_access_token = "user_access_token"
113
+
114
+ token = token_manager._token
115
+ assert_equal(token_manager.user_access_token, token)
116
+
117
+ response = {
118
+ "access_token" => "hellohello",
119
+ "token_type" => "Bearer",
120
+ "expires_in" => 3600,
121
+ "expiration" => 1_524_167_011,
122
+ "refresh_token" => "jy4gl91BQ"
123
+ }
124
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
125
+ .with(
126
+ body: { "apikey" => "iam_api_key", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
127
+ headers: {
128
+ "Accept" => "application/json",
129
+ "Authorization" => "Basic Yng6Yng=",
130
+ "Content-Type" => "application/x-www-form-urlencoded",
131
+ "Host" => "iam.bluemix.net"
132
+ }
133
+ ).to_return(status: 200, body: response.to_json, headers: {})
134
+ token_manager.user_access_token = ""
135
+ token = token_manager._token
136
+ assert_equal("hellohello", token)
137
+
138
+ token_manager.token_info["expiration"] = Time.now.to_i - (20 * 24 * 3600)
139
+ token = token_manager._token
140
+ assert_equal("hellohello", token)
141
+
142
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
143
+ .with(
144
+ headers: {
145
+ "Accept" => "application/json",
146
+ "Authorization" => "Basic Yng6Yng=",
147
+ "Content-Type" => "application/x-www-form-urlencoded",
148
+ "Host" => "iam.bluemix.net"
149
+ }
150
+ ).to_return(status: 200, body: response.to_json, headers: {})
151
+ token_manager.token_info["expiration"] = Time.now.to_i - 4000
152
+ token = token_manager._token
153
+ assert_equal("hellohello", token)
154
+
155
+ token_manager.token_info = {
156
+ "access_token" => "dummy",
157
+ "token_type" => "Bearer",
158
+ "expires_in" => 3600,
159
+ "expiration" => Time.now.to_i + 3600,
160
+ "refresh_token" => "jy4gl91BQ"
161
+ }
162
+ token = token_manager._token
163
+ assert_equal("dummy", token)
164
+ end
165
+ end
@@ -0,0 +1,461 @@
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 Language Translator V3 Service
10
+ class LanguageTranslatorV3Test < Minitest::Test
11
+ def test_translate_source_target
12
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
13
+ .with(
14
+ body: { "apikey" => "iam_api_key", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
15
+ headers: {
16
+ "Accept" => "application/json",
17
+ "Authorization" => "Basic Yng6Yng=",
18
+ "Content-Type" => "application/x-www-form-urlencoded",
19
+ "Host" => "iam.bluemix.net"
20
+ }
21
+ ).to_return(
22
+ status: 200,
23
+ body: {
24
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
25
+ "token_type" => "Bearer",
26
+ "expires_in" => 3600,
27
+ "expiration" => 1_524_167_011,
28
+ "refresh_token" => "jy4gl91BQ"
29
+ }.to_json,
30
+ headers: {}
31
+ )
32
+ service = IBMWatson::LanguageTranslatorV3.new(
33
+ version: "2018-05-01",
34
+ iam_api_key: "iam_api_key"
35
+ )
36
+ expected = {
37
+ "character_count" => 19,
38
+ "translations" => [{ "translation" => "Hello, how are you ? \u20ac" }],
39
+ "word_count" => 4
40
+ }
41
+ stub_request(:post, "https://gateway.watsonplatform.net/language-translator/api/v3/translate?version=2018-05-01")
42
+ .with(
43
+ body: "{\"text\":\"Hola, cómo estás? €\",\"source\":\"es\",\"target\":\"en\"}",
44
+ headers: {
45
+ "Accept" => "application/json",
46
+ "Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
47
+ "Content-Type" => "application/json",
48
+ "Host" => "gateway.watsonplatform.net"
49
+ }
50
+ ).to_return(status: 200, body: expected.to_json, headers: { "Content-Type" => "application/json" })
51
+ service_response = service.translate(
52
+ text: "Hola, cómo estás? €",
53
+ source: "es",
54
+ target: "en"
55
+ )
56
+ assert_equal(expected, service_response.result)
57
+ end
58
+
59
+ def test_translate_model_id
60
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
61
+ .with(
62
+ body: { "apikey" => "iam_api_key", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
63
+ headers: {
64
+ "Accept" => "application/json",
65
+ "Authorization" => "Basic Yng6Yng=",
66
+ "Content-Type" => "application/x-www-form-urlencoded",
67
+ "Host" => "iam.bluemix.net"
68
+ }
69
+ ).to_return(
70
+ status: 200,
71
+ body: {
72
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
73
+ "token_type" => "Bearer",
74
+ "expires_in" => 3600,
75
+ "expiration" => 1_524_167_011,
76
+ "refresh_token" => "jy4gl91BQ"
77
+ }.to_json,
78
+ headers: {}
79
+ )
80
+ service = IBMWatson::LanguageTranslatorV3.new(
81
+ version: "2018-05-01",
82
+ iam_api_key: "iam_api_key"
83
+ )
84
+ expected = {
85
+ "character_count" => 22,
86
+ "translations" => [
87
+ {
88
+ "translation" => "Messi es el mejor"
89
+ }
90
+ ],
91
+ "word_count" => 5
92
+ }
93
+ stub_request(:post, "https://gateway.watsonplatform.net/language-translator/api/v3/translate?version=2018-05-01")
94
+ .with(
95
+ body: "{\"text\":\"Messi is the best ever\",\"model_id\":\"en-es-conversational\"}",
96
+ headers: {
97
+ "Accept" => "application/json",
98
+ "Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
99
+ "Content-Type" => "application/json",
100
+ "Host" => "gateway.watsonplatform.net"
101
+ }
102
+ ).to_return(status: 200, body: expected.to_json, headers: { "Content-Type" => "application/json" })
103
+ service_response = service.translate(
104
+ text: "Messi is the best ever",
105
+ model_id: "en-es-conversational"
106
+ )
107
+ assert_equal(expected, service_response.result)
108
+ end
109
+
110
+ def test_identify
111
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
112
+ .with(
113
+ body: { "apikey" => "iam_api_key", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
114
+ headers: {
115
+ "Accept" => "application/json",
116
+ "Authorization" => "Basic Yng6Yng=",
117
+ "Content-Type" => "application/x-www-form-urlencoded",
118
+ "Host" => "iam.bluemix.net"
119
+ }
120
+ ).to_return(
121
+ status: 200,
122
+ body: {
123
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
124
+ "token_type" => "Bearer",
125
+ "expires_in" => 3600,
126
+ "expiration" => 1_524_167_011,
127
+ "refresh_token" => "jy4gl91BQ"
128
+ }.to_json,
129
+ headers: {}
130
+ )
131
+ service = IBMWatson::LanguageTranslatorV3.new(
132
+ version: "2018-05-01",
133
+ iam_api_key: "iam_api_key"
134
+ )
135
+ expected = {
136
+ "languages" => [
137
+ {
138
+ "confidence" => 0.477673,
139
+ "language" => "zh"
140
+ },
141
+ {
142
+ "confidence" => 0.262053,
143
+ "language" => "zh-TW"
144
+ },
145
+ {
146
+ "confidence" => 0.00958378,
147
+ "language" => "en"
148
+ }
149
+ ]
150
+ }
151
+ stub_request(:post, "https://gateway.watsonplatform.net/language-translator/api/v3/identify?version=2018-05-01")
152
+ .with(
153
+ body: "祝你有美好的一天",
154
+ headers: {
155
+ "Accept" => "application/json",
156
+ "Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
157
+ "Content-Type" => "text/plain",
158
+ "Host" => "gateway.watsonplatform.net"
159
+ }
160
+ ).to_return(status: 200, body: expected.to_json, headers: { "Content-Type" => "application/json" })
161
+ service_response = service.identify(
162
+ text: "祝你有美好的一天"
163
+ )
164
+ assert_equal(expected, service_response.result)
165
+ end
166
+
167
+ def test_list_identifiable_languages
168
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
169
+ .with(
170
+ body: { "apikey" => "iam_api_key", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
171
+ headers: {
172
+ "Accept" => "application/json",
173
+ "Authorization" => "Basic Yng6Yng=",
174
+ "Content-Type" => "application/x-www-form-urlencoded",
175
+ "Host" => "iam.bluemix.net"
176
+ }
177
+ ).to_return(
178
+ status: 200,
179
+ body: {
180
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
181
+ "token_type" => "Bearer",
182
+ "expires_in" => 3600,
183
+ "expiration" => 1_524_167_011,
184
+ "refresh_token" => "jy4gl91BQ"
185
+ }.to_json,
186
+ headers: {}
187
+ )
188
+ service = IBMWatson::LanguageTranslatorV3.new(
189
+ version: "2018-05-01",
190
+ iam_api_key: "iam_api_key"
191
+ )
192
+ expected = {
193
+ "languages" => [
194
+ {
195
+ "name" => "German",
196
+ "language" => "de"
197
+ },
198
+ {
199
+ "name" => "Greek",
200
+ "language" => "el"
201
+ },
202
+ {
203
+ "name" => "English",
204
+ "language" => "en"
205
+ },
206
+ {
207
+ "name" => "Esperanto",
208
+ "language" => "eo"
209
+ },
210
+ {
211
+ "name" => "Spanish",
212
+ "language" => "es"
213
+ },
214
+ {
215
+ "name" => "Chinese",
216
+ "language" => "zh"
217
+ }
218
+ ]
219
+ }
220
+ stub_request(:get, "https://gateway.watsonplatform.net/language-translator/api/v3/identifiable_languages?version=2018-05-01")
221
+ .with(
222
+ headers: {
223
+ "Accept" => "application/json",
224
+ "Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
225
+ "Host" => "gateway.watsonplatform.net"
226
+ }
227
+ ).to_return(status: 200, body: expected.to_json, headers: { "Content-Type" => "application/json" })
228
+ service_response = service.list_identifiable_languages
229
+ assert_equal(expected, service_response.result)
230
+ end
231
+
232
+ def test_create_model
233
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
234
+ .with(
235
+ body: { "apikey" => "iam_api_key", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
236
+ headers: {
237
+ "Accept" => "application/json",
238
+ "Authorization" => "Basic Yng6Yng=",
239
+ "Content-Type" => "application/x-www-form-urlencoded",
240
+ "Host" => "iam.bluemix.net"
241
+ }
242
+ ).to_return(
243
+ status: 200,
244
+ body: {
245
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
246
+ "token_type" => "Bearer",
247
+ "expires_in" => 3600,
248
+ "expiration" => 1_524_167_011,
249
+ "refresh_token" => "jy4gl91BQ"
250
+ }.to_json,
251
+ headers: {}
252
+ )
253
+ service = IBMWatson::LanguageTranslatorV3.new(
254
+ version: "2018-05-01",
255
+ iam_api_key: "iam_api_key"
256
+ )
257
+ expected = {
258
+ "status" => "available",
259
+ "model_id" => "en-es-conversational",
260
+ "domain" => "conversational",
261
+ "target" => "es",
262
+ "customizable" => false,
263
+ "source" => "en",
264
+ "base_model_id" => "en-es-conversational",
265
+ "owner" => "",
266
+ "default_model" => false,
267
+ "name" => "test_glossary"
268
+ }
269
+ stub_request(:post, "https://gateway.watsonplatform.net/language-translator/api/v3/models?base_model_id=en-fr&name=test_glossary&version=2018-05-01")
270
+ .with(
271
+ headers: {
272
+ "Accept" => "application/json",
273
+ "Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
274
+ "Host" => "gateway.watsonplatform.net"
275
+ }
276
+ ).to_return(status: 200, body: expected.to_json, headers: { "Content-Type" => "application/json" })
277
+ custom_model = File.open(Dir.getwd + "/resources/language_translator_model.tmx")
278
+ service_response = service.create_model(
279
+ base_model_id: "en-fr",
280
+ name: "test_glossary",
281
+ forced_glossary: custom_model
282
+ )
283
+ assert_equal(expected, service_response.result)
284
+
285
+ service_response = service.create_model(
286
+ base_model_id: "en-fr",
287
+ name: "test_glossary",
288
+ forced_glossary: custom_model.read,
289
+ parallel_corpus: "parallel corpus",
290
+ parallel_corpus_filename: "parallel_corpus_filename"
291
+ )
292
+ assert_equal(expected, service_response.result)
293
+
294
+ service_response = service.create_model(
295
+ base_model_id: "en-fr",
296
+ name: "test_glossary",
297
+ forced_glossary: custom_model,
298
+ forced_glossary_filename: "language_translator_model.tmx",
299
+ parallel_corpus: "parallel corpus"
300
+ )
301
+ assert_equal(expected, service_response.result)
302
+ end
303
+
304
+ def test_delete_model
305
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
306
+ .with(
307
+ body: { "apikey" => "iam_api_key", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
308
+ headers: {
309
+ "Accept" => "application/json",
310
+ "Authorization" => "Basic Yng6Yng=",
311
+ "Content-Type" => "application/x-www-form-urlencoded",
312
+ "Host" => "iam.bluemix.net"
313
+ }
314
+ ).to_return(
315
+ status: 200,
316
+ body: {
317
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
318
+ "token_type" => "Bearer",
319
+ "expires_in" => 3600,
320
+ "expiration" => 1_524_167_011,
321
+ "refresh_token" => "jy4gl91BQ"
322
+ }.to_json,
323
+ headers: {}
324
+ )
325
+ service = IBMWatson::LanguageTranslatorV3.new(
326
+ version: "2018-05-01",
327
+ iam_api_key: "iam_api_key"
328
+ )
329
+ expected = {
330
+ "status" => "OK"
331
+ }
332
+ stub_request(:delete, "https://gateway.watsonplatform.net/language-translator/api/v3/models/en-es-conversational?version=2018-05-01")
333
+ .with(
334
+ headers: {
335
+ "Accept" => "application/json",
336
+ "Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
337
+ "Host" => "gateway.watsonplatform.net"
338
+ }
339
+ ).to_return(status: 200, body: expected.to_json, headers: { "Content-Type" => "application/json" })
340
+ service_response = service.delete_model(
341
+ model_id: "en-es-conversational"
342
+ )
343
+ assert_equal(expected, service_response.result)
344
+ end
345
+
346
+ def test_get_model
347
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
348
+ .with(
349
+ body: { "apikey" => "iam_api_key", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
350
+ headers: {
351
+ "Accept" => "application/json",
352
+ "Authorization" => "Basic Yng6Yng=",
353
+ "Content-Type" => "application/x-www-form-urlencoded",
354
+ "Host" => "iam.bluemix.net"
355
+ }
356
+ ).to_return(
357
+ status: 200,
358
+ body: {
359
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
360
+ "token_type" => "Bearer",
361
+ "expires_in" => 3600,
362
+ "expiration" => 1_524_167_011,
363
+ "refresh_token" => "jy4gl91BQ"
364
+ }.to_json,
365
+ headers: {}
366
+ )
367
+ service = IBMWatson::LanguageTranslatorV3.new(
368
+ version: "2018-05-01",
369
+ iam_api_key: "iam_api_key"
370
+ )
371
+ expected = {
372
+ "status" => "available",
373
+ "model_id" => "en-es-conversational",
374
+ "domain" => "conversational",
375
+ "target" => "es",
376
+ "customizable" => false,
377
+ "source" => "en",
378
+ "base_model_id" => "",
379
+ "owner" => "",
380
+ "default_model" => false,
381
+ "name" => "en-es-conversational"
382
+ }
383
+ stub_request(:get, "https://gateway.watsonplatform.net/language-translator/api/v3/models/en-es-conversational?version=2018-05-01")
384
+ .with(
385
+ headers: {
386
+ "Accept" => "application/json",
387
+ "Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
388
+ "Host" => "gateway.watsonplatform.net"
389
+ }
390
+ ).to_return(status: 200, body: expected.to_json, headers: { "Content-Type" => "application/json" })
391
+ service_response = service.get_model(
392
+ model_id: "en-es-conversational"
393
+ )
394
+ assert_equal(expected, service_response.result)
395
+ end
396
+
397
+ def test_list_models
398
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
399
+ .with(
400
+ body: { "apikey" => "iam_api_key", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
401
+ headers: {
402
+ "Accept" => "application/json",
403
+ "Authorization" => "Basic Yng6Yng=",
404
+ "Content-Type" => "application/x-www-form-urlencoded",
405
+ "Host" => "iam.bluemix.net"
406
+ }
407
+ ).to_return(
408
+ status: 200,
409
+ body: {
410
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
411
+ "token_type" => "Bearer",
412
+ "expires_in" => 3600,
413
+ "expiration" => 1_524_167_011,
414
+ "refresh_token" => "jy4gl91BQ"
415
+ }.to_json,
416
+ headers: {}
417
+ )
418
+ service = IBMWatson::LanguageTranslatorV3.new(
419
+ version: "2018-05-01",
420
+ iam_api_key: "iam_api_key"
421
+ )
422
+ expected = {
423
+ "models" => [
424
+ {
425
+ "status" => "available",
426
+ "model_id" => "en-es-conversational",
427
+ "domain" => "conversational",
428
+ "target" => "es",
429
+ "customizable" => false,
430
+ "source" => "en",
431
+ "base_model_id" => "",
432
+ "owner" => "",
433
+ "default_model" => false,
434
+ "name" => "en-es-conversational"
435
+ },
436
+ {
437
+ "status" => "available",
438
+ "model_id" => "es-en",
439
+ "domain" => "news",
440
+ "target" => "en",
441
+ "customizable" => true,
442
+ "source" => "es",
443
+ "base_model_id" => "",
444
+ "owner" => "",
445
+ "default_model" => true,
446
+ "name" => "es-en"
447
+ }
448
+ ]
449
+ }
450
+ stub_request(:get, "https://gateway.watsonplatform.net/language-translator/api/v3/models?version=2018-05-01")
451
+ .with(
452
+ headers: {
453
+ "Accept" => "application/json",
454
+ "Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
455
+ "Host" => "gateway.watsonplatform.net"
456
+ }
457
+ ).to_return(status: 200, body: expected.to_json, headers: { "Content-Type" => "application/json" })
458
+ service_response = service.list_models
459
+ assert_equal(expected, service_response.result)
460
+ end
461
+ end