Creating A Basic Web Service using ActionWebService
This HowTo will take you through the basic steps to get a working web service up and running on Rails using ActionWebService.
Before you start make sure your install of Rails and ActionWebService are up to date. This code was written with Rails 1.0, so it should be alright with that or anything later.
Decide on the name for your service – in this example we will call it “helloservice” and create the rails directory by going to the directory you want to create it in and typing “rails helloservice”.
Once rails has finished creating the scripts and directories you need to create your service, this will require two classes, a controller containing the code that the service makes available to clients and a API description that allows Rails to create a WSDL file for you.
Here is the API code:
class UsefulSumApi < ActionWebService::API::Base api_method :useful_sum, :expects => [:int, :int], :returns => [:int] end
The Controller looks like this:
class UsefulSumController < ApplicationController
web_service_dispatching_mode :direct
wsdl_service_name 'useful_sum'
web_service_scaffold :invoke
def useful_sum(parameter1, parameter2)
return parameter1 + parameter2
end
end
This needs to be saved in helloservice/app/controllers as useful_sum_controller.rb.
Now just start your server by calling “ruby script/server” from the helloservice directory and you should be able to see the wsdl file at http://localhost:3000/useful_sum/wsdl or view a test page at http://localhost:3000/useful_sum/invoke that lets you add parameters and test the service.
Note: The URL to call from client applications is http://localhost:3000/useful_sum/api (must provide web_client_api helper method declaration in the controller with url)
For more information see the ActionWebService Manual