require 'soap/soap' require 'soapex/error' require 'soapex/services' def atsym(name) ("@"+(name.to_s)).to_sym end def fix_case_up(name) (newname = name.dup)[0] = name[0,1].upcase # upper first character newname end module SOAP::MultiService class Client class Config def initialize(options={}) @sandbox = false IO.foreach(filename) do |line| next if line =~ /^#/ name, value = line.split('=') value = (options[name] || value.strip) instance_variable_set(atsym(name), value) end @sandbox = ['True', 'true', '1'].include?(@sandbox.strip) end def filename raise NotImplementedError.new( 'method filename must be implemented by a subclass') end end attr_reader :config, :drivers def initialize(options={}) @methodMap = Hash.new prepareDrivers end def method_missing(m, *args) methodName = m.id2name requestName = fix_case_up(methodName) # upper first character if driver = getMethodDriver(methodName) serviceName = driver.class.name.split("::").last req = getRequestClass(serviceName, requestName).new(*args) return driver.method(methodName).call(req) else raise(SOAP::MultiService::UnknownAPICall, "Unknown API Call: #{requestName}", caller) end # Handle application level error rescue SOAP::FaultError => fault raise(SOAP::MultiService::ApiError.new(fault), "#{methodName} Call Failed: #{fault.faultstring.to_s}", caller) end private def modulePrefix raise NotImplementedError.new('method modulePrefix') end def prepareDrivers raise NotImplementedError.new( 'method prepareDrivers must be implemented by a subclass') end def updateMethodMap @methodMap.update( SOAP::MultiService::Services.makeMethodMap(@drivers.values)) end def getMethodDriver(methodName) @methodMap[methodName] end def getRequestClass(serviceName, requestName) eval "#{modulePrefix}::#{serviceName}::#{requestName}" end def makeDriver(name, location=nil) driver = eval("#{modulePrefix}::#{name}Service::#{name}Service").new driver.prepare(@headers, location) driver end end end