Class: Circuitbox::FaradayMiddleware
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Circuitbox::FaradayMiddleware
- Defined in:
- lib/circuitbox/faraday_middleware.rb
Defined Under Namespace
Classes: NullResponse, RequestFailed
Constant Summary collapse
- DEFAULT_EXCEPTIONS =
[ Faraday::Error::TimeoutError, RequestFailed, ]
- DEFAULT_CIRCUITBOX_OPTIONS =
{ open_circuit: lambda do |response| # response.status: # nil -> connection could not be established, or failed very hard # 5xx -> non recoverable server error, oposed to 4xx which are client errors response.status.nil? || (500 <= response.status && response.status <= 599) end }
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #call(request_env) ⇒ Object
- #exceptions ⇒ Object
- #identifier ⇒ Object
-
#initialize(app, opts = {}) ⇒ FaradayMiddleware
constructor
A new instance of FaradayMiddleware.
Constructor Details
#initialize(app, opts = {}) ⇒ FaradayMiddleware
Returns a new instance of FaradayMiddleware.
33 34 35 36 37 |
# File 'lib/circuitbox/faraday_middleware.rb', line 33 def initialize(app, opts = {}) @app = app @opts = DEFAULT_CIRCUITBOX_OPTIONS.merge(opts) super(app) end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
22 23 24 |
# File 'lib/circuitbox/faraday_middleware.rb', line 22 def opts @opts end |
Instance Method Details
#call(request_env) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/circuitbox/faraday_middleware.rb', line 39 def call(request_env) service_response = nil circuit(request_env).run!((request_env)) do @app.call(request_env).on_complete do |env| service_response = Faraday::Response.new(env) raise RequestFailed if open_circuit?(service_response) end end rescue Circuitbox::Error => ex circuit_open_value(request_env, service_response, ex) end |
#exceptions ⇒ Object
51 52 53 |
# File 'lib/circuitbox/faraday_middleware.rb', line 51 def exceptions [:exceptions] end |
#identifier ⇒ Object
55 56 57 |
# File 'lib/circuitbox/faraday_middleware.rb', line 55 def identifier @identifier ||= opts.fetch(:identifier, ->(env) { env[:url] }) end |