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



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

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
26
27
# File 'lib/rack/bug/toolbar.rb', line 12

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

  @env.delete("HTTP_IF_NONE_MATCH")

  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



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

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



59
60
61
# File 'lib/rack/bug/toolbar.rb', line 59

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

#response_type_okay_to_modify?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/rack/bug/toolbar.rb', line 29

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