Class: Appom::Wait

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

Constant Summary collapse

DEFAULT_TIMEOUT =
5
DEFAULT_INTERVAL =
0.25

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Wait

Create a new Wait instance

Options Hash (opts):

  • :timeout (Numeric) — default: 5

    Seconds to wait before timing out.

  • :interval (Numeric) — default: 0.25

    Seconds to sleep between polls.



13
14
15
16
# File 'lib/appom/wait.rb', line 13

def initialize(opts = {})
  @timeout  = opts.fetch(:timeout, DEFAULT_TIMEOUT)
  @interval = opts.fetch(:interval, DEFAULT_INTERVAL)
end

Instance Method Details

#untilObject

Wait until the given block returns a true value.

Raises:

  • (Error::TimeOutError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/appom/wait.rb', line 24

def until
  end_time = Time.now + @timeout
  error_message = ""

  until Time.now > end_time
    begin
      result = yield
      return result if result
    rescue => error
      error_message = error.message
    end

    sleep @interval
  end

  raise StandardError, error_message
end