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.



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

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

Instance Method Details

#back_offObject



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

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

#reduce_waitObject



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

def reduce_wait
  sleep(REDUCE_WAIT)
  back_off
end

#valueObject



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

def value
  @wait
end

#waitObject



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

def wait
  sleep(@wait)
end