class Scraypa::Throttle

Attributes

last_request_time[RW]
seconds[R]

Public Class Methods

new(params={}) click to toggle source
# File lib/scraypa/throttle.rb, line 8
def initialize params={}
  @seconds = params.fetch(:seconds, nil)
end

Public Instance Methods

throttle() click to toggle source
# File lib/scraypa/throttle.rb, line 12
def throttle
  @seconds && @last_request_time ? (@seconds.is_a?(Hash) ?
      sleep_from_last_request_time_for(
          Random.new.rand(@seconds[:from]..@seconds[:to])) :
      sleep_from_last_request_time_for(@seconds)) : nil
end

Private Instance Methods

sleep_from_last_request_time_for(seconds) click to toggle source
# File lib/scraypa/throttle.rb, line 21
def sleep_from_last_request_time_for seconds
  sleep_time = @last_request_time ?
      seconds - (Time.now - @last_request_time) : seconds
  sleep(sleep_time) if sleep_time > 0
end