Jason Sallis wrote:
> I've got a simple Ruby class that has one method which uses open-uri to 
> pass a request to a remote server and retrieve an xml document 
> containing some server statistics.  Nothing too exciting and it's 
> working fine.  The administrator has asked that we limit our calls to 
> once every 30 seconds to avoid hammering the server.  I'd like to bake 
> this forced delay right into my class so that if another application is 
> using it and tries to make 2 calls within 30 seconds, the second call is 
> delayed until the 30 second time period is up.  Any ideas on how I could 
> approach this?

class politeGet
  def initialize(delay=30)
    @delay = delay
    @last_get = nil
  end

  def getDoc(uri)
    tdiff = Time.now.to_i - @last_get.to_i #diff in seconds
    sleep(tdiff+1) if (tdiff) < @delay  #force desired delay with fudge 
as sleep() may return early
    open(uri)
    @last_get = Time.now.to_i
  end
end

not tested or even executed but should be close 9^)

Cheers
Chris

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