Wednesday, December 27, 2006

Ruby 4 Lines of Web service/SOAP Code Magic

This is amazing to see how easy and simple it is to write a SOAP code in ruby, have a look at the following code. This does call Google search engine by passing some random search query.

require 'soap/wsdlDriver'
factory = SOAP::WSDLDriverFactory.new("http://api.google.com/GoogleSearch.wsdl")
service = factory.create_rpc_driver
@service_output = service.doGoogleSearch("xxxxxxxxxxxxxx", "Ajuby", 0, 10, "false", "", "false", "", "latin1", "latin1")

That's it.. @service_output returns what I call it as "Ruby's intelligent data structure" which you can use to get respective data output

e.g.
- Results: <%=@service_output.estimatedTotalResultsCount%>
- searchTime: <%=@service_output.searchTime%>


Note: Above "xxxxxxxxxxxxx" is the api key that you need to obtain from Google to use their web services, its less than a minute job to get that from google.

Now there is a better and more modular way of writing above code using ActionWebService
where you can manage API's to access outgoing SOAP calls more effectively.

No comments: