Permalink
Browse files

DRY up template and output for request/env panel

  • Loading branch information...
1 parent 9147c30 commit d7b4e5eca0c21458a9e6430e16fb95d608e70c69 @knzconnor knzconnor committed with Luke Melia Mar 25, 2010
View
29 lib/rack/bug/panels/request_variables_panel.rb
@@ -16,7 +16,34 @@ def heading
end
def content
- render_template "panels/request_variables", :request => @request, :env => @env
+ sections = {}
+ sections["GET"] = sort(@request.GET) if @request.GET.any?
+ sections["POST"] = sort(@request.GET) if @request.POST.any?
+ sections["Session"] = sort(@request.env["rack.session"]) if @request.env["rack.session"] && @request.env["rack.session"].any?
+ sections["Cookies"] = sort(@request.env["rack.request.cookie_hash"]) if @request.env["rack.request.cookie_hash"] && @request.env["rack.request.cookie_hash"].any?
+ server, rack = split_and_filter_env(@env)
+ sections["SERVER VARIABLES"] = sort(server)
+ sections["Rack ENV"] = sort(rack)
+ render_template "panels/request_variables", :sections => sections
+ end
+
+ private
+ def sort(hash)
+ hash.sort_by { |k, v| k.to_s }
+ end
+
+ def split_and_filter_env(env)
+ server, rack = {}, {}
+ env.each do |k,v|
+ if k.index("rack.") == 0
+ rack[k] = v.to_s
+ elsif k.index("rack-bug.") == 0
+ #don't output the rack-bug variables - especially secret_key
+ else
+ server[k] = v.to_s
+ end
+ end
+ return server, rack
end
end
View
96 lib/rack/bug/views/panels/request_variables.html.erb
@@ -1,5 +1,7 @@
-<% if request.GET.any? %>
- <h3>GET</h3>
+<% ["GET", "POST", "Session", "Cookies", "SERVER VARIABLES", "Rack ENV"].each do |header|
+ next unless sections.has_key?(header)
+%>
+ <h3><%=header%></h3>
<table>
<thead>
<tr>
@@ -9,7 +11,7 @@
</thead>
<tbody>
<% i = 1 %>
- <% request.GET.sort_by { |k, v| k.to_s }.each do |key, val| %>
+ <% sections[header].each do |key, val| %>
<tr class="<%= i % 2 == 0 ? "even" : "odd" %>">
<td><%=h key %></td>
<td class="code"><div><%=h val %></div></td>
@@ -18,90 +20,4 @@
<% end %>
</tbody>
</table>
-<% end %>
-
-<% if request.POST.any? %>
- <h3>POST</h3>
- <table>
- <thead>
- <tr>
- <th>Variable</th>
- <th>Value</th>
- </tr>
- </thead>
- <tbody>
- <% i = 1 %>
- <% request.POST.sort_by { |k, v| k.to_s }.each do |key, val| %>
- <tr class="<%= i % 2 == 0 ? "even" : "odd" %>">
- <td><%=h key %></td>
- <td class="code"><div><%=h val %></div></td>
- </tr>
- <% i += 1 %>
- <% end %>
- </tbody>
- </table>
-<% end %>
-
-<% if request.env["rack.session"] && request.env["rack.session"].any? %>
- <h3>Session</h3>
- <table>
- <thead>
- <tr>
- <th>Variable</th>
- <th>Value</th>
- </tr>
- </thead>
- <tbody>
- <% i = 1 %>
- <% request.env["rack.session"].sort_by { |k, v| k.to_s }.each do |key, val| %>
- <tr class="<%= i % 2 == 0 ? "even" : "odd" %>">
- <td><%=h key %></td>
- <td class="code"><div><%=h val %></div></td>
- </tr>
- <% i += 1 %>
- <% end %>
- </tbody>
- </table>
-<% end %>
-
-<% if request.env["rack.request.cookie_hash"] && request.env["rack.request.cookie_hash"].any? %>
- <h3>Cookies</h3>
- <table>
- <thead>
- <tr>
- <th>Variable</th>
- <th>Value</th>
- </tr>
- </thead>
- <tbody>
- <% i = 1 %>
- <% request.env["rack.request.cookie_hash"].sort_by { |k, v| k.to_s }.each do |key, val| %>
- <tr class="<%= i % 2 == 0 ? "even" : "odd" %>">
- <td><%=h key %></td>
- <td class="code"><div><%=h val %></div></td>
- </tr>
- <% i += 1 %>
- <% end %>
- </tbody>
- </table>
-<% end %>
-
-<h3>Rack ENV</h3>
-<table>
- <thead>
- <tr>
- <th>Variable</th>
- <th>Value</th>
- </tr>
- </thead>
- <tbody>
- <% i = 1 %>
- <% env.sort_by { |k, v| k.to_s }.each do |key, val| %>
- <tr class="<%= i % 2 == 0 ? "even" : "odd" %>">
- <td><%=h key %></td>
- <td class="code"><div><%=h val %></div></td>
- </tr>
- <% i += 1 %>
- <% end %>
- </tbody>
-</table>
+<% end %>

0 comments on commit d7b4e5e

Please sign in to comment.