Class: Rack::Bug::Toolbar

Inherits:
Object
  • Object
show all
Includes:
Render
Defined in:
lib/rack/bug/toolbar.rb

Constant Summary collapse

MIME_TYPES =
["text/html", "application/xhtml+xml"]

Instance Method Summary collapse

Methods included from Render

#compile, #compile!, #compiled_source, #method_name, #method_name_without_locals, #render_template, #signed_params

Constructor Details

#initialize(app) ⇒ Toolbar

Returns a new instance of Toolbar.



8
9
10
# File 'lib/rack/bug/toolbar.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#builderObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/bug/toolbar.rb', line 32

def builder
  builder = Rack::Builder.new

  @env["rack-bug.panel_classes"].each do |panel_class|
    builder.use panel_class
  end

  builder.run @app

  return builder
end

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rack/bug/toolbar.rb', line 12

def call(env)
  @env = env
  @env["rack-bug.panels"] = []

  Rack::Bug.enable
  status, headers, body = builder.call(@env)
  Rack::Bug.disable

  @response = Rack::Response.new(body, status, headers)
  
  inject_toolbar if response_type_okay_to_modify?
  
  return @response.to_a
end

#inject_toolbarObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rack/bug/toolbar.rb', line 44

def inject_toolbar
  full_body = @response.body.join
  full_body.sub! /<\/body>/, render + "</body>"

  @response["Content-Length"] = full_body.size.to_s

  # Ensure that browser does
  @response["Etag"] = ""
  @response["Cache-Control"] = "no-cache"

  @response.body = [full_body]
end

#renderObject



57
58
59
# File 'lib/rack/bug/toolbar.rb', line 57

def render
  render_template("toolbar", :panels => @env["rack-bug.panels"].reverse)
end

#response_type_okay_to_modify?Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/rack/bug/toolbar.rb', line 27

def response_type_okay_to_modify?
  content_type, charset = @response.content_type.split(";")
  @response.ok? && MIME_TYPES.include?(content_type)
end