Class: Scraypa::Throttle

Inherits:
Object
  • Object
show all
Defined in:
lib/scraypa/throttle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Throttle

Returns a new instance of Throttle.



8
9
10
# File 'lib/scraypa/throttle.rb', line 8

def initialize params={}
  @seconds = params.fetch(:seconds, nil)
end

Instance Attribute Details

#last_request_timeObject

Returns the value of attribute last_request_time.



5
6
7
# File 'lib/scraypa/throttle.rb', line 5

def last_request_time
  @last_request_time
end

#secondsObject (readonly)

Returns the value of attribute seconds.



6
7
8
# File 'lib/scraypa/throttle.rb', line 6

def seconds
  @seconds
end

Instance Method Details

#throttleObject



12
13
14
15
16
17
# 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