Modifying ActiveResource classes

I’ve been struggling with an issue with ActiveResource for a bit now:
when a hostname resolves for an ActiveResource request, but there’s no
server on the other end to return information, ActiveResource’s timeout
value doesn’t work. The request just hangs.

After reviewing the ActiveResource code, I’ve realized that this is
because the underlying Net:Http object only has one timeout value set:
read_timeout. The Net:Http library defines this as “Seconds to wait
until reading one block (by one read(2) call)”. The Net:Http lib also
defines another timeout value, open_timeout, which is defined as
“Seconds to wait until connection is opened”.

I’m unsure why open_timeout isn’t set by default with the timeout value
set on an ActiveResource class, but after modifying the
ActiveResource::Connection class to include the open_timeout on the http
objects, my issue was resolved!

I’m new to rails so I’m unsure of the best way to actually make this
modification in my project; I don’t want to just change the code in my
gem directory. Is there a proper way to make these modifications in a
rails project? I’ve seen that it’s possible to load rails classes from
the /vendor folder, but do they all have to be there for it to work? I
started to make subclasses of the ActiveResource::Base and
ActiveResource::Connection classes, but it seemed like maybe there was
an easier way to do this, as the function that creates the Net:Http
instance is private…any thoughts?

So i’ve come to realize that this can be fixed by placing the following
in an initializer file under config/initializers:

class ActiveResource::Connection

  # Creates new Net::HTTP instance for communication with
  # remote service and resources.
  def http
    http             = Net::HTTP.new(@site.host, @site.port)
    http.use_ssl     = @site.is_a?(URI::HTTPS)
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
    http.read_timeout = @timeout if @timeout # If timeout is not 

set, the default Net::HTTP timeout (60s) is used.
http.open_timeout = @timeout if @timeout #I added this line
http
end
end

However, I’m not really sure I understand how this works. The function
I’m placing here apparently overrides the same-named function in
ActiveResource::Connection, but that function is private, so I’m unsure
as to how there’s no errors? I’m not finding much online about how you
can use initializers to override base rails classes.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs