Class: EventLoggerRails::Middleware::CaptureRequestDetails

Inherits:
Object
  • Object
show all
Defined in:
lib/event_logger_rails/middleware/capture_request_details.rb

Overview

Middleware to capture request details and store in global state

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CaptureRequestDetails

Initializes the middleware with the given app.

Parameters:

  • app (Proc)

    The Rack app.



12
13
14
# File 'lib/event_logger_rails/middleware/capture_request_details.rb', line 12

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Note:

The CurrentRequest is reset at the end of the request.

Captures request details and stores in global state

Parameters:

  • env (Hash)

    The Rack environment.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/event_logger_rails/middleware/capture_request_details.rb', line 22

def call(env)
  begin
    request = ActionDispatch::Request.new(env)

    CurrentRequest.id = env['action_dispatch.request_id']
    CurrentRequest.format = request.headers['Content-Type']
    CurrentRequest.method = request.method
    CurrentRequest.parameters = request.parameters.except(:controller, :action, :format)
    CurrentRequest.path = request.path
    CurrentRequest.remote_ip = request.remote_ip

    status, headers, body = @app.call(env)
  ensure
    CurrentRequest.reset
  end

  [status, headers, body]
end