Class: Cherby::Client

Inherits:
Savon::Client
  • Object
show all
Defined in:
lib/cherby/client.rb

Overview

Cherwell SOAP Client wrapper

Instance Method Summary collapse

Constructor Details

#initialize(base_url, verbose_logging = false) ⇒ Client

Create a Cherwell Client for the SOAP API at the given base URL.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cherby/client.rb', line 14

def initialize(base_url, verbose_logging=false)
  if File.exist?(base_url)
    wsdl_url = base_url
  elsif base_url =~ /^http/
    if base_url.downcase.end_with?('?wsdl')
      wsdl_url = base_url
    else
      wsdl_url = "#{base_url}?WSDL"
    end
  elsif base_url !~ /^http/
    raise ArgumentError, "Client URL must be a local file, or begin with http"
  end
  super(:wsdl => wsdl_url, :log => verbose_logging)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

If a method in #known_methods is called, send it as a request.

Examples:

# Login with positional arguments:
client.('sisko', 'baseball')
# or a Hash of arguments:
client.(:userId => 'sisko', :password => 'baseball')

# Get a BusinessObject definition with positional arguments:
client.get_business_object_definition('JournalNote')
# or a Hash of arguments:
client.get_business_object_definition(:nameOrId => 'JournalNote')


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

def method_missing(method, *args, &block)
  if known_methods.include?(method)
    if args.first.is_a?(Hash)
      call_wrap(method, args.first)
    else
      hash_args = args_to_hash(method, *args)
      call_wrap(method, hash_args)
    end
  else
    super
  end
end

Instance Method Details

#args_to_hash(method, *args) ⇒ Hash<String>

Convert positional parameters into a :key => value hash, with parameter names inferred from #params_for_method.

Examples:

client.args_to_hash(:login, 'odo', 'nerys')
# => {:userId => 'odo', :password => 'nerys'}

Raises:

  • (ArgumentError)

    If the given number of args doesn't match the number of parameters expected by method.



139
140
141
142
143
144
145
146
# File 'lib/cherby/client.rb', line 139

def args_to_hash(method, *args)
  params = params_for_method(method)
  if params.count != args.count
    raise ArgumentError.new(
      "Wrong number of arguments (#{args.count} for #{params.count})")
  end
  return Hash[params.keys.zip(args)]
end

#call_wrap(method, body = {}) ⇒ String

Call a given SOAP method with an optional body.

Examples:

client.call_wrap(:login, {:userId => 'garak', :pasword => 'fabric'})
# => true


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cherby/client.rb', line 45

def call_wrap(method, body={})
  method = method.to_sym
  if !known_methods.include?(method)
    raise ArgumentError, "Unknown Cherwell SOAP API method: #{method}"
  end
  # FIXME: Let Savon handle this snake_case stuff
  # Each request has a *_response containing a *_result
  response_field = (method.to_s + '_response').to_sym
  result_field = (method.to_s + '_result').to_sym
  # Submit the request
  response = self.call(method, :message => body)
  return response.to_hash[response_field][result_field]
end

#known_methodsArray<Symbol>

Valid methods in the Cherwell SOAP API



97
98
99
# File 'lib/cherby/client.rb', line 97

def known_methods
  return self.operations.sort
end

#params_for_method(method) ⇒ Hash

Return parameters for the given Cherwell API method.



111
112
113
114
115
116
117
# File 'lib/cherby/client.rb', line 111

def params_for_method(method)
  if @wsdl.operations.include?(method)
    return @wsdl.operations[method][:parameters] || {}
  else
    return {}
  end
end