Class: Rack::Bug::Toolbar

Inherits:
Object
  • Object
show all
Includes:
Options, 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

Methods included from Options

#options, #options=, #set

Constructor Details

#initialize(app, options = {}) ⇒ Toolbar

Returns a new instance of Toolbar.



35
36
37
38
39
# File 'lib/rack/bug/toolbar.rb', line 35

def initialize(app, options = {})
  @app = asset_server(app)
  initialize_options options
  instance_eval(&block) if block_given?
end

Instance Method Details

#asset_server(app) ⇒ Object



41
42
43
# File 'lib/rack/bug/toolbar.rb', line 41

def asset_server(app)
  RackStaticBugAvoider.new(app, Rack::Static.new(app, :urls => ["/__rack_bug__"], :root => public_path))
end

#builderObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rack/bug/toolbar.rb', line 118

def builder
  builder = Rack::Builder.new
  
  options["rack-bug.panel_classes"].each do |panel_class|
    builder.use panel_class
  end
  
  builder.run @app
  
  return builder
end

#call(env) ⇒ Object



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

def call(env)
  env.replace @default_options.merge(env)
  @env = env
  @original_request = Request.new(@env)

  if toolbar_requested? && ip_authorized? && password_authorized?
    dispatch
  else
    pass
  end
end

#dispatchObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rack/bug/toolbar.rb', line 65

def dispatch
  @env["rack-bug.panels"] = []
  
  Rack::Bug.enable
  status, headers, body = builder.call(@env)
  Rack::Bug.disable
  
  @response = Rack::Response.new(body, status, headers)
  
  if @response.redirect? && options["rack-bug.intercept_redirects"]
    intercept_redirect
  elsif modify?
    inject_toolbar
  end
  
  return @response.to_a
end

#inject_toolbarObject



130
131
132
133
134
135
136
# File 'lib/rack/bug/toolbar.rb', line 130

def inject_toolbar
  full_body = @response.body.join
  full_body.sub! /<\/body>/, render + "</body>"
  
  @response["Content-Length"] = full_body.size.to_s
  @response.body = [full_body]
end

#intercept_redirectObject



83
84
85
86
87
88
89
# File 'lib/rack/bug/toolbar.rb', line 83

def intercept_redirect
  redirect_to = @response.location
  new_body = render_template("redirect", :redirect_to => @response.location)
  new_response = Rack::Response.new(new_body, 200, { "Content-Type" => "text/html" })
  new_response["Content-Length"] = new_body.size.to_s
  @response = new_response
end

#ip_authorized?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
# File 'lib/rack/bug/toolbar.rb', line 95

def ip_authorized?
  return true unless options["rack-bug.ip_masks"]
  
  options["rack-bug.ip_masks"].any? do |ip_mask|
    ip_mask.include?(IPAddr.new(@original_request.ip))
  end
end

#modify?Boolean

Returns:

  • (Boolean)


112
113
114
115
116
# File 'lib/rack/bug/toolbar.rb', line 112

def modify?
  @response.ok? &&
  @env["X-Requested-With"] != "XMLHttpRequest" &&
  MIME_TYPES.include?(@response.content_type.split(";").first)
end

#passObject



61
62
63
# File 'lib/rack/bug/toolbar.rb', line 61

def pass
  @app.call(@env)
end

#password_authorized?Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
# File 'lib/rack/bug/toolbar.rb', line 103

def password_authorized?
  return true unless options["rack-bug.password"]
  
  expected_sha = Digest::SHA1.hexdigest ["rack_bug", options["rack-bug.password"]].join(":")
  actual_sha = @original_request.cookies["rack_bug_password"]
  
  actual_sha == expected_sha
end

#public_pathObject



45
46
47
# File 'lib/rack/bug/toolbar.rb', line 45

def public_path
  ::File.expand_path(::File.dirname(__FILE__) + "/../bug/public")
end

#renderObject



138
139
140
# File 'lib/rack/bug/toolbar.rb', line 138

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

#toolbar_requested?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/rack/bug/toolbar.rb', line 91

def toolbar_requested?
  @original_request.cookies["rack_bug_enabled"]
end