Calling a .NET Web Service From Rails (original)
January 10, 2007
Posted by on
A lot of people have come to my blog by googling an older article I wrote describing how to call a .NET web service from Rails. I didn’t migrate any of my older posts over as part of my move to WordPress.com, but I’ll repost this article for those who want it. My only disclaimer is that the Rails platform has changed quite a bit and if you find any mistakes with this code in the current Rails codebase please leave a comment and I will update it appropriately. Thanks!
The Rails platform implements ActionWebService, a set of libraries used for pushing out Web services from your rails application. This seems to work relatively well, but most would agree that AWS leaves a bit to be desired in the area of consuming web services. This is where the Ruby SOAP::WSDLDriverFactory library comes into play.
In this quick example, I will use a stock quote service located here.
The nice thing about .NET webservices (which usually have the .asmx extension) is that you can call the webservice and see all the parameters from the exposed description to test things out.
So, here is the controller code I am using which calls this web service:
@security = Security.find(@params['id'])
factory = SOAP::WSDLDriverFactory.new("http://ws.invesbot.com/stockquotes.asmx?WSDL")
soap = factory.create_rpc_driver
soapResponse = soap.GetQuote(:symbol => @security.ticker)
price = soapResponse.getQuoteResult.stockQuote.price
@security.description = soapResponse.getQuoteResult.stockQuote.business
@security.current_price = strip_html(price)
In this example, I am creating an object called @security which holds the price and description of a particular stock, passing its ticker symbol to a web service, and then retrieving and saving the description and current price of the stock back to my original record.
Here is what the code does:
- Pulls up a particular Security record by referencing its ID.
- Creates a web service factory using the WSDL file of the web service.
- Opens an RPC driver to that web service factory.
- Calls a particular web service method (in this case GetQuote) from the driver and passes the current Security object’s ticker to the ‘symbol’ parameter of the web service.
- Retrieves the value of the stock’s description from the Soap object’s business field and store it in my Security object.
- Repeats the previous step for the current price, and strips off the HTML markup before storing it as a decimal data type.
- Saves the Security record.
- Resets the soap stream so it can be called again.
The part I hard the most trouble with was the “marshalling” of the SOAP object. I had assumed the SOAP response would come back as XML and require some manipulation, but in fact it came back as an easy-to-use soap object based on the XML data structure. I recommend using breakpointer or IRB and use the .methods property on the soap object to traverse its structure and find the values you are looking for.
Please leave any comments on this tutorial below.