Class: Faraday::VerboseWsWrapper::Middleware
- Inherits:
-
Middleware
- Object
- Middleware
- Faraday::VerboseWsWrapper::Middleware
- Defined in:
- lib/faraday/verbose_ws_wrapper/middleware.rb
Overview
This class provides the main implementation for your middleware. Your middleware can implement any of the following methods:
-
on_request - called when the request is being prepared
-
on_complete - called when the response is being processed
Optionally, you can also override the following methods from Faraday::Middleware
-
initialize(app, options = {}) - the initializer method
-
call(env) - the main middleware invocation method. This already calls on_request and on_complete, so you normally don’t need to override it. You may need to in case you need to “wrap” the request or need more control (see “retry” middleware: github.com/lostisland/faraday/blob/main/lib/faraday/request/retry.rb#L142). IMPORTANT: Remember to call ‘@app.call(env)` or `super` to not interrupt the middleware chain!
Instance Attribute Summary collapse
-
#color ⇒ Object
writeonly
Sets the attribute color.
-
#log_body ⇒ Object
writeonly
Sets the attribute log_body.
-
#tag ⇒ Object
writeonly
Sets the attribute tag.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(env, tag, options = {}) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(env, tag, options = {}) ⇒ Middleware
Returns a new instance of Middleware.
21 22 23 24 25 26 27 |
# File 'lib/faraday/verbose_ws_wrapper/middleware.rb', line 21 def initialize(env, tag, = {}) .reverse_merge!({log_body: false}) super(env) self.tag = tag self.log_body = [:log_body] self.color = [:color] || :gray end |
Instance Attribute Details
#color=(value) ⇒ Object (writeonly)
Sets the attribute color
19 20 21 |
# File 'lib/faraday/verbose_ws_wrapper/middleware.rb', line 19 def color=(value) @color = value end |
#log_body=(value) ⇒ Object (writeonly)
Sets the attribute log_body
19 20 21 |
# File 'lib/faraday/verbose_ws_wrapper/middleware.rb', line 19 def log_body=(value) @log_body = value end |
#tag=(value) ⇒ Object (writeonly)
Sets the attribute tag
19 20 21 |
# File 'lib/faraday/verbose_ws_wrapper/middleware.rb', line 19 def tag=(value) @tag = value end |
Instance Method Details
#call(env) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/faraday/verbose_ws_wrapper/middleware.rb', line 29 def call(env) req_id = env.request_headers["X-Request-ID"] = SecureRandom.uuid = [@tag.send(@color).bold] << req_id.send(@color) unless Rails.env.development? Rails.logger.tagged do msg = ">> #{env.method} #{env.url}".send(@color) msg += " body: #{env.body.to_json}" if @log_body Rails.logger.info { msg } @app.call(env).on_complete do |env| raise 'request and response has differnet X-Request-ID header' if req_id != env.response_headers['X-Request-ID'] Rails.logger.info { "<< #{env.status} in #{env.response_headers['X-Runtime'].to_f * 1000 } ms" } Rails.logger.debug { "<< #{env.response.body}" } end end end |