Class: Notable::Middleware
- Inherits:
-
Object
- Object
- Notable::Middleware
- Defined in:
- lib/notable/middleware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
4 5 6 |
# File 'lib/notable/middleware.rb', line 4 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
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 74 |
# File 'lib/notable/middleware.rb', line 8 def call(env) if Notable.enabled start_time = Time.now status, headers, body = @app.call(env) request_time = Time.now - start_time safely do if env["action_dispatch.exception"] e = env["action_dispatch.exception"] = case status.to_i when 404 "Not Found" when 503 "Timeout" else "Error" end Notable.track , "#{e.class.name}: #{e.}" 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) # hack since Rails modifies PATH_INFO # and we don't want to modify env url = request.base_url + request.script_name + env["REQUEST_PATH"] url << "?#{request.query_string}" unless request.query_string.empty? 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| data = { note_type: note[:note_type], note: note[:note], user: user, action: action, status: status, params: params, request_id: request.uuid, ip: request.remote_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] else @app.call(env) end end |