old-www/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-ruby.html

278 lines
4.3 KiB
HTML

<HTML
><HEAD
><TITLE
>Using XML-RPC with Ruby</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.63
"><LINK
REL="HOME"
TITLE="XML-RPC HOWTO"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Using XML-RPC with Microsoft .NET"
HREF="xmlrpc-howto-dotnet.html"><LINK
REL="NEXT"
TITLE="Using XML-RPC with Proprietary Languages"
HREF="xmlrpc-howto-proprietary.html"></HEAD
><BODY
CLASS="section"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>XML-RPC HOWTO</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="xmlrpc-howto-dotnet.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="xmlrpc-howto-proprietary.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xmlrpc-howto-ruby"
>12. Using XML-RPC with Ruby</A
></H1
><P
><EM
>(This section of the XML-RPC HOWTO was generously
provided by Michael Neumann.)</EM
></P
><P
><A
HREF="http://www.ruby-lang.org/"
TARGET="_top"
>Ruby</A
> is an
object-oriented scripting language. It already has a major following in
Japan, and it's becoming popular elsewhere.</P
><P
>To use XML-RPC with Ruby, you must first install Yoshida Masato's
xmlparser module (a wrapper for James Clark's expat parser). This can
be found at the <A
HREF="http://www.ruby-lang.org/en/raa.html"
TARGET="_top"
>Ruby
Application Archive</A
>.</P
><P
>Then, you must install <A
HREF="http://www.s-direktnet.de/homepages/neumann/xmlrpc4r/index.html"
TARGET="_top"
>xmlrpc4r</A
>
using the following commands:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>bash$ tar -xvzf xmlrpc4r-1_2.tar.gz
bash$ cd xmlrpc4r-1_2
bash$ su root -c "ruby install.rb"</PRE
></TD
></TR
></TABLE
><DIV
CLASS="section"
><H2
CLASS="section"
><A
NAME="xmlrpc-howto-ruby-client"
>12.1. A Ruby Client</A
></H2
><P
>Here's a simple Ruby client:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>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}"</PRE
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="section"
><H2
CLASS="section"
><A
NAME="xmlrpc-howto-ruby-server"
>12.2. A Ruby Server</A
></H2
><P
>Here's a simple Ruby server:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>require "xmlrpc/server"
s = XMLRPC::CGIServer.new
s.add_hanlder("sample.sumAndDifference") do |a,b|
{ "sum" =&#62; a + b, "difference" =&#62; a - b }
end
s.serve</PRE
></TD
></TR
></TABLE
><P
>This could also have been written as follows:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>require "xmlrpc/server"
s = XMLRPC::CGIServer.new
class MyHandler
def sumAndDifference(a, b)
{ "sum" =&#62; a + b, "difference" =&#62; a - b }
end
end
s.add_handler("sample", MyHandler.new)
s.serve</PRE
></TD
></TR
></TABLE
><P
>To run either server in standalone mode, replace the second line
of code with the following:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>s = XMLRPC::Server.new(8080)</PRE
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="xmlrpc-howto-dotnet.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="xmlrpc-howto-proprietary.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Using XML-RPC with Microsoft .NET</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Using XML-RPC with Proprietary Languages</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>