Soap Client for Ruby 1.9 and Rails

soap client for ruby 1.9 and rails

Savon abstracts the XML part into a Ruby Hash. Have a look: http://github.com/rubiii/savon

Using SOAP and other Standard Libraries in Ruby 1.9.2

If you want to keep using Ruby 1.8's standard soap library (aka soap4r), you can try https://github.com/spox/soap4r-spox ...

wget --no-check-certificate https://github.com/spox/soap4r-spox/tarball/1.5.8.4
tar -xzf spox-soap4r-spox-1.5.8.4-0-g345a6cb.tar.gz
cd spox-soap4r-spox-345a6cb/
ruby setup.rb all

If you're using rvm, don't sudo the last command... instead su into root and rvm to ruby 1.9 so that setup.rb puts the files into the right place.

$ irb
ruby-1.9.2-p0 > require 'soap/rpc/driver'
=> true
ruby-1.9.2-p0 > require 'xsd/qname'
=> false
ruby-1.9.2-p0 > require 'soap/wsdlDriver'
=> true
ruby-1.9.2-p0 > require 'fileutils'
=> true

As suggested by other StackOverflow answers, you might want to switch to a gem like savon.

Tools for development SOAP-service on Ruby 1.9

Take a look at http://aws.rubyonrails.org/, when it's explained how to create web services within RubyOnRails.

How do I move from SOAP:RPC:Driver to Savon

When not using a WSDL one must set up wsdl explicitly (I find this a little counter-intuitive, but it works):

require 'savon'
client = Savon::Client.new do
wsdl.endpoint = 'http://example.com/endpoint'
wsdl.namespace = 'MY_NAMESPACE';
end

I don't believe there is an equivalent to add_method in Savon, but you should then be able to make a request thus:

response = client.request 'MyMethod' do
soap.body = { 'Arg1' => arg_one' }
end

However when I did this I received the following error:

Savon::SOAP::Fault: (SOAP-ENV:Client) SOAPAction shall match 'uri#method' if present 

I was able to work around this by explicitly setting SOAPAction:

response = client.request 'MyMethod', soap_action: '#MyMethod' do ...

Finally I got this error:

Savon::SOAP::Fault: (SOAP-ENV:Client) Denied access to method (MyMethod) in class (main) at /usr/lib/perl5/site_perl/5.8.8/SOAP/Lite.pm line 2128.

This seems to be because the given wsdl.namespace isn't passed as expected with the request. I was able to resolve this by explicitly setting it in both soap_action and xmlns:

response = client.request 'MyMethod', soap_action: 'MY_NAMESPACE#MyMethod', xmlns: 'MY_NAMESPACE'

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 = [
"GetQuote",
{ "xmlns" => "http://www.webserviceX.NET/" }
]
soap.body = {:symbol => "AAPL"}
end


Related Topics



Leave a reply



Submit