class Akita::HarLogger::Filter

Logging filter for `ActionController`s. TODO: Some amount of code duplication here. Should refactor.

Public Class Methods

install(out_file_name = nil, hook_name = :action_controller) click to toggle source

Registers an `on_load` initializer to add a logging filter to any ActionController that is created.

# File lib/akita/har_logger.rb, line 96
def self.install(out_file_name = nil, hook_name = :action_controller)
  ActiveSupport.on_load(hook_name) do
    around_action Filter.new(out_file_name)
  end
end
new(out_file_name = nil) click to toggle source
# File lib/akita/har_logger.rb, line 83
def initialize(out_file_name = nil)
  if out_file_name == nil then
    out_file_name = HarLogger.default_file_name
  end

  @entry_queue = HarLogger.get_queue(out_file_name)
rescue => e
  HarLogger.logException("initializing filter", e)
  raise
end

Public Instance Methods

around(controller) { || ... } click to toggle source

Implements the actual `around` filter.

# File lib/akita/har_logger.rb, line 103
def around(controller)
  start_time = Time.now

  # Read the request body here, in case there is non-Rack-compliant
  # middleware in the stack that closes the request-body stream on us.
  request_body = controller.response.request.env['rack.input'].read
  controller.response.request.env['rack.input'].rewind  # Be kind.

  yield

  end_time = Time.now
  wait_time_ms = ((end_time.to_f - start_time.to_f) * 1000).round

  response = controller.response
  request = response.request

  # Patch env with our saved request body.
  saved_input = request.env['rack.input']
  request.env['rack.input'] = StringIO.new request_body

  @entry_queue << (HarEntry.new start_time, wait_time_ms, request.env,
                                response.status, response.headers,
                                [response.body])

  # Be kind and restore the original request-body stream.
  request.env['rack.input'] = saved_input
rescue => e
  HarLogger.logException("handling request", e)
  raise
end