class Savon::Client

Savon::Client

Savon::Client is the main object for connecting to a SOAP service.

Attributes

original_self[RW]

Accessor for the original self of a given block.

soap[RW]

Returns the Savon::SOAP::XML object. Please notice, that this object is only available in a block given to Savon::Client#request. A new instance of this object is created per SOAP request.

Public Class Methods

new(wsdl_document = nil, &block) click to toggle source

Initializes the Savon::Client for a SOAP service. Accepts a block which is evaluated in the context of this object to let you access the wsdl, http, and wsse methods.

Examples

# Using a remote WSDL
client = Savon::Client.new("http://example.com/UserService?wsdl")

# Using a local WSDL
client = Savon::Client.new File.expand_path("../wsdl/service.xml", __FILE__)

# Directly accessing a SOAP endpoint
client = Savon::Client.new do
  wsdl.endpoint = "http://example.com/UserService"
  wsdl.namespace = "http://users.example.com"
end
# File lib/savon/client.rb, line 32
def initialize(wsdl_document = nil, &block)
  wsdl.document = wsdl_document if wsdl_document
  process 1, &block if block
  wsdl.request = http
end

Public Instance Methods

http() click to toggle source

Returns the HTTPI::Request.

# File lib/savon/client.rb, line 44
def http
  @http ||= HTTPI::Request.new
end
request(*args, &block) click to toggle source

Executes a SOAP request for a given SOAP action. Accepts a block which is evaluated in the context of this object to let you access the soap, wsdl, http and wsse methods.

Examples

# Calls a "getUser" SOAP action with the payload of "<userId>123</userId>"
client.request(:get_user) { soap.body = { :user_id => 123 } }

# Prefixes the SOAP input tag with a given namespace: "<wsdl:GetUser>...</wsdl:GetUser>"
client.request(:wsdl, "GetUser") { soap.body = { :user_id => 123 } }

# SOAP input tag with attributes: <getUser xmlns:wsdl="http://example.com">...</getUser>"
client.request(:get_user, "xmlns:wsdl" => "http://example.com")
# File lib/savon/client.rb, line 71
def request(*args, &block)
  raise ArgumentError, "Savon::Client#request requires at least one argument" if args.empty?

  self.soap = SOAP::XML.new
  preconfigure extract_options(args)
  process &block if block
  soap.wsse = wsse

  response = SOAP::Request.new(http, soap).response
  set_cookie response.http.headers
  response
end
wsdl() click to toggle source

Returns the Savon::Wasabi::Document.

# File lib/savon/client.rb, line 39
def wsdl
  @wsdl ||= Wasabi::Document.new
end
wsse() click to toggle source

Returns the Akami::WSSE object.

# File lib/savon/client.rb, line 49
def wsse
  @wsse ||= Akami.wsse
end

Private Instance Methods

evaluate(&block) click to toggle source

Evaluates a given block inside this object. Stores the original block binding.

# File lib/savon/client.rb, line 146
def evaluate(&block)
  self.original_self = eval "self", block.binding
  instance_eval &block
end
extract_options(args) click to toggle source

Expects an Array of args and returns an Array containing the namespace (might be nil), the SOAP input and a Hash of attributes for the input tag (which might be empty).

# File lib/savon/client.rb, line 99
def extract_options(args)
  attributes = Hash === args.last ? args.pop : {}
  namespace = args.size > 1 ? args.shift.to_sym : nil
  input = args.first

  [namespace, input, attributes]
end
method_missing(method, *args, &block) click to toggle source

Handles calls to undefined methods by delegating to the original block binding.

Calls superclass method
# File lib/savon/client.rb, line 152
def method_missing(method, *args, &block)
  super unless original_self
  original_self.send method, *args, &block
end
preconfigure(options) click to toggle source

Expects and Array of options and preconfigures the system.

# File lib/savon/client.rb, line 108
def preconfigure(options)
  soap.endpoint = wsdl.endpoint
  soap.namespace_identifier = options[0]
  soap.namespace = wsdl.namespace
  soap.element_form_default = wsdl.element_form_default if wsdl.document?
  soap.body = options[2].delete(:body)

  set_soap_action options[1]
  set_soap_input *options
end
process(offset = 0, &block) click to toggle source

Processes a given block. Yields objects if the block expects any arguments. Otherwise evaluates the block in the context of this object.

# File lib/savon/client.rb, line 135
def process(offset = 0, &block)
  block.arity > 0 ? yield_objects(offset, &block) : evaluate(&block)
end
set_soap_action(input) click to toggle source

Expects an input and sets the SOAPAction HTTP headers.

# File lib/savon/client.rb, line 120
def set_soap_action(input)
  soap_action = wsdl.soap_action(input.to_sym) if wsdl.document?
  soap_action ||= Gyoku::XMLKey.create(input).to_sym
  http.headers["SOAPAction"] = %Q{"#{soap_action}"}
end
set_soap_input(namespace, input, attributes) click to toggle source

Expects a namespace, input and attributes and sets the SOAP input.

# File lib/savon/client.rb, line 127
def set_soap_input(namespace, input, attributes)
  new_input = wsdl.soap_input(input.to_sym) if wsdl.document?
  new_input ||= Gyoku::XMLKey.create(input)
  soap.input = [namespace, new_input.to_sym, attributes].compact
end
yield_objects(offset) { |*[soap, wsdl, http, wsse][offset, arity]| ... } click to toggle source

Yields a number of objects to a given block depending on how many arguments the block is expecting.

# File lib/savon/client.rb, line 141
def yield_objects(offset, &block)
  yield *[soap, wsdl, http, wsse][offset, block.arity]
end