Sha256: 213504461795e6d4e84f459e62bd4f4afddc0fe433cd7386e9aa6d6317facfdb

Contents?: true

Size: 730 Bytes

Versions: 2

Compression:

Stored size: 730 Bytes

Contents

require 'faraday'
require 'circuitbox'

class Circuitbox
  class RequestError < StandardError; end

  class FaradayMiddleware < Faraday::Response::Middleware

    attr_accessor :identifier, :exceptions

    def initialize(app, opts={})
      @identifier = opts.fetch(:identifier) { ->(env) { env.url }}
      @exceptions = opts.fetch(:exceptions) { [Faraday::Error::TimeoutError] }
      super(app)
    end

    def call(env)
      id = identifier.respond_to?(:call) ? identifier.call(env) : identifier
      circuit = Circuitbox.circuit id, :exceptions => exceptions
      circuit.run do
        super(env)
      end
    end

    def on_complete(env)
      if !env.success?
        raise RequestError
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
circuitbox-0.7.0 lib/circuitbox/faraday_middleware.rb
circuitbox-0.5.2 lib/circuitbox/faraday_middleware.rb