Skip to content

Instantly share code, notes, and snippets.

@rubiii
Created June 6, 2010 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubiii/427832 to your computer and use it in GitHub Desktop.
Save rubiii/427832 to your computer and use it in GitHub Desktop.
Savon - Instantiating a Savon::Client
# rubygems.org/gems/savon by examples
# overview of examples at: http://gist.github.com/gists/427837
require "savon"
# = Instantiation without a WSDL
#
# Retrieving and parsing the WSDL is a pretty slow operation. And even though it's cached after the first time,
# it's still a pretty slow operation.
#
# When working without a WSDL you have to set the SOAP namespace and endpoint manually. Also Savon does not know
# anything about the SOAP input tag and soapAction to use. Take a look at: http://gist.github.com/427897
# for more information about how Savon tries to find out the right naming without a WSDL.
#
# In case there is a WSDL describing your service, here's how you can find the namespace and endpoint:
#
# * The namespace is defined in the "targetNamespace" attribute of the wsdl:definitions tag
# * The endpoint is defined in the "location" attribute of the wsdl:definitions/wsdl:service/wsdl:port/soap:address tag
client = Savon::Client.new do
soap.namespace = "http://ns.users.example.com"
soap.endpoint = "http://users.example.com"
end
# = Instantiation with a remote WSDL
#
# You can use Savon with a remote WSDL from the Web. It's not recommended, but it sure has some advantages
#
# * Both namespace and endpoint are set automatically
# * Savon::Client raises a NoMethodError when trying to execute an invalid SOAP request
# * Also Savon knows the SOAP input tag and soapAction to use
client = Savon::Client.new do
wsdl.location = "http://users.example.com?wsdl"
end
# = Instantiation with a local WSDL
#
# Using a local WSDL is a good alternative to not using a WSDL at all. Loading the WSDL from a file is
# obviously much faster than going over HTTP, but it still needs to be parsed.
client = Savon::Client.new do
wsdl.location = "/Users/rubiii/services/user.wsdl"
end
# = Instantiation and setting the WSDL as a String
#
# In case you can't pass in the path to your local WSDL document, you can still load it yourself and
# pass it to the Savon::WSDL object.
user_wsdl = IO.read "/Users/rubiii/services/user.wsdl"
client = Savon::Client.new { wsdl.document = user_wsdl }
# = Instantiation with a WSDL and a custom SOAP endpoint
#
# When using a (local or remote) WSDL document, you can still overwrite the SOAP endpoint.
client = Savon::Client.new do
wsdl.location = "http://users.example.com?wsdl"
soap.endpoint = "http://custom.example.com"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment