Class: Retrier

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Retrier

Returns a new instance of Retrier.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/retrier.rb', line 2

def initialize(options={}, &block)
  max_tries = options.fetch(:max_tries, 10)
  rescuable = Array(options.fetch(:rescue, StandardError))
  handlers = options[:handlers]

  attempts = 0

  begin
    attempts += 1
    return block.call(attempts)
  rescue *rescuable => exception
    if handlers
      handlers.each do |ex,handler|
        handler.call(exception) if Kernel.const_get(ex) == exception.class
      end
    elsif attempts >= max_tries
      raise exception
    else
      retry
    end
  end

end