02 October, 2005

SOAP clients in Ruby

I ran into some problems getting my Rails prototype to talk to an ASP.NET web service the other day. It turns out that the Rails Web Service client isn't really designed to talk to anything other than a Rails application, so instead I had to use the inbuilt Ruby SOAP client, as designed by Hiroshi Nakamura.

However, that resulted in even more confusion. So, in the interests of clarity (and so I don't forget) here is what I did.

First off, save the service's WSDL locally ("http://myserver.com/folder/service.asmx?wsdl" to access the WSDL). Then download the latest patch release of Soap4R - from http://dev.ctor.org/download/soap4r-20050928.tar.gz. Unpack the tarball and run a "sudo ruby install.rb". Then move to the bin folder and run a "ruby wsdl2ruby.rb --wsdl myfile.wsdl --type client". Soap4R reads the WSDL and generates Ruby stub objects for you.

However, that's not all. Your client code needs to "require" the generated "service.rb" file and then create an instance of the stub client. However, passing complex parameters to your service is slightly more involved. wsdl2ruby generates a class that encapsulates the parameters - create one of them and pass that to the service.

service = MyService.new(url)
# include this line to show the raw XML packets
service.wiredump_dev = STDOUT
# call a simple method taking a single integer
puts service.SimpleMethod(some_int)
# call a complex method taking mutiple parameters
parameters = ComplexMethod.new(some_int, some_string)
response = service.ComplexMethod(parameters)

Structs come back via the response object and are a map of the XML response object. So, assuming ComplexMethod returns an array of ComplexTypes, you would access it via response.ComplexMethodResult.size to get the number of elements in the results.

I've not figured out how to pass structs in to the service yet.

No comments:


eXTReMe Tracker