watson-api-client 0.0.8 → 0.8.2
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 +4 -4
- data/README.md +39 -78
- data/lib/watson-api-client.rb +17 -63
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 429d03112829eeb061595701c1d7778a96f48d6c
|
4
|
+
data.tar.gz: 622d063ab0f7e23621ad023f895d7432ce4b73fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f92efe8e700d702d06ab4db3ccbc8257de7145e6de1cff2cba8dcbc24eb7bc15ac35986185027710a7c93f799fad30dbb3478a1cac08b6e620916d64a83ec67
|
7
|
+
data.tar.gz: aedcfdef6d3b0313ae55751def88511282250b2a7402d3d3155f7387f608ada7369934e3a35a1c6370cf0395d08a7fba73da96c750b9ce64de56ee4c006f5b91
|
data/README.md
CHANGED
@@ -48,115 +48,76 @@ The watson-api-client is a gem to use REST API on the IBM Watson™ Developer Cl
|
|
48
48
|
To enable these API, you have to do the user registration to the IBM Bluemix™ beforehand, make the services effective, and be relating them to your application.
|
49
49
|
For more information, refer to 'Getting Started' in '[Table of Contents for Services Documentation](http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/)'.
|
50
50
|
|
51
|
-
###
|
51
|
+
###VisualRecognition example
|
52
52
|
|
53
|
-
|
53
|
+
Let's use 'Visual Recognition' service.
|
54
54
|
|
55
|
-
|
55
|
+
service = WatsonAPIClient::VisualRecognition.new(:version=>'2018-03-19', :user=>'apikey', :password=>'......')
|
56
|
+
[
|
57
|
+
service.getDetectFaces('url' => 'https://.....'),
|
58
|
+
service.detectFaces('images_file' => open('.....jpg','rb'))
|
59
|
+
].each do |result|
|
60
|
+
pp JSON.parse(result.body)
|
61
|
+
end
|
56
62
|
|
57
|
-
|
58
|
-
service = WatsonAPIClient::AlchemyLanguage.new(:apikey=>"......",
|
59
|
-
:verify_ssl=>OpenSSL::SSL::VERIFY_NONE)
|
60
|
-
result = service.URLGetTypedRelations('model' => 'en-news', # model: 'en-news',
|
61
|
-
'url' => 'www.cnn.com', # url: 'www.cnn.com',
|
62
|
-
'outputMode' => 'json') # outputMode: 'json')
|
63
|
-
p JSON.parse(result.body)
|
63
|
+
####Generation of the VisualRecognition service object
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
All constructor arguments are passed to the constructor of [RestClient::Resource](http://www.rubydoc.info/gems/rest-client/RestClient/Resource) class except for :apikey, :api_key and :version.
|
65
|
+
First of all, the instance of the VisualRecognition class has to be generated.
|
66
|
+
All constructor arguments are passed to the constructor of [RestClient::Resource](http://www.rubydoc.info/gems/rest-client/RestClient/Resource) class except for :version.
|
68
67
|
Please refer to the document of the rest-client for the details of this hash argument.
|
69
68
|
|
70
|
-
Class name called
|
71
|
-
:
|
72
|
-
Please refer to '[Viewing Bluemix environment variables](
|
69
|
+
Class name called VisualRecognition is the camel case-ized service name of [Watson API Reference](http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/).
|
70
|
+
:password is the 'apikey' picked out from environment variable VCAP_SERVICES.
|
71
|
+
Please refer to '[Viewing Bluemix environment variables](https://console.bluemix.net/docs/services/watson/getting-started-variables.html#vcapServices)' for the details of VCAP_SERVICES.
|
72
|
+
This gem version no longer supports the authentication mechanism for service instances created before May 23, 2018.
|
73
73
|
|
74
|
-
|
75
|
-
In this case, the specification of :apikey is omissible.
|
74
|
+
####Visual recognition using VisualRecognition#getDetectFaces and VisualRecognition#detectFaces
|
76
75
|
|
77
|
-
|
78
|
-
|
79
|
-
How to set the arguments can be seen at Alchemy's [API Reference](https://www.ibm.com/watson/developercloud/alchemy-language/api/v1/#relations).
|
76
|
+
Next, by the 'getDetectFaces' and 'detectFaces' method of the VisualRecognition class, we try to recognize examples.
|
77
|
+
How to set the arguments can be seen at VisualRecognition's [API Reference](https://www.ibm.com/watson/developercloud/visual-recognition/api/v3/curl.html?curl).
|
80
78
|
|
81
79
|
This can be seen by opening the [JSON code of Swagger](https://watson-api-explorer.mybluemix.net/listings/alchemy-language-v1.json).
|
82
80
|
|
83
|
-
The list of the method of the
|
84
|
-
|
85
|
-
p WatsonAPIClient::AlchemyLanguage::API['digest']
|
81
|
+
The list of the method of the VisualRecognition class can be seen even by using the following script.
|
86
82
|
|
87
|
-
|
88
|
-
When converting this JSON string to a hash object using JSON.parse method, the result of URLGetTypedRelations can be used variously by your client programs.
|
83
|
+
p WatsonAPIClient::VisualRecognition::API['digest']
|
89
84
|
|
90
|
-
|
85
|
+
The JSON string is stored in the body of the 'getDetectFaces' and 'detectFaces' method response.
|
86
|
+
When converting this JSON string to a hash object using JSON.parse method, the result of thesemethods can be used variously by your client programs.
|
91
87
|
|
92
|
-
|
88
|
+
###Discovery example
|
93
89
|
|
94
|
-
|
95
|
-
:password=>"yyyyy",
|
96
|
-
:verify_ssl=>OpenSSL::SSL::VERIFY_NONE)
|
97
|
-
result = service.profile(
|
98
|
-
'Content-Type' => "text/plain",
|
99
|
-
'Accept' => "application/json",
|
100
|
-
'Accept-Language' => "en",
|
101
|
-
'Content-Language' => "en",
|
102
|
-
'body' => open('https://raw.githubusercontent.com/suchowan/watson-api-client/master/LICENSE',
|
103
|
-
:ssl_verify_mode=>OpenSSL::SSL::VERIFY_NONE))
|
104
|
-
p JSON.parse(result.body)
|
90
|
+
Last, let's use 'Discovery' service.
|
105
91
|
|
106
|
-
|
107
|
-
The rest-client and the watson-api-client judge which of path, query, header, body each argument is used for automatically.
|
92
|
+
service = WatsonAPIClient::Discovery.new(:version=>'2018-08-01', :user=>".....", :password=>".....")
|
108
93
|
|
109
|
-
|
94
|
+
result = service.listEnvironments()
|
95
|
+
pp JSON.parse(result.body)
|
110
96
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
service.detect_faces('url'=>'https://example.com/example.jpg', :access=>'get'),
|
117
|
-
service.detect_faces_get('url'=>'https://example.com/example.jpg'),
|
118
|
-
service.detect_faces('image_file' => open('face.png','rb')),
|
119
|
-
service.detect_faces('image_file' => open('face.png','rb'), :access=>'post'),
|
120
|
-
service.detect_faces_post('image_file' => open('face.png','rb'))
|
121
|
-
].each do |result|
|
122
|
-
pp JSON.parse(result.body)
|
123
|
-
end
|
97
|
+
result = service.updateEnvironment(
|
98
|
+
'environment_id' => '.......',
|
99
|
+
'body' => JSON.generate({'name' => 'Tutorial', 'description' => 'description of Tutorial'})
|
100
|
+
)
|
101
|
+
pp JSON.parse(result.body)
|
124
102
|
|
125
|
-
|
126
|
-
|
127
|
-
The 'detect_faces' method comes to work in both 'get' access and 'post' access.
|
128
|
-
When being ambiguous, it's judged by the kind of designated parameters automatically.
|
103
|
+
If the server application is a Ruby on Rails application that require 'watson-api-client', and if it is deployed on the Cloud Foundry, the watson-api-client can read environment variable VCAP_SERVICES directly. In this case, the specification of :user and :password are omissible.
|
129
104
|
|
130
105
|
###Natural Language Classifier example
|
131
106
|
|
132
107
|
Please see [examples/NaturalLanguageClassifier/README.md](https://github.com/suchowan/watson-api-client/tree/master/examples/NaturalLanguageClassifier/README.md).
|
133
108
|
|
134
109
|
|
135
|
-
Additional note
|
110
|
+
Additional note
|
136
111
|
-------
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
(1) The JSON file which held the list of APIs emptied.
|
141
|
-
|
142
|
-
(2) The version of Swagger which describes API specifications went up from 1.2 to 2.0.
|
143
|
-
|
144
|
-
|
145
|
-
They may be linked to the release of the IBM Watson for Japanese language.
|
146
|
-
|
147
|
-
The new version 0.0.3 corresponding to them was released provisionally.
|
148
|
-
Concerning about (1) in the version 0.0.3, the locations of JSON files which describe API specification are acquired from contents of web pages for human using regular expressions.
|
149
|
-
|
150
|
-
Essentially, as well as former versions, the location of the API documents should be readable with JSON file.
|
151
|
-
I will request to the IBM to revive the JSON file which held the list of APIs.
|
152
|
-
|
153
|
-
At present this gem is an alpha version and only the normal behavior of RelationshipExtraction(functionality), PersonalityInsights, VisualRecognition and Natural Language Classifier are confirmed.
|
112
|
+
At present this gem is an alpha version and only the normal behavior of a few services are confirmed.
|
154
113
|
It is welcome when you can cooperate with the confirmation of other various functions.
|
155
114
|
|
115
|
+
IBM announces that the Watson API Explorer will be removed on [October 31, 2018](https://watson-api-explorer.ng.bluemix.net/).
|
116
|
+
Therefore, the life span of this gem version is about one month. Please understand this situation before using this gem veresion.
|
156
117
|
|
157
118
|
Credits
|
158
119
|
-------
|
159
|
-
Copyright (c) 2015-
|
120
|
+
Copyright (c) 2015-2018 [Takashi SUGA](http://hosi.org/TakashiSuga.ttl)
|
160
121
|
|
161
122
|
|
162
123
|
Legal
|
data/lib/watson-api-client.rb
CHANGED
@@ -6,9 +6,7 @@ require 'pp' if __FILE__ == $PROGRAM_NAME
|
|
6
6
|
|
7
7
|
class WatsonAPIClient
|
8
8
|
|
9
|
-
VERSION = '0.
|
10
|
-
|
11
|
-
class Alchemy < self; end
|
9
|
+
VERSION = '0.8.2'
|
12
10
|
|
13
11
|
class << self
|
14
12
|
|
@@ -18,25 +16,18 @@ class WatsonAPIClient
|
|
18
16
|
apis = {}
|
19
17
|
|
20
18
|
# Watson API Explorer
|
21
|
-
|
22
|
-
open(doc_urls
|
19
|
+
host = doc_urls[/^https?:\/\/[^\/]+/]
|
20
|
+
open(doc_urls, Options, &:read).scan(/<a class="swagger-list--item-link" href="\/(.+?)".*?>\s*(.+?)\s*<\/a>/i) do
|
23
21
|
begin
|
24
|
-
api = {'path'=>doc_urls
|
22
|
+
api = {'path'=>doc_urls + $1, 'title'=>$2.sub(/\s*\(.+?\)$/,'')}
|
25
23
|
open(api['path'], Options, &:read).scan(/url:\s*'(.+?)'/) do
|
26
|
-
api['path'] =
|
24
|
+
api['path'] = host + $1
|
27
25
|
end
|
28
26
|
apis[api['title']] = api
|
29
27
|
rescue OpenURI::HTTPError
|
30
28
|
end
|
31
29
|
end
|
32
30
|
|
33
|
-
# Watson Developercloud
|
34
|
-
host2 = doc_urls[:doc_base2][/^https?:\/\/[^\/]+/]
|
35
|
-
open(doc_urls[:doc_base2], Options, &:read).scan(/<li>\s*<img.+data-src=.+?>\s*<h2><a href="(.+?)".*?>\s*(.+?)\s*<\/a><\/h2>\s*<p>(.+?)<\/p>\s*<\/li>/) do
|
36
|
-
api = {'path'=>$1, 'title'=>$2, 'description'=>$3}
|
37
|
-
apis[api['title']]['description'] = api['description'] if api['path'] !~ /\.\./ && apis.key?(api['title'])
|
38
|
-
end
|
39
|
-
|
40
31
|
apis
|
41
32
|
end
|
42
33
|
|
@@ -69,7 +60,7 @@ class WatsonAPIClient
|
|
69
60
|
access = access.downcase
|
70
61
|
nickname = (operation['operationId'] || path.gsub(/\/\{.+?\}/,'').split('/').last) #.sub(/(.)/) {$1.downcase}
|
71
62
|
[nickname, nickname+'_'+access].each do |name|
|
72
|
-
methods[name][access] = {'path'=>path, 'operation'=>operation, 'body'=>body, 'query'=>query, 'min'=>min, 'max'=>max}
|
63
|
+
methods[name][access] = {'path'=>path, 'operation'=>operation, 'body'=>body, 'query'=>query, 'min'=>min, 'max'=>max, 'consumes'=>operation['consumes']}
|
73
64
|
end
|
74
65
|
digest[nickname][access] = {'path'=>path, 'summary'=>operation['summary']}
|
75
66
|
end
|
@@ -79,23 +70,15 @@ class WatsonAPIClient
|
|
79
70
|
end
|
80
71
|
|
81
72
|
api_docs = {
|
82
|
-
:gateway
|
83
|
-
:
|
84
|
-
:doc_base1 => 'https://watson-api-explorer.mybluemix.net/',
|
85
|
-
:doc_base2 => 'https://www.ibm.com/watson/developercloud/doc/',
|
73
|
+
:gateway => 'https://gateway.watsonplatform.net',
|
74
|
+
:doc_base => 'https://watson-api-explorer.mybluemix.net/',
|
86
75
|
:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE
|
87
76
|
}
|
88
77
|
JSON.parse(ENV['WATSON_API_DOCS'] || '{}').each_pair do |key, value|
|
89
78
|
api_docs[key.to_sym] = value
|
90
79
|
end
|
91
|
-
doc_urls =
|
92
|
-
|
93
|
-
:doc_base2 => api_docs.delete(:doc_base2)
|
94
|
-
}
|
95
|
-
Gateways = {
|
96
|
-
:gateway => api_docs.delete(:gateway),
|
97
|
-
:gateway_a => api_docs.delete(:gateway_a)
|
98
|
-
}
|
80
|
+
doc_urls = api_docs.delete(:doc_base)
|
81
|
+
Gateways = api_docs.delete(:gateway)
|
99
82
|
Options = api_docs
|
100
83
|
Services = JSON.parse(ENV['VCAP_SERVICES'] || '{}')
|
101
84
|
DefaultParams = {:user=>'username', :password=>'password'}
|
@@ -103,14 +86,9 @@ class WatsonAPIClient
|
|
103
86
|
|
104
87
|
retrieve_doc(doc_urls).each_value do |list|
|
105
88
|
AvailableAPIs << list['title'].gsub(/\s+(.)/) {$1.upcase}
|
106
|
-
|
107
|
-
case list['title']
|
108
|
-
when /^Alchemy/; ['Alchemy', 'alchemy_api' ]
|
109
|
-
when /^Visual/ ; ['Alchemy', 'watson_vision_combined' ]
|
110
|
-
else ; ['WatsonAPIClient', list['title'].gsub(/\s+/,'_').downcase]
|
111
|
-
end
|
89
|
+
env = list['title'].gsub(/\s+/,'_').downcase
|
112
90
|
module_eval %Q{
|
113
|
-
class #{list['title'].gsub(/\s+(.)/) {$1.upcase}} <
|
91
|
+
class #{list['title'].gsub(/\s+(.)/) {$1.upcase}} < WatsonAPIClient
|
114
92
|
Service = WatsonAPIClient::Services['#{env}']
|
115
93
|
RawDoc = "#{list['path']}"
|
116
94
|
|
@@ -145,7 +123,7 @@ class WatsonAPIClient
|
|
145
123
|
def initialize(options={})
|
146
124
|
define_api_methods
|
147
125
|
set_variables(options)
|
148
|
-
@url ||= Gateways
|
126
|
+
@url ||= Gateways + self.class::API['apis']['basePath']
|
149
127
|
@options = {}
|
150
128
|
self.class.superclass::DefaultParams.each_pair do |sym, key|
|
151
129
|
@options[sym] = @credential[key] if @credential.key?(key)
|
@@ -218,6 +196,9 @@ class WatsonAPIClient
|
|
218
196
|
options.keys.each do |key|
|
219
197
|
options[key.to_s] = options.delete(key) if key.kind_of?(Symbol)
|
220
198
|
end
|
199
|
+
(spec['min'] - options.keys).each do |key|
|
200
|
+
options[key.to_s] = @options[key.to_sym] if @options.include?(key.to_sym)
|
201
|
+
end
|
221
202
|
lacked = spec['min'] - options.keys
|
222
203
|
extra = options.keys - spec['max']
|
223
204
|
raise ArgumentError, "Lacked parameter(s) : '#{lacked.join(', ')}', see #{self.class::RawDoc}." unless lacked.empty?
|
@@ -226,36 +207,9 @@ class WatsonAPIClient
|
|
226
207
|
spec['query'].each do |param|
|
227
208
|
query[param] = options.delete(param) if options.key?(param)
|
228
209
|
end
|
210
|
+
options[:content_type] = spec['consumes'].first if spec['consumes'].kind_of?(Array) && spec['consumes'].length == 1
|
229
211
|
path = spec['path'].gsub(/\{(.+?)\}/) {options.delete($1)}
|
230
212
|
path += '?' + URI.encode_www_form(query) unless query.empty?
|
231
213
|
[path, access, spec]
|
232
214
|
end
|
233
|
-
|
234
|
-
#
|
235
|
-
# for Alchemy APIs
|
236
|
-
#
|
237
|
-
class Alchemy < self
|
238
|
-
|
239
|
-
DefaultParams = %w(apikey api_key version)
|
240
|
-
|
241
|
-
def initialize(options={})
|
242
|
-
define_api_methods
|
243
|
-
set_variables(options)
|
244
|
-
@url ||= (Gateways[:gateway_a] + self.class::API['apis']['basePath']).sub('/alchemy-api','')
|
245
|
-
@apikey = {}
|
246
|
-
self.class.superclass::DefaultParams.each do |key|
|
247
|
-
@apikey[key] = @credential[key] if @credential.key?(key)
|
248
|
-
@apikey[key] = options.delete(key.to_sym) if options.key?(key.to_sym)
|
249
|
-
end
|
250
|
-
@options = options
|
251
|
-
@service = RestClient::Resource.new(@url, @options)
|
252
|
-
end
|
253
|
-
|
254
|
-
private
|
255
|
-
|
256
|
-
def swagger_info(method, options)
|
257
|
-
options.update(@apikey)
|
258
|
-
super(method, options)
|
259
|
-
end
|
260
|
-
end
|
261
215
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: watson-api-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Takashi SUGA
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|