Class: HttpLog::Middleware

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

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 Method Details

#call(env) ⇒ Object



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

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

  if passes_filters?
    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

  @app.call env
end

#passes_filters?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/http_log/middleware.rb', line 26

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