Module: Capybara::Maleficent

Defined in:
lib/capybara/maleficent.rb,
lib/capybara/maleficent/logger.rb,
lib/capybara/maleficent/version.rb,
lib/capybara/maleficent/configuration.rb

Defined Under Namespace

Modules: Spindle Classes: Configuration, Logger

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.configurationObject



49
50
51
# File 'lib/capybara/maleficent.rb', line 49

def self.configuration
  @configuration || Configuration.new
end

.configure {|@configuration| ... } ⇒ Object

Yields:



53
54
55
56
57
# File 'lib/capybara/maleficent.rb', line 53

def self.configure
  @configuration = Configuration.new
  yield(@configuration)
  @configuration
end

.default_sleeperObject



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

def self.default_sleeper
  Kernel
end

.with_sleep_injection(the_sleeper: Kernel, logger: configuration.logger, sleep_durations: configuration.sleep_durations, handled_exceptions: configuration.handled_exceptions) { ... } ⇒ Object

A method that wraps the given block in a retry sleep strategy. Of particular use for waiting on Capybara.

Parameters:

  • the_sleeper (#sleep) (defaults to: Kernel)

    What will be doing the sleeping; default is Kernel

  • logger (#debug, #info) (defaults to: configuration.logger)
  • sleep_durations (Array<Integer>) (defaults to: configuration.sleep_durations)

    The size of the array represents the max number of sleep attempts, each element represents how long

  • handled_exceptions (Array<Exceptions] Which exceptions should the sleep injector handle) (defaults to: configuration.handled_exceptions)

    andled_exceptions [Array<Exceptions] Which exceptions should the sleep injector handle

Yields:

  • The block of code for which we will allow sleeping

Returns:

  • The value of the given block

See Also:

  • for specifications of behavior


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/capybara/maleficent.rb', line 17

def self.with_sleep_injection(the_sleeper: Kernel, logger: configuration.logger, sleep_durations: configuration.sleep_durations, handled_exceptions: configuration.handled_exceptions)
  logger.debug "Starting Capybara::Maleficent.with_sleep_injection"
  sleep_durations = sleep_durations.clone
  last_try_sleep_duration = sleep_durations.pop
  return_value = nil
  success = false
  sleep_durations.each do |sleep_duration|
    begin
      return_value = yield.tap { success = true }
      break if success
    rescue *handled_exceptions
      logger.info "Sleeping for #{sleep_duration} via Capybara::Maleficent.with_sleep_injection (for #{handled_exceptions.inspect})"
      the_sleeper.sleep(sleep_duration)
      next
    end
  end
  if success
    logger.debug "Ending Capybara::Maleficent.with_sleep_injection"
    return return_value
  else
    logger.info "Sleeping for #{last_try_sleep_duration}via Capybara::Maleficent.with_sleep_injection (for #{handled_exceptions.inspect}). This is the last try."
    the_sleeper.sleep(last_try_sleep_duration)
    yield.tap do
      logger.debug "Ending Capybara::Maleficent.with_sleep_injection"
    end
  end
end