XML_RPC

Some links:

Using XML-RPC with ruby

You will want to get xmlrpc4r. You can do this from: xmlrpc4r homepage (fantasy coders)

Before you can build the above, you will also need an XML parser (use Yoshida Masato's xmlparser -- which is a wrapper for the expat parser written by James Clark). Get this from RAA, then do

untar it
cd xmlparser
ruby extconf.rb
make
su
make site-install
This worked fine without any extra options, finding the expat header in /usr/include and the library in /usr/lib on my fedora system. After the following one line ruby script confirms that xmlparser is on my system.
require "xml/parser"

Now you can build xmlrpc4r. I had to edit one line in xmlrpc4r/lib/config.rb and select XMLStreamParser instead of NQXMLStreamParser. NQXML is a pure ruby XML parser, but I have never used it. So do this: This was simple, get the tar ball, untar it, su to root, and then ruby install.rb (or equivalently:)

untar it
cd xmlrpc4r
cd lib
edit config.rb as described
cd ..
su
ruby install.rb

After this the following client script will run:

#!/usr/bin/ruby

require "xmlrpc/client"

# Make an object to represent the XML-RPC server.
server = XMLRPC::Client.new( "xmlrpc-c.sourceforge.net", "/api/sample.php")

# Call the remote server and get our result
result = server.call("sample.sumAndDifference", 5, 3)

sum = result["sum"]
difference = result["difference"]

puts "Sum: #{sum}, Difference: #{difference}"

From here, take a look at the xmlrpc4r HOWTO

A simple server is as follows:

#!/usr/bin/ruby

require "xmlrpc/server"

#s = XMLRPC::CGIServer.new
s = XMLRPC::Server.new(8080)

class MyHandler
  def sumAndDifference(a, b)
    { "sum" => a + b, "difference" => a - b }
  end
end

s.add_handler("sample", MyHandler.new)
s.serve

A client to talk to it is:

#!/usr/bin/ruby

require "xmlrpc/client"

# Make an object to represent the XML-RPC server.
server = XMLRPC::Client.new( "localhost", "RPC2", 8080)

# Call the remote server and get our result
result = server.call("sample.sumAndDifference", 5, 3)

sum = result["sum"]
difference = result["difference"]

puts "Sum: #{sum}, Difference: #{difference}"

Feedback? Questions? Drop me a line!

Uncle Tom's Computer Info / [email protected]