Programming
Rails 3 and Web Services
July 21, 2012
0

Hello to everyone,

Working for a new company brings a lot of new excitement and also a lot of new stuff to learn; now it’s Rails 3 and Ruby moment… So, because of a project I’m following, I had the need to learn how to get something out of a web service using Ruby… Well, it’s really easy and simple and I want to share with you what I’ve learned.

First of all install a nice gem called Savon using the following command :

     gem install savon

This gem gives you a whole set of methods to call a web service and use its responses.

Open your favourite text editor and start coding this basic usage example:

require "savon"
client = Savon.client("http://www.webservicex.net/globalweather.asmx?WSDL")

As you can see, after telling Ruby that you want to use Savon, you need to istantiate a client passing to Savon client method the WSDL address of the web service you want to use.

After this, lets the client object sends a request to the web service , suppressing the SOAPAction header, since the free web service I’m using in this example, doesn’t need it; setting up a request using a do/end als let’s you set up some nice stuff like the entire envelope you need.

response = client.request "GetCitiesByCountry" do
	http.headers.delete("SOAPAction")
	soap.xml =  "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:web='http://www.webserviceX.NET'>
			   <soapenv:Header/>
			   <soapenv:Body>
			      <web:GetCitiesByCountry>
				 <!--Optional:-->
				 <web:CountryName>italy</web:CountryName>
			      </web:GetCitiesByCountry>
			   </soapenv:Body>
		    </soapenv:Envelope>"
end

puts response.to_hash

Now try this small example and you will see how powerful Savon is and how many features it has. I will, for sure, study this gem in deep and give you some feedback about it. Up to now here is the reference

Savon

Enjoy and share!!!

Leave a Reply

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close