advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Average Rating: 5/5 | Rate this item | 3 users have rated this item.
 Print Print
Ruby Programming Language Enables Concise Network Programming (cont'd)

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

advertisement
", "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.

Page 4 of 4
advertisement
Mark Watson is a Java consultant and the author of 14 books on Java, artificial intelligence, C++, and intelligent agents.
Previous Page: Implement an XML-RPC-Based Web Service  
Page 1: IntroductionPage 3: Implement an XML-RPC-Based Web Service
Page 2: Implement a REST-Based Web ServicePage 4: Implement a SOAP-Based Web Service
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES