Class: Spider::WaitTime

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

Constant Summary collapse

MAX_WAIT =
180
DEFAULT_WAIT =
2
REDUCE_WAIT =
300

Instance Method Summary collapse

Constructor Details

#initialize(period = nil) ⇒ WaitTime

Returns a new instance of WaitTime.



20
21
22
23
24
25
26
# File 'lib/wait_time.rb', line 20

def initialize(period=nil)
  unless period.nil?
    @wait = (period > MAX_WAIT ? MAX_WAIT : period)
  else
    @wait = DEFAULT_WAIT
  end
end

Instance Method Details

#back_offObject



28
29
30
31
32
33
34
35
# File 'lib/wait_time.rb', line 28

def back_off
  if @wait.zero?
    @wait = DEFAULT_WAIT 
  else
    waitval = @wait * 2 
    @wait = (waitval > MAX_WAIT ? MAX_WAIT : waitval)
  end
end

#reduce_waitObject



41
42
43
44
# File 'lib/wait_time.rb', line 41

def reduce_wait
  sleep(REDUCE_WAIT)
  back_off
end

#valueObject



46
47
48
# File 'lib/wait_time.rb', line 46

def value
  @wait
end

#waitObject



37
38
39
# File 'lib/wait_time.rb', line 37

def wait
  sleep(@wait)
end