Class: Rack::Insight::Toolbar
- Inherits:
-
Object
- Object
- Rack::Insight::Toolbar
show all
- Includes:
- Logging, Render
- Defined in:
- lib/rack/insight/toolbar.rb
Constant Summary
collapse
- MIME_TYPES =
["text/plain", "text/html", "application/xhtml+xml"]
Instance Method Summary
collapse
Methods included from Logging
logger, verbose, verbosity
Methods included from Render
#compile, #compile!, #compiled_source, #method_name, #method_name_without_locals, #render_template, #signed_params
Constructor Details
#initialize(app, insight) ⇒ Toolbar
Returns a new instance of Toolbar.
8
9
10
11
12
|
# File 'lib/rack/insight/toolbar.rb', line 8
def initialize(app, insight)
@app = app
@insight = insight
@request_table = Database::RequestTable.new
end
|
Instance Method Details
#call(env) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/rack/insight/toolbar.rb', line 14
def call(env)
@env = env
status, , body = @app.call(@env)
response = Rack::Response.new(body, status, )
if okay_to_modify?(env, response)
inject_toolbar(response)
end
return response.to_a
end
|
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/rack/insight/toolbar.rb', line 36
def inject_toolbar(response)
full_body = response.body.join
toolbar = render
toolbar.force_encoding('UTF-8') if RUBY_VERSION > '1.9.0'
full_body.sub! /<\/body>/, toolbar + "</body>"
response["Content-Length"] = full_body.size.to_s
response["Etag"] = ""
response["Cache-Control"] = "no-cache"
response.body = [full_body]
end
|
#okay_to_modify?(env, response) ⇒ Boolean
27
28
29
30
31
32
33
34
|
# File 'lib/rack/insight/toolbar.rb', line 27
def okay_to_modify?(env, response)
return false unless response.ok?
return false unless response.content_type.respond_to?(:split)
content_type, charset = response.content_type.split(";")
return false unless MIME_TYPES.include?(content_type)
req = Rack::Request.new(env)
!req.xhr?
end
|
#render ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/rack/insight/toolbar.rb', line 53
def render
req_id = (@env['rack-insight.request-id'] || @request_table.last_request_id).to_i
requests = @request_table.to_a.map do |row|
{ :id => row[0], :method => row[1], :path => row[2] }
end
unless verbose(:silent)
logger.info do
"Injecting toolbar: active panels: #{@insight.panels.map{|pnl| pnl.class.name}.inspect}"
end
end
= render_template("headers_fragment",
:panels => @insight.panels,
:request_id => req_id,
:handle_javascript => @insight.config[:handle_javascript])
current_request_fragment = render_template("request_fragment",
:request_id => req_id,
:requests => requests,
:panels => @insight.panels)
render_template("toolbar",
:request_fragment => current_request_fragment,
:headers_fragment => ,
:request_id => req_id,
:handle_javascript => Rack::Insight::Config.config[:handle_javascript])
end
|