class Pagetience::Meditate

Constants

DEFAULT_TIMEOUT_MESSAGE

Attributes

block[RW]
polling[RW]
timeout[RW]

Public Class Methods

for(opts, &block) click to toggle source
# File lib/pagetience/meditate.rb, line 8
def for(opts, &block)
  opts[:timeout] ||= 30
  opts[:polling] ||= 1
  opts[:msg] ||= DEFAULT_TIMEOUT_MESSAGE
  Meditate.new(opts[:timeout], opts[:polling]) { block.call }.until_enlightened opts[:expecting], opts[:msg]
end
new(timeout=30, polling=1, &block) click to toggle source
# File lib/pagetience/meditate.rb, line 16
def initialize(timeout=30, polling=1, &block)
  @timeout = timeout
  @polling = polling
  @block = block
end

Public Instance Methods

until_enlightened(expected=nil, msg=DEFAULT_TIMEOUT_MESSAGE) click to toggle source
# File lib/pagetience/meditate.rb, line 22
def until_enlightened(expected=nil, msg=DEFAULT_TIMEOUT_MESSAGE)
  raise ArgumentError, 'Timeout cannot be lower than the polling value.' unless @timeout > @polling

  while @timeout > 0 && @timeout > @polling
    @latest_result = @block.call
    break if @latest_result == expected
    sleep @polling
    @timeout = @timeout - @polling
  end

  msg = msg.send(:call) if msg.is_a? Proc
  raise Pagetience::TimeoutError, msg unless @latest_result == expected

  @latest_result
end