What's the Best Way to Use Soap With Ruby

What's the best way to use SOAP with Ruby?

We used the built in soap/wsdlDriver class, which is actually SOAP4R.
It's dog slow, but really simple. The SOAP4R that you get from gems/etc is just an updated version of the same thing.

Example code:

require 'soap/wsdlDriver'

client = SOAP::WSDLDriverFactory.new( 'http://example.com/service.wsdl' ).create_rpc_driver
result = client.doStuff();

That's about it

Rails-The best way to use external SOAP API?

The current contenders for "best" library for consuming External SOAP services seems to be either Savon or Handsoap. There is a comparison between the two here

I can't comment on handsoap as I haven't used it by I am happy with Savon which is working well for me.

In terms of Application structure then I would create a folder under lib for the interface named after the external entity and then store files under there using the namespacing features of rails.

So an example I have an external interface to a system called Sentinel. So I have RAILS_ROOT/lib/sentinel and then all classes within that folder are declared within a Sentinel module.

What is the best way to send a SOAP request in Ruby on Rails?

Savonrb gem will be helpful for you in this case.

It is a ruby SOAP client to make SOAP communications easier. Also, check out this Railscasts episode to get started.

consume soap service with ruby and savon

This is a problem with the way Savon handles Namespaces. See this answer Why is "wsdl" namespace interjected into action name when using savon for ruby soap communication?

You can resolve this by specifically calling soap.input and passing it an array, the first element is the method and the second is a hash containing the namespace(s)

require 'rubygems'
require 'savon'

client = Savon::Client.new "http://www.webservicex.net/stockquote.asmx?WSDL"
client.get_quote do |soap|
soap.input = [
{ "xmlns" => "http://www.webserviceX.NET/" }
soap.body = {:symbol => "AAPL"}

SOAP in Rails3?

We're using Savon with Rails 3, works well for our use (Google Adwords API)

SOAP call in Ruby on Rails using Savon gets weird around the envelope and main operation

Ok this works (verified)

@client = Savon.client(
:wsdl => 'https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/?wsdl',
:log => true,
:wsse_auth => ['devc_!R4xc8p9', 'xxx'],
:pretty_print_xml => true,
:convert_request_keys_to => :camelcase,
:env_namespace => :s,
:namespace_identifier => nil

message = {
"d6p1:Message" => {
"d6p1:MessageID" => "10",
"d6p1:MessageTimeStamp" => Time.now.strftime("%d-%m-%Y %H:%M:%S")
"d6p1:Customer" => {
"d6p1:CustomerCode" => "DEVC",
"d6p1:CustomerNumber" => "11223344"},
"d6p1:Barcode" => {
"d6p1:Type" => "3S",
"d6p1:Range" => "DEVC",
"d6p1:Serie" => "1000000-2000000" }

attributes = { "xmlns:d6p1" => "http://postnl.nl/cif/domain/BarcodeWebService/",
"xmlns:i" => "http://www.w3.org/2001/XMLSchema-instance",
"xmlns" => "http://postnl.nl/cif/services/BarcodeWebService/"}

@client.call(:generate_barcode, :attributes => attributes,
:message => message,
:soap_header => { "Action" => "http://postnl.nl/cif/services/BarcodeWebService/IBarcodeWebService/GenerateBarcode"})

So the trick was adding :namespace_identifier => nil and sending attributes. Setting namespace_identifier removes the wsdl from GenerateBarcode ans attributes sets some namespaces on GenerateBarcode tag. Now I remember why I hate SOAP so much :(

Related Topics

Leave a reply