Question about classes in Rails

Let me start by stating the fact that I consider myself a ruby / rails
novice (above newb, just enought to be dangerous, perhaps … :slight_smile: )

I have an issue that relates to the ruby-openx gem, which allows ruby
to talk to an OpenX ad-server via XMLRPC, However, I think my problem
is kind of a more basic problem related to Rails, but I am sure it is
also probably due to the way that certain things are implemented in
the gem, so I’m hopeful I can get some basic guidance here on a
problem that has me stumped.

OpenX::Services::Base has a section of code that the other modules /
classes in the openx gem use when they extend this base class to
retrieve the connection to the OpenX server that looks like this:

@@connection = nil

def connection= c
@@connection = c
end

def connection
unless @@connection and @@connection.id
@@connection = Session.new(configuration[‘url’])
@@connection.create( configuration[‘username’],
configuration[‘password’]
)
end
@@connection
end

The connection is saved as a class variable, and evidently in Rails,
at least in production mode or when config.cache_classes = true,
classes appear to persist past an initial request so they don’t have
to be reloaded on every request.

The OpenX XMLRPC API expects authenticated sessions to pass back a
session id that is retrieved when you successfully login via the
XMLRPC API. The OpenX::Services::Session class handles this just fine.

However, what ends up happening is that the session times out due to
lack of activity, and the XMLRPC API does not provide a way to tell if
a session is expired or not, so it typically causes 500 internal
server errors in the Rails app that’s trying to communicate with
OpenX.

What I would like to do is basically have a private copy of the openx
classes that exists only during a rails request and do not persist
past the end of the request, independent of the config.cache_classes
setting. I believe this would solve the problem well enough for me to
be able to use as a work-around.

Is it possible to do in Rails what I am asking? I think the reason
this is not an issue with the library as written is that it was not
written with Rails usage in mind, but for usage with either a cron job
or a CLI ruby script instead; however, I have a need for it to operate
properly in a Rails environment.

I’m not keen on either re-writing the gem code, nor am I interested in
keeping the session alive via a cron job, as that’s a hack IMO, and
not a solution.

So, if anyone has any thoughts, I’d love to hear them. Thanks.

The gem’s code can be found at GitHub - aasmith/openx: A ruby interface the the OpenX XML-RPC interface, for
anyone who is curious.