Class: WatsonAPIClient
- Inherits:
-
Object
- Object
- WatsonAPIClient
- Defined in:
- lib/watson-api-client.rb
Constant Summary collapse
- VERSION =
'0.0.1'- Services =
JSON.parse(ENV['VCAP_SERVICES'] || '{}')
- Base =
api_docs.delete(:base)
- Options =
api_docs
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ WatsonAPIClient
constructor
All subclass constructors use following hash parameter -.
Constructor Details
#initialize(options = {}) ⇒ WatsonAPIClient
Note:
VCAP_SERVICES is IBM Bluemix™ environment variable.
All subclass constructors use following hash parameter -
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/watson-api-client.rb', line 80 def initialize(={}) credential = self.class::Service ? self.class::Service.first['credentials'] : {} if [:url] @url = .delete(:url) elsif credential['url'] @url = credential['url'] else @url = self.class::API['apis']['basePath'] @url += self.class::API['apis']['resourcePath'] unless @url.index(self.class::API['apis']['resourcePath']) end = {:user=>credential['username'], :password=>credential['password']}.merge() @service = RestClient::Resource.new(@url, ) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/watson-api-client.rb', line 119 def method_missing(method, *args, &block) definition = self.class::API['methods'][method.to_s] if definition self.class.module_eval %Q{ def #{method}(options={}) rest_access_#{definition['body'] ? 'with' : 'without'}_body("#{method}", options) end } send(method, *args, &block) else _method_missing(method, *args, &block) end end |
Class Method Details
.listings(apis) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/watson-api-client.rb', line 12 def listings(apis) methods = {} digest = {} apis['apis'].each do |api| api['operations'].each do |operation| body = nil (operation['parameters']||[]).each do |parameter| next unless parameter['paramType'] == 'body' body = parameter['name'] break end nickname = operation['nickname'].sub(/(.)/) {$1.downcase} methods[nickname] = {'path'=>api['path'], 'operation'=>operation, 'body'=>body} digest[nickname] = {'method'=>operation['method'], 'path'=>api['path'], 'summary'=>operation['summary']} end end {'apis'=>apis, 'methods'=>methods, 'digest'=>digest} end |