Class: HttpLog::Middleware

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
# File 'lib/http_log/middleware.rb', line 5

def initialize(app)
  @app = app
end

Instance Attribute Details

#requestObject

Returns the value of attribute request.



3
4
5
# File 'lib/http_log/middleware.rb', line 3

def request
  @request
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/http_log/middleware.rb', line 9

def call(env)
  begin
    @proxy = HttpRequest.new(env)

    if passes_filters? && !@proxy.multi_part?
      request = HttpLog::Request.from_request(@proxy)

      HttpLog.callbacks.each do |callback|
        callback.call @proxy, request
      end

      request.save
      env['http_log.request_id'] = request.id.to_s
    end
  rescue => ex
    Rails.logger.warn "Request could not be saved! #{ex} -- switch to debugging logging for more info"
    Rails.logger.debug ex.backtrace.join("\n")
    Rails.logger.debug env.inspect
  end

  @app.call env
end

#passes_filters?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/http_log/middleware.rb', line 32

def passes_filters?
  HttpLog.filters.each do |filter|
    matches_filter = if filter.is_a? Symbol
               @proxy.path_info =~ /\.#{filter}$/
             elsif filter.is_a? Regexp
                @proxy.url =~ filter
             else
               filter.call @proxy
             end

    return false if matches_filter
  end

  true
end