ActionWebService with Rails 2.2

08 Feb 2009

I encountered a trick for young players the other day when providing XML-RPC web service support within a Rails application.

Since Rails 2.0 was introduced, RESTful web services via ActiveResource have been supported in preference to SOAP or XML-RPC web services. Indeed, ActionWebService was dropped from Rails when version 2.0 was released.

However, there are times when the need to provide an interface to another application mandates the use of SOAP or XML-RPC. Recently I had a need to provide an XML-RPC web service server within a Rails application. Fortunately, a version of ActionWebService has been kept up to date on GitHub together with instructions for implementing it.

As I was developing my support for XML-RPC calls, I found chapter 25 of Thomas and Hansson’s “Agile Web Development with Rails” (second edition) invaluable.

For simplicity’s sake, let’s say my controller was:

class BackendController < ApplicationController  
  wsdl_service_name 'Backend'  
  web_service_api BackendApi  
  web_service_scaffold :invoke if Rails.env == 'development'

  def foo_bar(args)  
    # method code goes here  
  end  
end  

And here is an XML-RPC test client:

require 'xmlrpc/client'  
require 'pp'

server = XMLRPC::Client.new2("http://example.com/backend/api")  
result = server.call("FooBar", \["arg1", "arg2", "arg3"\])

pp result  

Notice that the call is to a method called “FooBar” whereas the method within the Rails controller is called “foo_bar”. If you’re like me you will have originally fallen into the trap of assuming that the remote call will have the same name as the controller method rather than the camel case equivalent.

Other posts

Previous post: Automated Testing with Ruby

More recently: Impressed with Prawn for PDF Generation

© 2024 Keith Pitty, all rights reserved.