Class: ActiveTracker::OutputCapturer

Inherits:
Object
  • Object
show all
Defined in:
lib/active_tracker/output_capturer.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ OutputCapturer

Returns a new instance of OutputCapturer.



3
4
5
# File 'lib/active_tracker/output_capturer.rb', line 3

def initialize(app)
  @app         = app
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/active_tracker/output_capturer.rb', line 7

def call(env)
  start_time = Time.current
  status, headers, response = @app.call(env)
  [status, headers, response]
ensure
  capture(response, headers)
  duration = (Time.current.to_f - start_time.to_f) * 1000
  ActiveTracker::Plugin::Request.record_duration(duration)
end

#capture(response, headers) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_tracker/output_capturer.rb', line 17

def capture(response, headers)
  body = response.body rescue nil
  unless body.is_a?(String)
    body = body.to_a rescue [body.body] rescue "No body given"
  end

  if body.respond_to?(:each)
    output = []
    body.each do |line|
      output << line
    end
    output = output.join("\n")
  else
    output = body.to_s
  end

  ActiveTracker::Plugin::Request.output_capture(output, headers["Content-type"])
end