The other day I needed to integrate with Lyris ListManager using their API. Their API uses SOAP and my application is on Rails. I started looking for information about Ruby/Rails + SOAP integration and found this article first: http://wiki.rubyonrails.org/rails/pages/HowToUseSOAP4RWithRails
That’s how I started. Assuming that you have a running SOAP service on mylistmanager.com:82, here are the steps I took:
1. Install soap4r’s gem (if you don’t have it already)
[etagwerker@benteveo]$ sudo gem install soap4r
Password:
Successfully installed httpclient-2.1.3.1
Successfully installed soap4r-1.5.8
2 gems installed
Installing ri documentation for httpclient-2.1.3.1…
Installing RDoc documentation for httpclient-2.1.3.1…
Unfortunately, that rubyonrails.org article was not clear enough for what I wanted to do. Basically, I want to consume a SOAP service. So I needed to create a SOAP client stub that I could use from my application. After googling for information on wsdl2ruby, I found this really good article: http://www.brendonwilson.com/blog/2006/04/02/ruby-soap4r-wsdl-hell
2. Generate your client stub with wsdl2ruby
[etagwerker@benteveo]$ wsdl2ruby.rb –wsdl http://mylistmanager.com:82/?wsdl –type client –force
I, [2009-02-11T11:06:26.012941 #7276] INFO — app: Creating class definition.
I, [2009-02-11T11:06:26.013108 #7276] INFO — app: Creates file ‘lmapi.rb’.
I, [2009-02-11T11:06:26.117915 #7276] INFO — app: Creating mapping registry definition.
I, [2009-02-11T11:06:26.118102 #7276] INFO — app: Creates file ‘lmapiMappingRegistry.rb’.
I, [2009-02-11T11:06:26.234855 #7276] INFO — app: Creating driver.
I, [2009-02-11T11:06:26.235469 #7276] INFO — app: Creates file ‘lmapiDriver.rb’.
I, [2009-02-11T11:06:26.308501 #7276] INFO — app: Creating client skelton.
I, [2009-02-11T11:06:26.308847 #7276] INFO — app: Creates file ‘lmapiClient.rb’.
I, [2009-02-11T11:06:26.321677 #7276] INFO — app: End of app. (status: 0)
This generated a bunch of Ruby files that are very useful for the integration. Basically, it makes everything more simple. You don’t need to worry about SOAP messages, responses, etc.. You just use the classes in that stub to interact with your SOAP service.
3. Move the client stub files to your /lib directory (in your Rails project)
This will make your stub client classes accessible to your application
4. Add soap4r to your boot.rb file
I don’t know where I found this step, but I was getting an error with SOAP when trying to use SOAP classes form my application. Your boot.rb should look like this:
def load_rubygems
require ‘rubygems’
gem ’soap4r’
Basically, I added gem ’soap4r’ which imports the SOAP classes in my application.
5. Fire up your Rails console (./script/console in your Rails project) and test it!
Luckily, I found a really thorough example here: http://markthomas.org/2007/09/12/getting-started-with-soap4r - However, if you look at the files you generated with wsdl2ruby, you will find a lot of useful and descriptive information (as code and comments)
To test it, I include the lmapiDriver.rb (present in the /lib directory), I create a driver, I give it the user credentials and I start using it (selectLists and apiVersion are both Lyris SOAP API methods)
require “lmapiDriver.rb”
driver = LmapiSoap.new
#Add authentication
wsdl = “http://mylistmanager.com:82/?wsdl”
user = “yourusername”
pass = “yourpassword”
driver.options["protocol.http.basic_auth"] << [wsdl,user,pass]
# Print the name of all lists
lists = driver.selectLists(”,”)
driver.apiVersion
# If it all worked out fine, you should see:
# => “10.2″
I found this integration really simple. wsdl2ruby.rb is an amazing tool for speeding things up.