Class: ThereWasAnAttempt

Inherits:
Object
  • Object
show all
Defined in:
lib/there_was_an_attempt.rb,
lib/there_was_an_attempt/version.rb

Defined Under Namespace

Modules: Intervals, Reattempt, Wait

Constant Summary collapse

VERSION =
'2.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(intervals: Intervals::DEFAULT, wait: Wait::WITH_SLEEP, reattempt: Reattempt::ALWAYS) ⇒ ThereWasAnAttempt

Returns a new instance of ThereWasAnAttempt.



18
19
20
21
22
# File 'lib/there_was_an_attempt.rb', line 18

def initialize(intervals: Intervals::DEFAULT, wait: Wait::WITH_SLEEP, reattempt: Reattempt::ALWAYS)
  @intervals = intervals
  @wait = wait
  @reattempt = reattempt
end

Class Method Details

.attempt(&block) ⇒ Object



24
25
26
# File 'lib/there_was_an_attempt.rb', line 24

def self.attempt(&block)
  new.attempt(&block)
end

Instance Method Details

#attempt(&block) ⇒ Object



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

def attempt(&block)
  @intervals.map do |seconds|
    result = block.call
    break [result] if result.success? || !reattempt?(result.failure)

    result.or { |failure| wait(seconds); Dry::Monads::Failure(failure) }
  end.last
end