Class: Faraday::VerboseWsWrapper::Middleware

Inherits:
Middleware
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  options.reverse_merge!({log_body: false})
  super(env)
  self.tag = tag
  self.log_body = options[:log_body]
  self.color = options[:color] || :gray
end

Instance Attribute Details

#color=(value) ⇒ Object (writeonly)

Sets the attribute color

Parameters:

  • value

    the value to set the attribute color to.



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

Parameters:

  • value

    the value to set the attribute log_body to.



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

Parameters:

  • value

    the value to set the attribute tag to.



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
  tags = [@tag.send(@color).bold]
  tags << req_id.send(@color) unless Rails.env.development?
  Rails.logger.tagged tags 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