Class: Debugbar::TrackCurrentRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/debugbar/middlewares/track_current_request.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ TrackCurrentRequest

Returns a new instance of TrackCurrentRequest.



3
4
5
# File 'lib/debugbar/middlewares/track_current_request.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/debugbar/middlewares/track_current_request.rb', line 7

def call(env)
  Debugbar::Current.ignore = Debugbar.config.ignore_request?(env)

  return @app.call(env) if Debugbar::Current.ignore?

  Debugbar::Current.new_request!(SecureRandom.uuid)

  res = @app.call(env)

  # TODO: Remove this if statement?
  # We check meta because the frontend doesn't support request without meta yet.
  # It might happen with ActionController::Live where the following code
  # will run BEFORE ActionControllerEventSubscriber.process_action is called
  if Debugbar::Current.request&.meta
    # filename = "#{Time.now.to_i}--#{Debugbar::Current.request.meta.dig(:params, :controller)}_#{Debugbar::Current.request.meta.dig(:params, :action).gsub('/', '_')}.json"
    # File.open(Rails.root.join('_requests', filename), "w") do |f|
    #   f.write(Debugbar::Current.request.to_json)
    # end

    RequestBuffer.push(Debugbar::Current.pop_request!)

    # TODO: Refactor has not having ActionCable might more common than I thought
    if Debugbar.connected? && defined?(ActionCable)
      data = RequestBuffer.all.map(&:to_h)
      ActionCable.server.broadcast("debugbar_channel", data)
    end
  end

  res
end