My application requires "Mechanize with a holdoff", that is, one that
limits its #get and #submit requests to a maximum of one every N
seconds.  I have a simple Governor class which runs a block with a
minimum holdoff between runs:

class Governor
  def with_holdoff(holdoff)
    unless (@ran_at.nil? || holdoff.nil?)
      delay = (@ran_at + holdoff) - Time.now
      sleep(delay) if (delay > 0)
    end
    @ran_at = Time.now
    yield
  end
end

I could write a class that encapsulates a Mechanize object and wraps a
with_holdoff call around its #get and #submit methods, e.g.:

class WrappedMechanize
  @mechanize = Mechanize.new
  ...
  def get(uri)
    @governor.with_holdoff(@holdoff_time) { @mechanize.get(uri) }
  end
end

... but that seems un-rubyish.  What's the cleanest way to extend the
Mechanize functionality?  Perhaps a subclass like this?

class MechanizeWithHoldoff < Mechanize
  ...
  def get_with_holdoff(uri, holdoff_time)
    @governor.with_holdoff(holdoff_time) { super.get(uri) }
  end
end

Suggestions welcome.

- ff
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to