ben robison
when only more words will do
Howto: Put WSDL on Rails [Part 2]
In the first article about WSDL on Rails we created our own web service that accepted a string, modified it, and returned a new string. Now we’ll take a look at creating another application that can consume or subscribe to our web service. This is almost too easy. I figure the best way to demonstrate the simplicity is to keep this article as short as possible.
1. Fire up the old WebServiceProvider application that we created in Part 1
From the application root directory type ./script/server
2. Create a new WebServiceConsumer application
In a new terminal window type rails WebServiceConsumer
3. Create a controller for your service consumer
cd WebServiceConsumer
./script/generate controller consumer
4. Subscribe to your WebServiceProvider using its WSDL
Open up your consumer_controller.rb in app/controllers. It will be empty and it needs to look like this:
class ConsumerController < ApplicationController
require 'soap/wsdlDriver'
def subscribe
if params[:terms] == nil
@service_output = “”
else
url = “http://localhost:3000/subscription/service.wsdl”
factory = SOAP::WSDLDriverFactory.new(url)
service = factory.create_rpc_driver
@service_output = service.Subscribe(params[:terms])
end
end
end
This is where the magic happens. The line require 'soap/wsdlDriver' brings in the needed Ruby library. The subscribe method is simply an action like any other in your application. We’re going to create a form that accepts some user input and then we’ll pass them on to our WebServiceProvider. The url, factory, and service lines could all be done in one line, but I’ve broken them out here to demonstrate.
If the box is left blank, we don’t do anything. If the user inputs some data, then we have a little bit of work to do. We create a new service object based on the WebServiceProvider’s description of itself as defined by its WSDL. Then our local service object acts as a proxy and any method calls we make on the local object are simply passed on to it the actual WebServiceProvider.
We can then define our @service_output as a variable we can then call in our view. Now for testing.
5. Create two views for testing
In app/views/consumer create an index.rhtml file as follows:
<html>
<head>
<title>WebServiceConsumer</title>
</head>
<body>
<h1>WebServiceConsumer</h1>
<p>
Enter your terms below. They will be passed on to our web service and you'll be able to see the response.
</p>
<%= start_form_tag :action=> 'subscribe' %>
<p><label>Terms</label><br/>
<%= text_field 'terms', '' %></p>
<%= submit_tag "Subscribe" %>
<%= end_form_tag %>
</body>
</html>
Also in app/views/consumer create subscribe.rhtml as follows:
<html>
<head>
<title>WebServiceConsumer</title>
</head>
<body>
<h1>WebServiceConsumer</h1>
<p>
The web service returned: <br /><br />
<%= @service_output %>
</p>
</body>
</html>
6. Now fire up your WebServiceConsumer app on a different port
Depending on your particular setup this may be different. I use lighttpd, so you’ll need to open app/config/lighttpd.conf and change the server.port = 3000 to say something like server.port = 3100. Then you can run ./script/server to fire up your app on port 3100.
Now point your browser to http://localhost:3100/consumer
Enter your terms into the text box, click Subscribe, and pat yourself on the back. You’ve just subscribed to a web service.
Subscribe to RSS
doesn’t work for me