Class: Notable::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
# File 'lib/notable/middleware.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/notable/middleware.rb', line 7

def call(env)
  start_time = Time.now
  status, headers, body = @app.call(env)
  request_time = Time.now - start_time

  Safely.safely do
    if env["action_dispatch.exception"]
      e = env["action_dispatch.exception"]
      message =
        case status.to_i
        when 404
          "Not Found"
        when 503
          "Timeout"
        else
          "Error"
        end
      Notable.track message, "#{e.class.name}: #{e.message}"
    elsif (!status or status.to_i >= 400) and !Notable.notes.any?
      Notable.track Rack::Utils::HTTP_STATUS_CODES[status.to_i]
    end

    if request_time > Notable.slow_request_threshold and status.to_i != 503
      Notable.track "Slow Request"
    end

    notes = Notable.notes
    if notes.any?
      request = ActionDispatch::Request.new(env)

      url = request.original_url

      controller = env["action_controller.instance"]
      action = controller && "#{controller.params["controller"]}##{controller.params["action"]}"
      params = controller && controller.request.filtered_parameters.except("controller", "action")

      user = Notable.user_method.call(env)

      notes.each do |note|
        ip = request.remote_ip
        if ip && Notable.mask_ips
          ip = Notable.mask_ip(ip)
        end

        data = {
          note_type: note[:note_type],
          note: note[:note],
          user: user,
          action: action,
          status: status,
          params: params,
          request_id: request.uuid,
          ip: ip,
          user_agent: request.user_agent,
          url: url,
          referrer: request.referer,
          request_time: request_time
        }
        Notable.track_request_method.call(data, env)
      end
    end
  end

  [status, headers, body]
ensure
  Notable.clear_notes
end