watson-api-client 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7e3210faf43b3240684b0871e6a09bc77c0aa5ad
4
- data.tar.gz: 2b40d2d3d87820368555f4577de2f23e538c8765
3
+ metadata.gz: bf43a7c4e5b1a840e5e59c752434155f9a1704e4
4
+ data.tar.gz: ac7cf313d8948c803dba69ac10121a6618027a48
5
5
  SHA512:
6
- metadata.gz: bfcccc4f138b4ef62b69af897584580f86a6fdc2b71cd221e5688fe6021552f7edfe2ee5c5fb754addc07dd59d5bb991091eab8bc2d2b20ff975813dc17f3bc5
7
- data.tar.gz: 56d9179cf6db6a307f1ca6e59e99ee3ebec842e80d36ebb901cfe6692314655b31e2abb5ff7573466c57518b17c33bb54256b43d2911f40ea3e74cf7115f00ba
6
+ metadata.gz: edbe184d558657b75a05a7852f64b99554dbfaeab16176c9d25621ed275dd92c4988c358c83db440ed3bd63188483894711ce76db40e1d7eed3764b54d7a548f
7
+ data.tar.gz: d084db495948621d638cda6289c31e5200aaefdb1a2cc3660b4c7ad9c0763848a16c8afcb5be8f13ea5c668a4c8fc4cecff7a6e4003fa0f1e92c4d61929a53eb
data/LICENSE CHANGED
@@ -1,21 +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.
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 CHANGED
@@ -55,7 +55,7 @@ Let's use the 'Relationship Extraction' service.
55
55
  'txt' => 'John Smith lives in New York, and he has been living there since 2001.')
56
56
  p JSON.parse(result.body)
57
57
 
58
- ####The generation of the RelationshipExtraction service object
58
+ ####Generation of the RelationshipExtraction service object
59
59
  First of all, the instance of the RelationshipExtraction class has to be generated.
60
60
  The constructor argument is passed to the constructor of [RestClient::Resource](http://www.rubydoc.info/gems/rest-client/RestClient/Resource) class.
61
61
  Please refer to the document of the rest-client for the details of this hash argument.
@@ -70,7 +70,7 @@ Therefore, it is necessary to start up a server application even if it is a dumm
70
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
71
  In this case, the specification of :user and :password is omissible.
72
72
 
73
- ####The extraction of relationship in an example sentence using RelationshipExtraction#extract
73
+ ####Extraction of relationship in an example sentence using RelationshipExtraction#extract
74
74
  Next, by the 'extract' method of the RelationshipExtraction class, we try to extract relationship in an example sentence.
75
75
  How to set the argument can be seen by clicking on the 'Expand Operations' link of the Watson API Reference 'Relationship Extraction'.
76
76
  The method name called 'extract' is the nickname corresponding to 'POST /v1/sire/0'.
@@ -95,17 +95,30 @@ Next, let's use 'Personality Insights'.
95
95
  'Accept' => "application/json",
96
96
  'Accept-Language' => "en",
97
97
  'Content-Language' => "en",
98
- 'body' => open('https://raw.githubusercontent.com/suchowan/when_exe/master/LICENSE.txt',
98
+ 'body' => open('https://raw.githubusercontent.com/suchowan/watson-api-client/master/LICENSE',
99
99
  :ssl_verify_mode=>OpenSSL::SSL::VERIFY_NONE))
100
100
  p JSON.parse(result.body)
101
101
 
102
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
103
  The rest-client and the watson-api-client judge which of path, query, header, body each argument is used for automatically.
104
104
 
105
+ ###Visual Recognition example
106
+
107
+ Last, let's use 'Visual Recognition'.
108
+
109
+ service = WatsonAPIClient::VisualRecognition.new(:user=>"xxxxxx",
110
+ :password=>"yyyyy",
111
+ :verify_ssl=>OpenSSL::SSL::VERIFY_NONE)
112
+ result = service.recognizeLabelsService(
113
+ 'img_File' => open('image.jpg','rb')
114
+ )
115
+ p JSON.parse(result.body)
116
+
117
+ It seems that the image file must be on the local file system.
105
118
 
106
119
  More
107
120
  -------
108
- At present this gem is a α version and only the normal behavior of RelationshipExtraction and PersonalityInsights are confirmed.
121
+ At present this gem is an α version and only the normal behavior of RelationshipExtraction, PersonalityInsights and VisualRecognition are confirmed.
109
122
  It is welcome when you can cooperate with the confirmation of other various functions.
110
123
 
111
124
 
data/Rakefile CHANGED
@@ -1 +1 @@
1
- require "bundler/gem_tasks"
1
+ require "bundler/gem_tasks"
@@ -1,132 +1,121 @@
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
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.2'
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'].sub(/^[-_\/]+/,'').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
+ self.class::API['methods'].each_pair do |method, definition|
82
+ self.class.module_eval %Q{define_method("#{method}",
83
+ Proc.new {|options={}| rest_access_#{definition['body'] ? 'with' : 'without'}_body("#{method}", options)}
84
+ )} unless respond_to?(method)
85
+ end
86
+ credential = self.class::Service ? self.class::Service.first['credentials'] : {}
87
+ if options[:url]
88
+ @url = options.delete(:url)
89
+ elsif credential['url']
90
+ @url = credential['url']
91
+ else
92
+ @url = self.class::API['apis']['basePath']
93
+ @url += self.class::API['apis']['resourcePath'] unless @url.index(self.class::API['apis']['resourcePath'])
94
+ end
95
+ @options = {:user=>credential['username'], :password=>credential['password']}.merge(options)
96
+ @service = RestClient::Resource.new(@url, @options)
97
+ end
98
+
99
+ private
100
+
101
+ def rest_access_without_body(method, options={})
102
+ path, access = swagger_info(method, options)
103
+ @service[path].send(access, options)
104
+ end
105
+
106
+ def rest_access_with_body(method, options={})
107
+ path, access = swagger_info(method, options)
108
+ body = options.delete(self.class::API['methods'][method.to_s]['body'])
109
+ @service[path].send(access, body, options)
110
+ end
111
+
112
+ def swagger_info(method, options)
113
+ spec = self.class::API['methods'][method.to_s]
114
+ lacked = []
115
+ spec['operation']['parameters'].each do |parameter|
116
+ lacked << parameter['name'] if parameter['required'] && !options[parameter['name']]
117
+ end
118
+ raise ArgumentError, "Parameter(s) '#{lacked.join(', ')}' required, see #{self.class::RawDoc}." unless lacked.empty?
119
+ [spec['path'].gsub(/\{(.+?)\}/) {options.delete($1)}, spec['operation']['method'].downcase]
120
+ end
121
+ end
@@ -1,24 +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
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 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.0.1
4
+ version: 0.0.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: 2015-07-10 00:00:00.000000000 Z
11
+ date: 2015-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client