class RackProf
Constants
- DEFAULT_PRINTER
- PRINTERS
- VERSION
Public Class Methods
new(app, options = {})
click to toggle source
# File lib/rack_prof.rb, line 16 def initialize(app, options = {}) @app = app end
Public Instance Methods
call(env)
click to toggle source
# File lib/rack_prof.rb, line 20 def call(env) request = Rack::Request.new(env) if request.params.delete('profile') profile(env, request) else @app.call(env) end end
Private Instance Methods
camel_case(word)
click to toggle source
# File lib/rack_prof.rb, line 70 def camel_case(word) word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase } end
get_file_name(request, printer)
click to toggle source
# File lib/rack_prof.rb, line 48 def get_file_name(request, printer) path = request.path.gsub('/', '-') path.slice!(0) base_name = PRINTERS[printer] File.join(Dir.tmpdir, "#{path}-#{base_name}") end
parse_printer(printer)
click to toggle source
# File lib/rack_prof.rb, line 55 def parse_printer(printer) if printer.nil? DEFAULT_PRINTER elsif printer.is_a?(Class) printer else name = "#{camel_case(printer)}Printer" if RubyProf.const_defined?(name) RubyProf.const_get(name) else DEFAULT_PRINTER end end end
print(result, printer, file_name)
click to toggle source
# File lib/rack_prof.rb, line 41 def print(result, printer, file_name) File.open(file_name, 'wb') do |file| printer.new(result).print(file, :min_percent => 1) end file_name end
profile(env, request)
click to toggle source
# File lib/rack_prof.rb, line 31 def profile(env, request) response = nil result = RubyProf.profile { response = @app.call(env) } printer = parse_printer(request.params.delete('printer')) file_name = get_file_name(request, printer) print(result, printer, file_name) Launchy.open(file_name) response end