Class: MiniProfiler::Profiler
- Inherits:
-
Object
- Object
- MiniProfiler::Profiler
- Defined in:
- lib/mini_profiler.rb
Constant Summary collapse
- XML_HTTP_REQUEST_HEADER =
'xmlhttprequest'
Instance Method Summary collapse
- #ajax_request?(env) ⇒ Boolean
- #asset_request?(path) ⇒ Boolean
- #call(env) ⇒ Object
- #html_response?(headers) ⇒ Boolean
-
#initialize(app) ⇒ Profiler
constructor
A new instance of Profiler.
- #inject_header(headers, timing) ⇒ Object
- #inject_html!(headers, response, timing) ⇒ Object
- #profile_request?(env, headers, response) ⇒ Boolean
Constructor Details
#initialize(app) ⇒ Profiler
Returns a new instance of Profiler.
10 11 12 |
# File 'lib/mini_profiler.rb', line 10 def initialize(app) @app = app end |
Instance Method Details
#ajax_request?(env) ⇒ Boolean
53 54 55 |
# File 'lib/mini_profiler.rb', line 53 def ajax_request?(env) env['HTTP_X_REQUESTED_WITH'] == XML_HTTP_REQUEST_HEADER end |
#asset_request?(path) ⇒ Boolean
14 15 16 |
# File 'lib/mini_profiler.rb', line 14 def asset_request?(path) path =~ /^\/mini_profiler\/public/ end |
#call(env) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mini_profiler.rb', line 18 def call(env) status, headers, response = nil path = env['PATH_INFO'] if asset_request? path file = Rack::File.new '' file.path = ::File.join(::File.dirname(__FILE__), env['PATH_INFO']) return file.serving env end duration = Benchmark.realtime do status, headers, response = @app.call(env) end * 1000 if profile_request?(env, headers, response) timing = Timing.new timing.name = env['REQUEST_URI'] timing.duration = duration if html_response?(headers) inject_html!(headers, response, timing) elsif ajax_request?(env) inject_header(headers, timing) end end [status, headers, response] end |
#html_response?(headers) ⇒ Boolean
57 58 59 |
# File 'lib/mini_profiler.rb', line 57 def html_response?(headers) headers && headers['Content-Type'] && headers['Content-Type'].include?('text/html') end |
#inject_header(headers, timing) ⇒ Object
94 95 96 |
# File 'lib/mini_profiler.rb', line 94 def inject_header(headers, timing) headers['X-Mini-Profiler-Id'] = timing.id end |
#inject_html!(headers, response, timing) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mini_profiler.rb', line 61 def inject_html!(headers, response, timing) code = '' code << '<div class="profiler-results"></div>' code << '<link rel="stylesheet" href="/mini_profiler/public/includes.css" />' code << '<script type="text/javascript" src="/mini_profiler/public/jquery.tmpl.js"></script>' code << '<script type="text/javascript" src="/mini_profiler/public/includes.js"></script>' code << %Q{ <script type="text/javascript"> (function() { var init = function() { MiniProfiler.init({ id: '#{timing.id}', name: '#{timing.name}', duration: #{timing.duration} }); }; var o = window.onload; window.onload = function(){if(o)o; init()}; }()); </script> } # most likely ActionDispatch response - set body instead if response.respond_to?(:body) response_body = response.body = response.body.gsub('</body>', "#{code}</body>") else # anything else, modify it directly response_body = response.first.gsub!('</body>', "#{code}</body>") end headers['Content-Length'] = response_body.bytesize.to_s end |
#profile_request?(env, headers, response) ⇒ Boolean
49 50 51 |
# File 'lib/mini_profiler.rb', line 49 def profile_request?(env, headers, response) response && env['HTTP_ACCEPT'].include?('text/html') && (ajax_request?(env) || html_response?(headers)) end |