watson-api-client 0.0.1
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 +7 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +121 -0
- data/Rakefile +1 -0
- data/lib/watson-api-client.rb +132 -0
- data/watson-api-client.gemspec +24 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7e3210faf43b3240684b0871e6a09bc77c0aa5ad
|
4
|
+
data.tar.gz: 2b40d2d3d87820368555f4577de2f23e538c8765
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bfcccc4f138b4ef62b69af897584580f86a6fdc2b71cd221e5688fe6021552f7edfe2ee5c5fb754addc07dd59d5bb991091eab8bc2d2b20ff975813dc17f3bc5
|
7
|
+
data.tar.gz: 56d9179cf6db6a307f1ca6e59e99ee3ebec842e80d36ebb901cfe6692314655b31e2abb5ff7573466c57518b17c33bb54256b43d2911f40ea3e74cf7115f00ba
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Takashi SUGA <suchowan@box.email.ne.jp>.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
watson-api-client - An IBM Watson™ API client
|
2
|
+
================================================================
|
3
|
+
|
4
|
+
[](http://badge.fury.io/rb/watson-api-client)
|
5
|
+
|
6
|
+
The [watson-api-client](http://rubygems.org/gems/watson-api-client) is a gem to use REST API on the IBM Watson™ Developer Cloud.
|
7
|
+
|
8
|
+
It wraps the [rest-client](https://rubygems.org/gems/rest-client) REST API using [Swagger](http://swagger.io/) documents retrievable from the [Watson API Reference](https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/).
|
9
|
+
|
10
|
+
|
11
|
+
Installation
|
12
|
+
------------
|
13
|
+
|
14
|
+
The watson-api-client gem can be installed by running:
|
15
|
+
|
16
|
+
gem install watson-api-client
|
17
|
+
|
18
|
+
Since watson-api-client is dependent on the rest-client, when the rest-client is not installed, the rest-client is also installed automatically.
|
19
|
+
|
20
|
+
|
21
|
+
Documentation
|
22
|
+
-------------
|
23
|
+
|
24
|
+
The simple API documentation for the watson-api-client is available on [RubyDoc.info](http://rubydoc.info/gems/watson-api-client).
|
25
|
+
|
26
|
+
However, most of the classes and methods of this gem are not described in the above document because they are dynamically defined.
|
27
|
+
Instead, you can output to the standard output the actual those list of classes and methods when you run the lib/watson-api-client.rb directly.
|
28
|
+
|
29
|
+
|
30
|
+
Source Code
|
31
|
+
-----------
|
32
|
+
|
33
|
+
The source code for the watson-api-client is available on [GitHub](https://github.com/suchowan/watson-api-client).
|
34
|
+
|
35
|
+
|
36
|
+
Example Usage
|
37
|
+
-------------
|
38
|
+
|
39
|
+
###Preparation
|
40
|
+
|
41
|
+
The watson-api-client is a gem to use REST API on the IBM Watson™ Developer Cloud.
|
42
|
+
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.
|
43
|
+
For more information, refer to 'Getting Started' in '[Table of Contents for Services Documentation](http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/)'.
|
44
|
+
|
45
|
+
###Relationship Extraction example
|
46
|
+
|
47
|
+
Let's use the 'Relationship Extraction' service.
|
48
|
+
|
49
|
+
require 'watson-api-client'
|
50
|
+
service = WatsonAPIClient::RelationshipExtraction.new(:user=>"xxxxxx",
|
51
|
+
:password=>"yyyyy",
|
52
|
+
:verify_ssl=>OpenSSL::SSL::VERIFY_NONE)
|
53
|
+
result = service.extract('rt' => 'json',
|
54
|
+
'sid' => 'ie-en-news',
|
55
|
+
'txt' => 'John Smith lives in New York, and he has been living there since 2001.')
|
56
|
+
p JSON.parse(result.body)
|
57
|
+
|
58
|
+
####The generation of the RelationshipExtraction service object
|
59
|
+
First of all, the instance of the RelationshipExtraction class has to be generated.
|
60
|
+
The constructor argument is passed to the constructor of [RestClient::Resource](http://www.rubydoc.info/gems/rest-client/RestClient/Resource) class.
|
61
|
+
Please refer to the document of the rest-client for the details of this hash argument.
|
62
|
+
|
63
|
+
Class name called RelationshipExtraction is the camel case-ized service name of [Watson API Reference](http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/).
|
64
|
+
:user and :password are the 'username' and the 'password' picked out from environment variable VCAP_SERVICES.
|
65
|
+
Please refer to '[Viewing Bluemix environment variables](http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/getting_started/gs-bluemix.shtml#vcapViewing)' for the details of VCAP_SERVICES.
|
66
|
+
|
67
|
+
However, when no server application associated with the service exists, 'Show Credentials' link does not appear on the console window.
|
68
|
+
Therefore, it is necessary to start up a server application even if it is a dummy.
|
69
|
+
|
70
|
+
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.
|
71
|
+
In this case, the specification of :user and :password is omissible.
|
72
|
+
|
73
|
+
####The extraction of relationship in an example sentence using RelationshipExtraction#extract
|
74
|
+
Next, by the 'extract' method of the RelationshipExtraction class, we try to extract relationship in an example sentence.
|
75
|
+
How to set the argument can be seen by clicking on the 'Expand Operations' link of the Watson API Reference 'Relationship Extraction'.
|
76
|
+
The method name called 'extract' is the nickname corresponding to 'POST /v1/sire/0'.
|
77
|
+
This can be seen by opening the JSON code of Swagger by clicking on the '[Raw](http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/listings/relationship-extraction)' link of the 'Relationship Extraction' of API Reference window.
|
78
|
+
|
79
|
+
The list of the method of the RelationshipExtraction class can be seen even by using the following script.
|
80
|
+
|
81
|
+
p WatsonAPIClient::RelationshipExtraction::API['digest']
|
82
|
+
|
83
|
+
Since 'json' is specified as Return type ('rt') in this example, the JSON string is stored in the body of the 'extract' method response.
|
84
|
+
When converting this JSON string to a hash object using JSON.parse method, the result of Relationship Extraction can be used variously by your client programs.
|
85
|
+
|
86
|
+
###Personality Insights example
|
87
|
+
|
88
|
+
Next, let's use 'Personality Insights'.
|
89
|
+
|
90
|
+
service = WatsonAPIClient::PersonalityInsights.new(:user=>"xxxxxx",
|
91
|
+
:password=>"yyyyy",
|
92
|
+
:verify_ssl=>OpenSSL::SSL::VERIFY_NONE)
|
93
|
+
result = service.profile(
|
94
|
+
'Content-Type' => "text/plain",
|
95
|
+
'Accept' => "application/json",
|
96
|
+
'Accept-Language' => "en",
|
97
|
+
'Content-Language' => "en",
|
98
|
+
'body' => open('https://raw.githubusercontent.com/suchowan/when_exe/master/LICENSE.txt',
|
99
|
+
:ssl_verify_mode=>OpenSSL::SSL::VERIFY_NONE))
|
100
|
+
p JSON.parse(result.body)
|
101
|
+
|
102
|
+
The class name, the method name, and the argument setting rules are the same as that of the case of 'Relationship Extraction' almost.
|
103
|
+
The rest-client and the watson-api-client judge which of path, query, header, body each argument is used for automatically.
|
104
|
+
|
105
|
+
|
106
|
+
More
|
107
|
+
-------
|
108
|
+
At present this gem is a α version and only the normal behavior of RelationshipExtraction and PersonalityInsights are confirmed.
|
109
|
+
It is welcome when you can cooperate with the confirmation of other various functions.
|
110
|
+
|
111
|
+
|
112
|
+
Credits
|
113
|
+
-------
|
114
|
+
Copyright (c) 2015 [Takashi SUGA](http://hosi.org/TakashiSuga.ttl)
|
115
|
+
|
116
|
+
|
117
|
+
Legal
|
118
|
+
-------
|
119
|
+
The watson-api-client is released under the MIT license, see [LICENSE](https://github.com/suchowan/watson-api-client/blob/master/LICENSE) for details.
|
120
|
+
|
121
|
+
IBM Watson™ and IBM Bluemix™ are trade marks of the IBM corporation.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'openssl'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'rest-client'
|
5
|
+
require 'pp' if __FILE__ == $PROGRAM_NAME
|
6
|
+
|
7
|
+
class WatsonAPIClient
|
8
|
+
|
9
|
+
VERSION = '0.0.1'
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def listings(apis)
|
13
|
+
methods = {}
|
14
|
+
digest = {}
|
15
|
+
apis['apis'].each do |api|
|
16
|
+
api['operations'].each do |operation|
|
17
|
+
body = nil
|
18
|
+
(operation['parameters']||[]).each do |parameter|
|
19
|
+
next unless parameter['paramType'] == 'body'
|
20
|
+
body = parameter['name']
|
21
|
+
break
|
22
|
+
end
|
23
|
+
nickname = operation['nickname'].sub(/(.)/) {$1.downcase}
|
24
|
+
methods[nickname] = {'path'=>api['path'], 'operation'=>operation, 'body'=>body}
|
25
|
+
digest[nickname] = {'method'=>operation['method'], 'path'=>api['path'], 'summary'=>operation['summary']}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
{'apis'=>apis, 'methods'=>methods, 'digest'=>digest}
|
29
|
+
end
|
30
|
+
private :listings
|
31
|
+
end
|
32
|
+
|
33
|
+
api_docs = {
|
34
|
+
:base => 'https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/',
|
35
|
+
:path => 'listings/api-docs.json',
|
36
|
+
:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE
|
37
|
+
}
|
38
|
+
JSON.parse(ENV['WATSON_API_DOCS'] || '{}').each_pair do |key, value|
|
39
|
+
api_docs[key.to_sym] = value
|
40
|
+
end
|
41
|
+
|
42
|
+
Services = JSON.parse(ENV['VCAP_SERVICES'] || '{}')
|
43
|
+
Base = api_docs.delete(:base)
|
44
|
+
path = api_docs.delete(:path)
|
45
|
+
Options = api_docs
|
46
|
+
listings = JSON.parse(open(Base + path, Options).read)
|
47
|
+
|
48
|
+
listings['apis'].each do |list|
|
49
|
+
module_eval %Q{
|
50
|
+
class #{list['path'].gsub(/[-_\/](.)/) {$1.upcase}} < self
|
51
|
+
Service = superclass::Services['#{list['path'][1..-1].gsub(/-/, '_')}']
|
52
|
+
RawDoc = "#{Base + listings['basePath'] + list['path']}"
|
53
|
+
|
54
|
+
class << self
|
55
|
+
alias :_const_missing :const_missing
|
56
|
+
|
57
|
+
def const_missing(constant)
|
58
|
+
if constant == :API
|
59
|
+
const_set(:API, listings(JSON.parse(open(RawDoc, superclass::Options).read)))
|
60
|
+
else
|
61
|
+
_const_missing(constant)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
pp [self, 'See ' + RawDoc, API['digest']] if '#{__FILE__}' == '#{$PROGRAM_NAME}'
|
66
|
+
end
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
# All subclass constructors use following hash parameter -
|
71
|
+
#
|
72
|
+
# @param [Hash] options See following..
|
73
|
+
# @option options [String] :url API URL (default: the url described in listings or VCAP_SERVICES)
|
74
|
+
# @option options [String] :user USER ID (default: the username described in VCAP_SERVICES)
|
75
|
+
# @option options [String] :password USER Password (default: the password described in VCAP_SERVICES)
|
76
|
+
# @option options [Object] other_options Other options are passed to RestClient::Resource.new[http://www.rubydoc.info/gems/rest-client/RestClient/Resource] as it is.
|
77
|
+
#
|
78
|
+
# @note VCAP_SERVICES[http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/getting_started/gs-bluemix.shtml#vcapViewing] is IBM Bluemix™ environment variable.
|
79
|
+
#
|
80
|
+
def initialize(options={})
|
81
|
+
credential = self.class::Service ? self.class::Service.first['credentials'] : {}
|
82
|
+
if options[:url]
|
83
|
+
@url = options.delete(:url)
|
84
|
+
elsif credential['url']
|
85
|
+
@url = credential['url']
|
86
|
+
else
|
87
|
+
@url = self.class::API['apis']['basePath']
|
88
|
+
@url += self.class::API['apis']['resourcePath'] unless @url.index(self.class::API['apis']['resourcePath'])
|
89
|
+
end
|
90
|
+
@options = {:user=>credential['username'], :password=>credential['password']}.merge(options)
|
91
|
+
@service = RestClient::Resource.new(@url, @options)
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
def rest_access_without_body(method, options={})
|
97
|
+
path, access = swagger_info(method, options)
|
98
|
+
@service[path].send(access, options)
|
99
|
+
end
|
100
|
+
|
101
|
+
def rest_access_with_body(method, options={})
|
102
|
+
path, access = swagger_info(method, options)
|
103
|
+
body = options.delete(self.class::API['methods'][method.to_s]['body'])
|
104
|
+
@service[path].send(access, body, options)
|
105
|
+
end
|
106
|
+
|
107
|
+
def swagger_info(method, options)
|
108
|
+
spec = self.class::API['methods'][method.to_s]
|
109
|
+
lacked = []
|
110
|
+
spec['operation']['parameters'].each do |parameter|
|
111
|
+
lacked << parameter['name'] if parameter['required'] && !options[parameter['name']]
|
112
|
+
end
|
113
|
+
raise ArgumentError, "Parameter(s) '#{lacked.join(', ')}' required, see #{self.class::RawDoc}." unless lacked.empty?
|
114
|
+
[spec['path'].gsub(/\{(.+?)\}/) {options.delete($1)}, spec['operation']['method'].downcase]
|
115
|
+
end
|
116
|
+
|
117
|
+
alias :_method_missing :method_missing
|
118
|
+
|
119
|
+
def method_missing(method, *args, &block)
|
120
|
+
definition = self.class::API['methods'][method.to_s]
|
121
|
+
if definition
|
122
|
+
self.class.module_eval %Q{
|
123
|
+
def #{method}(options={})
|
124
|
+
rest_access_#{definition['body'] ? 'with' : 'without'}_body("#{method}", options)
|
125
|
+
end
|
126
|
+
}
|
127
|
+
send(method, *args, &block)
|
128
|
+
else
|
129
|
+
_method_missing(method, *args, &block)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'watson-api-client'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'watson-api-client'
|
7
|
+
s.version = WatsonAPIClient::VERSION
|
8
|
+
s.authors = ['Takashi SUGA']
|
9
|
+
s.email = ['suchowan@box.email.ne.jp']
|
10
|
+
s.homepage = 'https://github.com/suchowan/watson-api-client'
|
11
|
+
s.license = 'MIT'
|
12
|
+
s.summary = %q{An IBM Watson™ API client}
|
13
|
+
s.description = %q{The watson-api-client is a gem to use REST API on the IBM Watson™ Developer Cloud. It wraps the rest-client REST API using Swagger documents retrievable from the Watson API Reference.}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ['lib']
|
19
|
+
s.required_ruby_version = '>= 1.9.3'
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency 'rspec'
|
23
|
+
s.add_runtime_dependency 'rest-client'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: watson-api-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Takashi SUGA
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: The watson-api-client is a gem to use REST API on the IBM Watson™ Developer
|
28
|
+
Cloud. It wraps the rest-client REST API using Swagger documents retrievable from
|
29
|
+
the Watson API Reference.
|
30
|
+
email:
|
31
|
+
- suchowan@box.email.ne.jp
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- lib/watson-api-client.rb
|
41
|
+
- watson-api-client.gemspec
|
42
|
+
homepage: https://github.com/suchowan/watson-api-client
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata: {}
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.9.3
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 2.4.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: An IBM Watson™ API client
|
66
|
+
test_files: []
|
67
|
+
has_rdoc:
|