Class: Rack::Lineprof

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/lineprof.rb,
lib/rack/lineprof/sample.rb,
lib/rack/lineprof/source.rb

Defined Under Namespace

Classes: Sample, Source

Constant Summary collapse

CONTEXT =
0
NOMINAL =
1
WARNING =
2
CRITICAL =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Lineprof

Returns a new instance of Lineprof.



18
19
20
# File 'lib/rack/lineprof.rb', line 18

def initialize app, options = {}
  @app, @options = app, options
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



16
17
18
# File 'lib/rack/lineprof.rb', line 16

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/rack/lineprof.rb', line 16

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rack/lineprof.rb', line 22

def call env
  request = Rack::Request.new env
  matcher = request.params['lineprof'] || options[:profile]
  logger  = options[:logger] || ::Logger.new(STDOUT)

  return @app.call env unless matcher

  response = nil
  profile = lineprof(%r{#{matcher}}) { response = @app.call env }

  logger.debug Term::ANSIColor.blue("\n[Rack::Lineprof] #{'=' * 63}") + "\n\n" +
       format_profile(profile) + "\n"

  response
end

#format_profile(profile) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/rack/lineprof.rb', line 38

def format_profile profile
  sources = profile.map do |filename, samples|
    Source.new filename, samples, options
  end

  sources.map(&:format).compact.join "\n"
end