Class: Pilfer::Middleware

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Middleware.



8
9
10
11
12
13
# File 'lib/pilfer/middleware.rb', line 8

def initialize(app, options = {}, &profile_guard)
  @app           = app
  @profiler      = options[:profiler] || default_profiler
  @file_matcher  = options[:file_matcher]
  @profile_guard = profile_guard || proc { true }
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



6
7
8
# File 'lib/pilfer/middleware.rb', line 6

def app
  @app
end

#file_matcherObject

Returns the value of attribute file_matcher.



6
7
8
# File 'lib/pilfer/middleware.rb', line 6

def file_matcher
  @file_matcher
end

#profile_guardObject

Returns the value of attribute profile_guard.



6
7
8
# File 'lib/pilfer/middleware.rb', line 6

def profile_guard
  @profile_guard
end

#profilerObject

Returns the value of attribute profiler.



6
7
8
# File 'lib/pilfer/middleware.rb', line 6

def profiler
  @profiler
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/pilfer/middleware.rb', line 15

def call(env)
  if profile_guard.call(env)
    run_profiler(request_description(env)) { app.call(env) }
  else
    app.call(env)
  end
end

#default_profilerObject



32
33
34
35
# File 'lib/pilfer/middleware.rb', line 32

def default_profiler
  reporter = Pilfer::Logger.new($stdout)
  Pilfer::Profiler.new(reporter)
end

#request_description(env) ⇒ Object



37
38
39
# File 'lib/pilfer/middleware.rb', line 37

def request_description(env)
  "#{env["REQUEST_METHOD"]} #{env["PATH_INFO"]}"
end

#run_profiler(description, &downstream) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/pilfer/middleware.rb', line 23

def run_profiler(description, &downstream)
  if file_matcher
    profiler.profile_files_matching(file_matcher, description,
                                    :submit => :async, &downstream)
  else
    profiler.profile(description, :submit => :async, &downstream)
  end
end