Class: Savon::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/savon/client.rb

Overview

Savon::Client

Savon::Client is the main object for connecting to a SOAP service. It includes methods to access both the Savon::WSDL::Document and HTTPI::Request object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Client

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 { wsdl.document = "http://example.com/UserService?wsdl" }

# Using a local WSDL
client = Savon::Client.new { wsdl.document = "../wsdl/user_service.xml" }

# Directly accessing a SOAP endpoint
client = Savon::Client.new do
  wsdl.endpoint = "http://example.com/UserService"
  wsdl.namespace = "http://users.example.com"
end


32
33
34
35
# File 'lib/savon/client.rb', line 32

def initialize(&block)
  process 1, &block if block
  wsdl.request = http
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

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



151
152
153
154
# File 'lib/savon/client.rb', line 151

def method_missing(method, *args, &block)
  super unless original_self
  original_self.send method, *args, &block
end

Instance Attribute Details

#soapObject

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.



55
56
57
# File 'lib/savon/client.rb', line 55

def soap
  @soap
end

Instance Method Details

#httpObject

Returns the HTTPI::Request.



43
44
45
# File 'lib/savon/client.rb', line 43

def http
  @http ||= HTTPI::Request.new
end

#request(*args, &block) ⇒ Object

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")

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/savon/client.rb', line 70

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

#wsdlObject

Returns the Savon::WSDL::Document.



38
39
40
# File 'lib/savon/client.rb', line 38

def wsdl
  @wsdl ||= WSDL::Document.new
end

#wsseObject

Returns the Savon::WSSE object.



48
49
50
# File 'lib/savon/client.rb', line 48

def wsse
  @wsse ||= WSSE.new
end