Skip to content
This repository
Browse code

DRY up template and output for request/env panel

  • Loading branch information...
commit d7b4e5eca0c21458a9e6430e16fb95d608e70c69 1 parent 9147c30
Tim Connor authored March 25, 2010 Luke Melia committed May 31, 2010
29  lib/rack/bug/panels/request_variables_panel.rb
@@ -16,7 +16,34 @@ def heading
16 16
       end
17 17
 
18 18
       def content
19  
-        render_template "panels/request_variables", :request => @request, :env => @env
  19
+        sections = {}
  20
+        sections["GET"] = sort(@request.GET) if @request.GET.any?
  21
+        sections["POST"] = sort(@request.GET) if @request.POST.any?
  22
+        sections["Session"] = sort(@request.env["rack.session"]) if @request.env["rack.session"] && @request.env["rack.session"].any?
  23
+        sections["Cookies"] = sort(@request.env["rack.request.cookie_hash"]) if @request.env["rack.request.cookie_hash"] && @request.env["rack.request.cookie_hash"].any?
  24
+        server, rack = split_and_filter_env(@env)
  25
+        sections["SERVER VARIABLES"] = sort(server)
  26
+        sections["Rack ENV"] = sort(rack)
  27
+        render_template "panels/request_variables", :sections => sections
  28
+      end
  29
+
  30
+    private
  31
+      def sort(hash)
  32
+        hash.sort_by { |k, v| k.to_s }
  33
+      end
  34
+
  35
+      def split_and_filter_env(env)
  36
+        server, rack = {}, {}
  37
+        env.each do |k,v|
  38
+          if k.index("rack.") == 0
  39
+            rack[k] = v.to_s
  40
+          elsif k.index("rack-bug.") == 0
  41
+            #don't output the rack-bug variables - especially secret_key
  42
+          else
  43
+            server[k] = v.to_s
  44
+          end
  45
+        end
  46
+        return server, rack
20 47
       end
21 48
 
22 49
     end
96  lib/rack/bug/views/panels/request_variables.html.erb
... ...
@@ -1,5 +1,7 @@
1  
-<% if request.GET.any? %>
2  
-  <h3>GET</h3>
  1
+<% ["GET", "POST", "Session", "Cookies", "SERVER VARIABLES", "Rack ENV"].each do |header|
  2
+     next unless sections.has_key?(header)
  3
+%>
  4
+  <h3><%=header%></h3>
3 5
   <table>
4 6
     <thead>
5 7
       <tr>
@@ -9,7 +11,7 @@
9 11
     </thead>
10 12
     <tbody>
11 13
       <% i = 1 %>
12  
-      <% request.GET.sort_by { |k, v| k.to_s }.each do |key, val| %>
  14
+      <% sections[header].each do |key, val| %>
13 15
         <tr class="<%= i % 2 == 0 ? "even" : "odd" %>">
14 16
           <td><%=h key %></td>
15 17
           <td class="code"><div><%=h val %></div></td>
@@ -18,90 +20,4 @@
18 20
       <% end %>
19 21
     </tbody>
20 22
   </table>
21  
-<% end %>
22  
-
23  
-<% if request.POST.any? %>
24  
-  <h3>POST</h3>
25  
-  <table>
26  
-    <thead>
27  
-      <tr>
28  
-        <th>Variable</th>
29  
-        <th>Value</th>
30  
-      </tr>
31  
-    </thead>
32  
-    <tbody>
33  
-      <% i = 1 %>
34  
-      <% request.POST.sort_by { |k, v| k.to_s }.each do |key, val| %>
35  
-        <tr class="<%= i % 2 == 0 ? "even" : "odd" %>">
36  
-          <td><%=h key %></td>
37  
-          <td class="code"><div><%=h val %></div></td>
38  
-        </tr>
39  
-        <% i += 1 %>
40  
-      <% end %>
41  
-    </tbody>
42  
-  </table>
43  
-<% end %>
44  
-
45  
-<% if request.env["rack.session"] && request.env["rack.session"].any? %>
46  
-  <h3>Session</h3>
47  
-  <table>
48  
-    <thead>
49  
-      <tr>
50  
-        <th>Variable</th>
51  
-        <th>Value</th>
52  
-      </tr>
53  
-    </thead>
54  
-    <tbody>
55  
-      <% i = 1 %>
56  
-      <% request.env["rack.session"].sort_by { |k, v| k.to_s }.each do |key, val| %>
57  
-        <tr class="<%= i % 2 == 0 ? "even" : "odd" %>">
58  
-          <td><%=h key %></td>
59  
-          <td class="code"><div><%=h val %></div></td>
60  
-        </tr>
61  
-        <% i += 1 %>
62  
-      <% end %>
63  
-    </tbody>
64  
-  </table>
65  
-<% end %>
66  
-
67  
-<% if request.env["rack.request.cookie_hash"] && request.env["rack.request.cookie_hash"].any? %>
68  
-  <h3>Cookies</h3>
69  
-  <table>
70  
-    <thead>
71  
-      <tr>
72  
-        <th>Variable</th>
73  
-        <th>Value</th>
74  
-      </tr>
75  
-    </thead>
76  
-    <tbody>
77  
-      <% i = 1 %>
78  
-      <% request.env["rack.request.cookie_hash"].sort_by { |k, v| k.to_s }.each do |key, val| %>
79  
-        <tr class="<%= i % 2 == 0 ? "even" : "odd" %>">
80  
-          <td><%=h key %></td>
81  
-          <td class="code"><div><%=h val %></div></td>
82  
-        </tr>
83  
-        <% i += 1 %>
84  
-      <% end %>
85  
-    </tbody>
86  
-  </table>
87  
-<% end %>
88  
-
89  
-<h3>Rack ENV</h3>
90  
-<table>
91  
-  <thead>
92  
-    <tr>
93  
-      <th>Variable</th>
94  
-      <th>Value</th>
95  
-    </tr>
96  
-  </thead>
97  
-  <tbody>
98  
-    <% i = 1 %>
99  
-    <% env.sort_by { |k, v| k.to_s }.each do |key, val| %>
100  
-      <tr class="<%= i % 2 == 0 ? "even" : "odd" %>">
101  
-        <td><%=h key %></td>
102  
-        <td class="code"><div><%=h val %></div></td>
103  
-      </tr>
104  
-      <% i += 1 %>
105  
-    <% end %>
106  
-  </tbody>
107  
-</table>
  23
+<% end %>

0 notes on commit d7b4e5e

Please sign in to comment.
Something went wrong with that request. Please try again.