ruby_web_services
ruby_web_services
What is SOAP ?
The Simple Object Access Protocol SOAP is a cross-platform and language-independent RPC
protocol based on XML and, usually butnotnecessarily HTTP.
It uses XML to encode the information that makes the remote procedure call, and HTTP to
transport that information across a network from clients to servers and vice versa.
SOAP has several advantages over other technologies like COM, CORBA etc: for example, its
relatively cheap deployment and debugging costs, its extensibility and ease-of-use, and the
existence of several implementations for different languages and platforms.
This tutorial will make you familiar to the SOAP implementation for Ruby SOAP4R. This is a basic
tutorial, so if you need a deep detail, you would need to refer other resources.
Installing SOAP4R:
SOAP4R is the SOAP implementation for Ruby developed by Hiroshi Nakamura and can be
downloaded from:
NOTE: There may be a great chance that you already have installed this component.
Download SOAP
If you are aware of gem utility then you can use following command to install SOAP4R and related
packages.
If you are working on Windows then you need to download a zipped file from the above location
and need to install it using standard installation method by running ruby install.rb.
This tutorial will give deatail on writing a stand alone server. There are following steps involved in
writing a SOAP server:
NOTE: If you want to write a FastCGI based server then you need to take SOAP::RPC::CGIStub as
parent class, rest of the procedure will remain same.
They can be written as simple Ruby methods. For example, let's write two methods to add two
numbers and divide two numbers:
# Handler methods
def add(a, b)
return a + b
end
def div(a, b)
return a / b
end
end
Paramter Description
receiver The object that contains the methodName method. you define the service
methods in the same class as the methodDef method, this parameter is self.
methodName The name of the method that is called due to a RPC request.
paramArg Specifies, when given, the parameter names and parameter modes.
To understand the usage of inout or out parameters, consider the following service method that
takes two parameters inParamandinoutParam, returns one normal return value retVal and two further
parameters: inoutParam and outParam:
add_method(self, 'aMeth', [
%w(in inParam),
%w(inout inoutParam),
%w(out outParam),
%w(retval return)
])
myServer = MyServer.new('ServerName',
'urn:ruby:ServiceName', hostname, port)
myServer.start
Paramter Description
ServerName A server name, you can give what you like most.
urn:ruby:ServiceName Here urn:ruby is constant but you can give a unique ServiceName
name for this server.
Example:
Now, using above steps, let us write one standalone server:
require "soap/rpc/standaloneserver"
begin
class MyServer < SOAP::RPC::StandaloneServer
# Handler methods
def add(a, b)
return a + b
end
def div(a, b)
return a / b
end
end
server = MyServer.new("MyServer",
'urn:ruby:calculation', 'localhost', 8080)
trap('INT){
server.shutdown
}
server.start
rescue => err
puts err.message
end
When executed, this server application starts a standalone SOAP server on localhost and listens for
requests on port 8080. It exposes one service methods, add and div, which takes two parameters
and return the result.
$ ruby MyServer.rb&
Following is the bare minimum information you would need to call a SOAP service:
Now, we will write a SOAP client which would call service methods defined in above example,
named add and div.
Paramter Description
nameSpace The namespace to use for all RPCs done with this SOAP::RPC::Driver object.
soapAction A value for the SOAPAction field of the HTTP header. If nil this defaults to the
empty string ""
driver.add_method(name, *paramArg)
Paramter Description
result = driver.serviceMethod(paramArg...)
Here serviceMethod is the actual web service method and paramArg... is the list parameters
required to pass in the service method.
Example:
Based on the above steps, we will write a SOAP client as follows:
#!/usr/bin/ruby -w
require 'soap/rpc/driver'
NAMESPACE = 'urn:ruby:calculation'
URL = 'http://localhost:8080/'
begin
driver = SOAP::RPC::Driver.new(URL, NAMESPACE)
Further Readings:
I have explained you just very basic concepts of Web Services with Ruby. If you want to drill down it
further, then there is following link to find more details on Web Services with Ruby.
Loading [MathJax]/jax/output/HTML-CSS/jax.js