# Modifying ActiveResource classes

**URL:** <https://www.ruby-forum.com/t/modifying-activeresource-classes/172635>\
**Category:** Rails\
**Created:** [July 24, 2009, 11:10am UTC](https://www.ruby-forum.com/t/modifying-activeresource-classes/172635 "2009-07-24T11:10:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bencahen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecb155/32.png) [@bencahen](https://www.ruby-forum.com/u/bencahen)\
**Post date:** [July 24, 2009, 11:10am UTC](https://www.ruby-forum.com/t/modifying-activeresource-classes/172635/1 "2009-07-24T11:10:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bencahen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecb155/32.png) [@bencahen](https://www.ruby-forum.com/u/bencahen)\
**Post date:** [July 25, 2009, 12:49am UTC](https://www.ruby-forum.com/t/modifying-activeresource-classes/172635/2 "2009-07-25T00:49:31Z")

</div>

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.
