Implement a SOAP-Based Web Service
First, you write a simple
SOAP client. You frequently will want to access other services that support a SOAP interface, so all you need to know is how to write clients. (However, you shortly will see how to also write SOAP services in Ruby.)
The Ruby built-in SOAP library automatically marshals values in Ruby variables to and from XML. The following client accesses the services "upper_case
", "
lower_case", and "
times_string" from a server that supports SOAP and implements these services:
Listing 9
require 'soap/rpc/driver'
stub = SOAP::RPC::Driver.new("http://127.0.0.1:9090", "http://markwatson.com/Demo")
stub.add_method('upper_case', 'a_string')
stub.add_method('lower_case', 'a_string')
stub.add_method('times_string', 'a_string', 'a_number')
puts stub.lower_case("Jack and Jill went up the hill.")
puts stub.upper_case("Jack and Jill went up the hill.")
puts stub.times_string("Jack and Jill went up the hill.", 2)
The code specifies that the SOAP server is at IP address 127.0.0.1 (localhost) and listens for connections on port 9090. It specifies "http://markwatson.com/Demo" as a name space (must match what the server uses).
The server requires a class that implements the exposed methods "upper_case", "lower_case", and "times_string"; so you first implement DemoClass, which has these three methods:
Listing 10
require 'soap/rpc/standaloneserver'
# define a class that has three methods to call remotely:
class DemoClass
def upper_case(a_string)
a_string.upcase
end
def lower_case(a_string)
a_string.downcase
end
def times_string(a_string, a_number)
a_string * a_number
end
end
# create a SOAP enabled server for the methods in DemoClass:
class MyServer < SOAP::RPC::StandaloneServer
def on_init
demo = DemoClass.new
add_method(demo, "upper_case", "a_string")
add_method(demo, "lower_case", "a_string")
add_method(demo, "times_string", "a_string", "a_number")
end
end
server = MyServer.new("Demo", "http://markwatson.com/Demo", "0.0.0.0", 9090)
trap('INT') { server.shutdown }
server.start
In Listing 10, after the code defines DemoClass for implementing the Web services, it sub-classes the SOAP standalone server class; the class
on_init method adds methods to the class.
If you have ever written SOAP clients and services in other programming languages, you will appreciate how easy it is to use Ruby's SOAP libraries.
The Wrap-Up
For most applications, I like to use XML-RPC because it is lighter weight than SOAP and has implementations for more programming languages. REST is great for some applications that map well to a function-type call (think of the query URL as a function followed by arguments) returning an XML payload.
This article contained a lot of code, but hopefully you both understood the examples and ran the example code snippets without any problems. Want to go further with network programming in Ruby? Review the Related Resources section in the left column for more detailed information on the topics this article lightly covered.