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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wsdl_document = nil, &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("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



33
34
35
36
37
38
39
# File 'lib/savon/client.rb', line 33

def initialize(wsdl_document = nil, &block)
  self.config = Savon.config.clone
  wsdl.document = wsdl_document if wsdl_document

  process 1, &block if block
  wsdl.request = http
end

Instance Attribute Details

#configObject

Accessor for the Savon::Config.



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

def config
  @config
end

Instance Method Details

#httpObject

Returns the HTTPI::Request.



50
51
52
# File 'lib/savon/client.rb', line 50

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 the SOAP::RequestBuilder object to let you access its +soap+, +wsdl+, +http+ and +wsse+ methods.

== Examples

# Calls a "getUser" SOAP action with the payload of "123" 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: ..." client.request(:get_user, "xmlns:wsdl" => "http://example.com")

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/savon/client.rb', line 73

def request(*args, &block)
  raise ArgumentError, "Savon::Client#request requires at least one argument" if args.empty?

  options = extract_options(args)

  request_builder = SOAP::RequestBuilder.new(options.delete(:input), options)
  request_builder.wsdl = wsdl
  request_builder.http = http.dup
  request_builder.wsse = wsse.dup
  request_builder.config = config.dup

  post_configuration = lambda { process(0, request_builder, &block) if block }

  response = request_builder.request(&post_configuration).response
  http.set_cookies(response.http)

  if wsse.verify_response
    WSSE::VerifySignature.new(response.http.body).verify!
  end

  response
end

#wsdlObject

Returns the Wasabi::Document.



45
46
47
# File 'lib/savon/client.rb', line 45

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

#wsseObject

Returns the Akami::WSSE object.



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

def wsse
  @wsse ||= Akami.wsse
end