Class: WatsonAPIClient

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(options = {}) ⇒ WatsonAPIClient

Note:

VCAP_SERVICES is IBM Bluemix™ environment variable.

All subclass constructors use following hash parameter -

Parameters:

  • options (Hash) (defaults to: {})

    See following..

Options Hash (options):

  • :url (String)

    API URL (default: the url described in listings or VCAP_SERVICES)

  • :user (String)

    USER ID (default: the username described in VCAP_SERVICES)

  • :password (String)

    USER Password (default: the password described in VCAP_SERVICES)

  • other_options (Object)

    Other options are passed to RestClient::Resource.new as it is.



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/watson-api-client.rb', line 80

def initialize(options={})
  credential = self.class::Service ? self.class::Service.first['credentials'] : {}
  if options[:url]
    @url   = options.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
  @options = {:user=>credential['username'], :password=>credential['password']}.merge(options)
  @service = RestClient::Resource.new(@url, @options)
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