Consuming SOAP Webservices in Ruby

Consuming soap webservices in ruby is dead simple. I will show to do it with Savon a great ruby library to work with soap webservices.

For testing purposes I will use this public soap webservices that give me information about the periodic table.

In order to install Savon just type in your command line:
$ gem install savon

After installing Savon, lets try some code
require 'rubygems'
require 'savon'

client = do
  wsdl.document = ""

puts client.wsdl.soap_actions

response = client.request :wsdl, "get_element_symbol" do
 soap.body = {
   :ElementName => "Zinc"

In the code showed above I'm importing savon and rubygems as well(so I can ruby without command line arguments) and I'm creating the SOAP client, when I do that I tell savon where is the wsdl through wsdl.document.

The method client.wsdl.soap_actions provide me a list of all soap operations available for this webservice(this is quite useful), so this will be printed on your console.

For least but not least I'm invoking the soap operation called get_element_symbol, you may notice that i pass the parameter ElementName in the soap body with the value Zinc, this is required by this webservice wsdl.

When you run the code you will be something like this(I'm using Aptama RedRails IDE :D)
D, [2011-02-21T01:18:16.611034 #10364] DEBUG -- : Retrieving WSDL from:
D, [2011-02-21T01:18:18.137121 #10364] DEBUG -- : HTTPI executes HTTP GET using the httpclient adapter
D, [2011-02-21T01:18:24.754500 #10364] DEBUG -- : SOAP request:
D, [2011-02-21T01:18:24.755500 #10364] DEBUG -- : SOAPAction: "http://www.webserviceX.NET/GetElementSymbol", Content-Type: text/xml;charset=UTF-8
D, [2011-02-21T01:18:24.757500 #10364] DEBUG -- : Zinc
D, [2011-02-21T01:18:24.768501 #10364] DEBUG -- : HTTPI executes HTTP POST using the httpclient adapter
D, [2011-02-21T01:18:31.420881 #10364] DEBUG -- : SOAP response (status 200):
D, [2011-02-21T01:18:31.422881 #10364] DEBUG -- : <NewDataSet>





As you can see Savon is simple and cool like everything in Ruby :D


Popular posts from this blog

Podman in Linux

Java Agents

Manage Work not People