XMLRPCS gem published

June 26, 2008

I have just published on rubyforge the gem for my xmlrpc modifications.
It’s called xmlrpcs and contains the class discussed in the previous post.

xmlrpcs Rubyforge homepage
Online RDoc

The changes are that now it has full rdoc comments and the path to include it is ‘xmlrpc/xmlrpcs’
I would like to relase my upnp binding as a gem too, but that will come later.
To install it just do

gem install xmlrpcs

and it’s done


Ruby XMLRPC sockets changes

June 25, 2008

[Continues: Ruby xml-talking to rtorrent]

I have rewritten how i changed the socket in XMLRPC are modified in a more Ruby style way.

A new class, in XMLRPC module, called ClientS (in a file called xmlrpcs.rb). It is a subclass of the Client class to ease the implementation of other kind of trasport system if the Net::HTTP based one doesn’t fit your needs.

require 'xmlrpc/client'

class XMLRPC::ClientS < XMLRPC::Client
        def initialize(info)
            @info = info
        end

        private

        # create new socket
        def new_socket(info,async)
            raise "Must be subclassed"
        end

        # write xmlrpc request in the previously created socket
        def write_request(socket,request)
          if socket.write(request) != request.length then
              raise "Not all the data has been sent"
          end
        end

        # read response from the socket
        def read_response(socket)
            socket.read()
        end

        # do_rpc working with custom sockets
        def do_rpc( request, async )
          sock = new_socket(@info,async)
          write_request(sock,request)
          return read_response(sock)
        end
    end
end

This makes it very simple to extend the class by just subclassing ClientS or just redefining the function new_socket.
To avoid to close the socket at each request, which may be not the right choice for every code, you can redefine the do_rpc
The info field is used to pass some arguments to create the socket, for instance

require 'xmlrpcs'
require 'socket'

class XMLClient < XMLRPC::ClientS
    def new_socket( info, async )
        UNIXSocket.new(info)
    end
end

c=XMLClient.new("/path/to/sock")
c.call(..)

The code can be found in the code page

[Continued by: XMLRPCs gem published]