Accessing a SOAP Web Service Using Ruby on Rails



I was recently working with a ColdFusion application that needed to access a SOAP Web Service and decided to code something similar with Ruby on Rails. As usual, I was expecting the process to be a bit more cumbersome than CF. I say this largely because I haven’t dealt with any scripting languages that make it easier than CF (we’re talking less than three lines of code to call a basic WS). Once again I was completely suprised with how easy Rails makes this. The following is a brief overview of my experience.

To demonstrate accessing a basic Web Service from Rails I decided to use the XMethods delayed stock quote service. I decided the user would be able to pass a URL parameter to the app and have the quote displayed on a page. The request would look something like http://yourdomain.com/stock/quote?symbol=aapl where stock is the controller name and quote is the action (or method). Once the request is made, the magic happens and the current quote price is displayed.

Let’s assume you’ve created your Rails application shell and have added a controller named stock_controller.rb. In this file you need to create a method called quote that resembles the following:

class StockController < ApplicationController
require 'soap/wsdlDriver'
def quote
soap_client = SOAP::WSDLDriverFactory.new("http://services.xmethods.net/soap/ urn:xmethods-delayed-quotes.wsdl")
driver = soap_client.createDriver
@price = driver.getQuote(params[:symbol])
end
end

This code basically loads the SOAP driver, creates an instance of it, and then calls the getQuote method of the service. The symbol parameter is passed on to the getQuote method and the result is stored in an instance variable called price. Creating an instance variable will enable us to display it within the view.

The view responsible for displaying the stock information is located in app/views/stock/quote.rhtml. The code basically looks like the following:

The current stock price for <%=@params[:symbol]%> is <%=@price%>.

Now accessing the URL mentioned above (or one that applies to your environment) yields the following output:

The current stock price for aapl is 67.99.

That’s the price at the time of this writing and let’s hope it continues to climb! As you can see, accessing SOAP services using Rails is a very trivial task. This covers a somewhat basic example and I’ll try to post some more in-depth stuff in the coming weeks.



4 Responses to “Accessing a SOAP Web Service Using Ruby on Rails”

  1. SangDoo says:

    class CurrencyController .
    _____________________________________________
    I’ve got an error:

    ArgumentError in CurrencyController#theWord

    wrong number of arguments (1 for 2)

    RAILS_ROOT: ./script/../config/..

    C:/ruby/lib/ruby/1.8/soap/wsdlDriver.rb:549:in `NumberToWords’
    #{RAILS_ROOT}/app/controllers/currency_controller.rb:11:in `theWord’
    -e:3
    _______________________________________

    Any help will be appreciated. Thanks!

  2. dennis says:

    You may have to post more of your code to be able to determine what’s wrong. It appears to be having problems converting the current stock price to a certain format within your app. What does your CurrencyController look like?

  3. Ananda Putra says:

    Hi,
    I’ve try your sample, but it did’nt work since there is a proxy in my network.
    How to config the proxy in your sample?

    Thanks.

  4. dennis says:

    Good question, I haven’t had to deal with a proxy in this scenario but will see what I can find out.

Leave a Reply