Class: RequestRecorder::Middleware

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

Constant Summary collapse

MARKER =
"request_recorder"
MAX_STEPS =
100
SEPARATOR =
"-"
AUTH =
:frontend_auth

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



19
20
21
22
23
24
# File 'lib/request_recorder/middleware.rb', line 19

def initialize(app, options={})
  @app = app
  @store = options.fetch(:store)
  @auth = options[AUTH]
  @headers = options[:headers] || {}
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/request_recorder/middleware.rb', line 26

def call(env)
  # keep this part as fast as possible, since 99.99999% of requests will not need it
  return @app.call(env) unless "#{env["PATH_INFO"]}-#{env["QUERY_STRING"]}-#{env["HTTP_COOKIE"]}".include?(MARKER)

  if env["PATH_INFO"].to_s.starts_with?("/#{MARKER}/")
    key = env["PATH_INFO"].split("/")[2]
    render_frontend(env, key)
  else
    result = nil
    log = capture_logging do
      begin
        result = @app.call(env)
      rescue Exception => e
        result = e
      end
    end

    steps_left, id = read_state_from_env(env)
    return [500, {}, "#{MARKER} exceeded maximum value #{MAX_STEPS}"] if steps_left > MAX_STEPS
    id = persist_log(id, log)

    if result.is_a?(Exception)
      raise result # Do not mess up the apps normal exception behavior
    else
      path = [env["PATH_INFO"], env["QUERY_STRING"]].compact.join("?")
      extra_headers = chrome_logger_headers(log, path) if @auth && @auth.call(env)
      response_with_data_in_cookie(result, steps_left, id, extra_headers)
    end
  end
end