[Awesome Ruby Gem] Use xmlrpc gem to call remote procedure over HTTP

xmlrpc

XMLRPC is a lightweight protocol that enables remote procedure calls over HTTP. It is defined at What is XML-RPC? - http://www.xmlrpc.com.

XMLRPC allows you to create simple distributed computing solutions that span computer languages. Its distinctive feature is its simplicity compared to other approaches like SOAP and CORBA.

The Ruby standard library package xmlrpc enables you to create a server that implements remote procedures and a client that calls them. Very little code is required to achieve either of these.

Starting in Ruby 2.4.0, the xmlrpc library that was before bundled with ruby has been extracted to a gem. So you must just add this to your Gemfile file and bundle again.

Installation

You can install it as a gem:

1
$ gem install xmlrpc

or add it into a Gemfile (Bundler):

1
2
3
4
5
# Gemfile

# GitHub - ruby/xmlrpc: The Ruby standard library package 'xmlrpc'
# https://github.com/ruby/xmlrpc
gem 'xmlrpc', '0.2.1'

Then, run bundle install.

1
$ bundle install

Example

Try the following code. It calls a standard demonstration remote procedure.

1
2
3
4
5
6
require 'xmlrpc/client'
require 'pp'

server = XMLRPC::Client.new2("http://xmlrpc-c.sourceforge.net/api/sample.php")
result = server.call("sample.sumAndDifference", 5, 3)
pp result

References

[1] GitHub - ruby/xmlrpc: The Ruby standard library package ‘xmlrpc’ - https://github.com/ruby/xmlrpc

[2] xmlrpc | RubyGems.org | your community gem host - https://rubygems.org/gems/xmlrpc/

[3] What is XML-RPC? - http://www.xmlrpc.com