Class: Ridley::Middleware::Retry
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Ridley::Middleware::Retry
- Defined in:
- lib/ridley/middleware/retry.rb
Overview
Note:
Borrowed and modified from: https://github.com/lostisland/faraday/blob/master/lib/faraday/request/retry.rb use the Faraday official middleware after the release of 0.9.x
Catches exceptions and retries each request a limited number of times.
Instance Method Summary collapse
-
#build_exception_matcher(exceptions) ⇒ Object
construct an exception matcher object.
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Retry
constructor
A new instance of Retry.
Constructor Details
#initialize(app, options = {}) ⇒ Retry
Returns a new instance of Retry.
20 21 22 23 24 |
# File 'lib/ridley/middleware/retry.rb', line 20 def initialize(app, = {}) super(app) @options = .slice(:max, :interval, :exceptions) @errmatch = build_exception_matcher(@options[:exceptions]) end |
Instance Method Details
#build_exception_matcher(exceptions) ⇒ Object
construct an exception matcher object.
An exception matcher for the rescue clause can usually be any object that responds to ‘===`, but for Ruby 1.8 it has to be a Class or Module.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ridley/middleware/retry.rb', line 44 def build_exception_matcher(exceptions) matcher = Module.new (class << matcher; self; end).class_eval do define_method(:===) do |error| exceptions.any? do |ex| if ex.is_a? Module then error.is_a? ex else error.class.to_s == ex.to_s end end end end matcher end |
#call(env) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ridley/middleware/retry.rb', line 26 def call(env) retries = @options[:max] begin @app.call(env) rescue @errmatch if retries > 0 retries -= 1 sleep @options[:interval] if @options[:interval] > 0 retry end raise end end |