I'm playing around with a ruby app using XML-RPC. Currently I'm using
global variables so I can create multiple objects without having to log
in for each and every object.
def initialize(user, pass)
$user, $pass = user, pass
$client = XMLRPC::Client.new(...)
$sessionid = $client.call("xmlrpc.method", $user, $pass)
Derived classes look like this:
class Object1 < Base
if not $sessionid then self.login end
This way I can log in once with the first object created without having
to log in each time. However, this seems inelegant to me. Is there a
more elegant, or just more rubyish, way of doing this? Also, I'd have
potential problems if I wanted to log on as multiple users.
Would it make more sense just to pass around an instance of the Base object:
if not xmlrpc_client.sessionid then xmlrpc_client.login
def call_xmlrpc_method(method, params)
results = xmlrpc_client.call(method, params)
xmlrpc_client = Base.new
o2 = Object2.new(xmlrpc_client)
Pro: no global variables. Con: coupling between Object2 and Base classes.