Class: DevPanel::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/devpanel/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
# File 'lib/devpanel/middleware.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/devpanel/middleware.rb', line 7

def call(env)
  if env["REQUEST_URI"] =~ /__DevPanel\/main/
    [200, { "Content-Type" => "text/html; charset=utf-8" }, [dev_panel_output]]
  elsif env["REQUEST_URI"] =~ /__DevPanel\/set_options/
     params = Rack::Utils.parse_query(env['QUERY_STRING'], "&")
     Stats.set_by_params(params)
    [200, { "Content-Type" => "text/plain; charset=utf-8" }, ["#{Stats.show?} #{Stats.left} #{Stats.top}"]]
  elsif env["REQUEST_URI"] =~ /__DevPanel\/console/
    params = Rack::Utils.parse_query(env['QUERY_STRING'], "&")
    query = params["query"]
    [200, { "Content-Type" => "text/plain; charset=utf-8" }, ["#{CGI::escapeHTML(eval(query).to_s)}"]]
  else
    @app.call(env)
  end
end

#cssObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/devpanel/middleware.rb', line 27

def css
  "    <style>\n      table {\n        width: 100%\n      }\n\n      #devPanelWindow {\n        border-radius: 3px;\n        margin-bottom: 2px;\n        box-shadow: 0 0 10px rgba(0, 0, 0, 0.03), 1px 1px 0 rgba(0, 0, 0, 0.05), -1px 1px 0 rgba(0, 0, 0, 0.05), 0 0 0 4px rgba(0, 0, 0, 0.04);\n        font-family: menlo, lucida console, monospace;\n        border: 3px solid #29488B;\n        z-index: 500000000;\n        padding: 3px; \n        color: #000; \n        background-color: #F0F0F5; \n        position: absolute; \n        float: left;             \n      }\n\n      #devPanelHider {\n        background: #5366EB;\n        box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3);\n        font-family: arial;\n        font-size: 12.8px;\n        overflow: hidden;\n        padding: 6px 10px;\n        border: solid 1px #CCC;\n        border-bottom: 0;\n        border-top-left-radius: 2px;\n        border-top-right-radius: 2px;\n        text-align:left; \n        border: solid 1px #fff;\n      }\n\n      .hider-color {\n        color: #fff\n      }\n\n      #devPanelContainer {\n        font-family: menlo, lucida console, monospace;\n        background-color: #fff;\n        box-shadow: inset 3px 3px 3px rgba(0, 0, 0, 0.1), inset 0 0 0 1px rgba(0, 0, 0, 0.1);\n        width: 300px;\n        padding: 2px           \n      }\n\n      #devPanelContainer td {\n        font-family: arial;\n        font-size: 12px;\n        font-weight: normal;\n        padding: 5px;\n        overflow: auto;\n        letter-spacing: 1.5px\n      }\n\n      #devPanelContainer tr {\n        background-color: #2E2E2E;\n        color: rgb(238, 238, 238);\n        border-bottom: 1px solid #4B4444;\n      }\n\n      #devPanelContainer .alt {\n        background-color: #000000;\n        color: rgb(238, 238, 238);\n        border-bottom: 1px solid #4B4444;\n      }\n\n      #devPanelContainer td.firstColumn {\n        width: 90px;\n        font-weight: bold;\n      }\n\n      #viewTime {\n        font-size: 10px;\n        text-decoration: underline;\n      }\n\n      #partialList {\n        position: absolute;\n        top: 0px;\n        left: 0px;\n        background: #F1F1F1;\n        border: 2px solid #000;\n        background-color: #fff;\n        box-shadow: inset 3px 3px 3px rgba(0, 0, 0, 0.1), inset 0 0 0 1px rgba(0, 0, 0, 0.1);\n        font-family: arial;\n        font-size: 10px;\n        overflow: hidden;\n        padding: 6px 10px;\n        border-top-left-radius: 2px;\n        border-top-right-radius: 2px;\n        display: none;\n        z-index: 500000001;\n      }\n\n      #console {\n        position: absolute;\n        top: 0px;\n        left: 0px;\n        background: #F1F1F1;\n        border: 2px solid #000;\n        background-color: #fff;\n        box-shadow: inset 3px 3px 3px rgba(0, 0, 0, 0.1), inset 0 0 0 1px rgba(0, 0, 0, 0.1);\n        font-family: arial;\n        font-size: 10px;\n        overflow: hidden;\n        padding: 6px 10px;\n        border-top-left-radius: 2px;\n        border-top-right-radius: 2px;\n        display: none;\n        z-index: 500000001;\n        width: 600px;\n      }\n\n      #consoleResults {\n        width: 97%;\n        height: 250px;\n        font-size: 13px;\n        overflow-x: scroll;\n      }\n\n      #consoleInput {\n        width: 97%;\n        height: 20px;\n      }\n\n      .green { background: #21D61A !important}\n      .yellow { background: #BEBE00 !important }\n      .orange { background: #F0A811 !important }\n      .red { background: #B90000 !important }\n\n    </style>\n  css_code\nend\n"

#dev_panel_outputObject



23
24
25
# File 'lib/devpanel/middleware.rb', line 23

def dev_panel_output
  (css + html_containers + html_table).html_safe
end

#first_td(content = "") ⇒ Object



231
232
233
# File 'lib/devpanel/middleware.rb', line 231

def first_td(content = "")
  "<td class='firstColumn'>#{content}</td>"
end

#heat_colorObject



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/devpanel/middleware.rb', line 178

def heat_color
  time = Stats.data[:action_controller].duration.round(0)
  if(time < 500)
     "green"
  elsif(time < 1500)
     "yellow"
  elsif(time < 2500)
     "orange"
  else
     "red"
  end
end

#html_containersObject



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/devpanel/middleware.rb', line 164

def html_containers
  "    <div id='partialList'>\#{partial_list}</div>\n    <div id='console'>\n      <div id=\"consoleResults\">></div>        \n      <input id=\"consoleInput\" type=\"text\" placeholder=\"Type Command\">\n\n    </div>\n    <div id=\"devPanelWindow\" style=\"top: \#{Stats.top.to_s}px; left: \#{Stats.left.to_s}px;\" >\n    <div id=\"devPanelHider\" class=\"\#{heat_color}\"><a class=\"hider-color\" href=\"#\">\#{stats(:controller)}#\#{stats(:action)}</a> / <span class=\"hider-color\" style=\"font-size: 10px\">\#{Stats.data[:action_controller].duration.round(0).to_s}ms</span></div>\n    <div id=\"devPanelContainer\">\n  html_code\nend\n"

#html_tableObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/devpanel/middleware.rb', line 195

def html_table
  table_rows = rowify([
    first_td("Tools:")            + td("<a href='#' id='consoleButton'>Console</a>"),        
    first_td("Total:")            + td("#{Stats.total_duration.to_s}ms"),
    first_td("Controller:")       + td("#{Stats.controller_duration.to_s}ms (#{Stats.controller_duration_percent}%)"),
    first_td("View:")             + td("#{Stats.view_duration.to_s}ms (#{Stats.view_duration_percent}%)"),
    first_td("Partials:")         + td(partial_count),
    first_td("Response:")         + td(stats(:status)),
    first_td("Controller:")       + td(stats(:controller)),
    first_td("Action:")           + td(stats(:action)),
    first_td("Method:")           + td(stats(:method)),
    first_td("Params:")           + td(stats(:params)),
    first_td("Log:")              + td(Stats.data[:log])
  ])

  "<table style='margin: auto; table-layout: fixed'>#{table_rows}</table></div></div>"
end

#partial_countObject



213
214
215
# File 'lib/devpanel/middleware.rb', line 213

def partial_count
  "<div id='viewTime'>#{Stats.data[:partial_count] || 0}</div>"
end

#partial_listObject



217
218
219
220
221
# File 'lib/devpanel/middleware.rb', line 217

def partial_list
  str = ""
  Stats.data[:partials].each_pair {|k,v| str << "#{k}: #{Stats.data[:partials][k]}<br>" } if Stats.data[:partials].present?
  str
end

#rowify(arr) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/devpanel/middleware.rb', line 235

def rowify(arr)
  result = ""
  arr.each_with_index do |data, index|
    result += tr(data, index.even? ? "alt" : "")
  end
  result
end

#stats(symbol) ⇒ Object



191
192
193
# File 'lib/devpanel/middleware.rb', line 191

def stats(symbol)
  Stats.data[:action_controller].payload[symbol]
end

#td(content = "") ⇒ Object



227
228
229
# File 'lib/devpanel/middleware.rb', line 227

def td(content = "")
  "<td>#{content}</td>"
end

#tr(content = "", klass = "") ⇒ Object



223
224
225
# File 'lib/devpanel/middleware.rb', line 223

def tr(content = "", klass="")
  "<tr class=#{klass}>#{content}</tr>"
end