consume web services with a rails app?

I want to do a proof-of-concept showing how to consume data from an xml web service provided by a partner company.

I have had a lot of difficulty finding examples of consuming xml web services (plenty of info on providing xml web services).

I'm looking for the correct way to architect such a web app, using MVC and Rails as I will not be requiring a database on my side of the fence.

What you're going to want to use is a library called soap4r, it has another library called wsdl2ruby.

http://dev.ctor.org/soap4r (read it all, but there is a long list of good how-to's)

It will generate some files for you that make it easier to consume the web service...

There should be 4 files and you'll need to require things at the top of each. I wanted to be able to run my test ruby script as well as my rails implementation concurrently (well without changing the require statements every time anyway) so I did something like this...

require File.expand_path(File.dirname(__FILE__) + "../../../config/ environment") require "#{RAILS_ROOT}/lib/soap/customer_details.rb" require "#{RAILS_ROOT}/lib/soap/customer_detailsMappingRegistry.rb" require 'soap/rpc/driver'

class WsGetCustAcctDtls < ::soap::RPC::Driver

Things are put here from wsdl2ruby...

And after that you just need to be able to query your web service. This is what my WSDLNameClient.rb file (the ruby file that wsdl2ruby generates) looks like:

http://pastie.textmate.org/private/u1yw8kyfi7ysopnh5mwrzq

The authentication stuff in there was specific to the web service I was consuming, I had to build up a header and pass it in, etc... it was a pita.

I'm not an expert, but I did end up getting it working after a lot of googling and playing around with it. Here is an article that helped me. but check out all the how-to's in that first link.

http://www.brendonwilson.com/blog/2006/04/02/ruby-soap4r-wsdl-hell/

Hopefully knowing the libraries you're supposed to be using will help your googling too. Good luck!

-Jon Kinney Inacom Information Systems Ruby on Rails Developer

Kind of depends on what you really mean by "xml web service".

If they're exposing a SOAP interface, then you probably want to use the soap4r gem.

If they're exposing a interface according to the XML RPC standard, Ruby has built-in support for that too: http://ruby-doc.org/stdlib/libdoc/xmlrpc/rdoc/index.html

If they're exposing a REST XML service according to Rails conventions :-), then you can use ActiveResource directly inside of your Rails app. The interface is similar (but a subset) of ActiveRecord, so it will feel pretty natural inside of a Rails app.

If it's a proprietary XML standard, then using something like XmlSimple (which Rails includes by default) let's you slurp up an XML doc into a hash pretty easily, and you can attack it from there.

Hope this helps?

Jeff

softiesonrails.com purpleworkshops.com

I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the comment here! http://www.dealsourcedirect.com/toy-story-lamp.html