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
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/devpanel/middleware.rb', line 7

def call(env)
  request_uri = env["REQUEST_URI"]
  params = Rack::Utils.parse_query(env['QUERY_STRING'], "&")

  if request_uri =~ /__DevPanel\/main/
    [200, { "Content-Type" => "text/html; charset=utf-8" }, [dev_panel_output]]
  elsif request_uri =~ /__DevPanel\/set_options/
    Stats.set_by_params(params)
    [200, { "Content-Type" => "text/plain; charset=utf-8" }, ["#{Stats.show?} #{Stats.left} #{Stats.top}"]]
  elsif request_uri =~ /__DevPanel\/console/
    query = params["query"]
    [200, { "Content-Type" => "text/plain; charset=utf-8" }, ["#{CGI::escapeHTML(eval(query).to_s)}"]]
  elsif request_uri =~ /__DevPanel\/assets/
    data = File.read(File.dirname(__FILE__) + '/assets/' + request_uri.split('/__DevPanel/assets/').last)
    [200, { "Content-Type" => "text/plain; charset=utf-8" }, [data]]
  elsif request_uri =~ /__DevPanel\/file/
    filename = request_uri.split('/__DevPanel/file/').last
    data = File.read('/' + filename)
    template = ERB.new File.new("#{File.dirname(__FILE__)}/views/file_viewer.html.erb").read, nil, "%"
    os = OpenStruct.new(data: stats(:data), filename: filename)
    result = template.result(os.instance_eval { binding })
    [200, { "Content-Type" => "text/html; charset=utf-8" }, [result]]
  else
    @app.call(env)
  end
end

#dev_panel_outputObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/devpanel/middleware.rb', line 34

def dev_panel_output
  template = ERB.new File.new("#{File.dirname(__FILE__)}/views/header.html.erb").read, nil, "%"
  os = OpenStruct.new(
    controller: stats(:controller),
    action: stats(:action),
    status: stats(:status),
    partial_count: partial_count,
    total_duration: Stats.total_duration.round(0).to_s,
    controller_duration: Stats.controller_duration.round(0).to_s,
    controller_percent: Stats.controller_duration_percent.to_s,
    view_duration: Stats.view_duration.to_s,
    view_duration_percent: Stats.view_duration_percent,
    log: Stats.data[:log],
    partial_list: Hash[Stats.data[:partials].to_a.reverse],
    partial_paths: Stats.data[:partial_paths] || [],
    params: stats(:params)
    )
  template.result(os.instance_eval { binding }).html_safe
end

#heat_colorObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/devpanel/middleware.rb', line 54

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

#partial_countObject



71
72
73
# File 'lib/devpanel/middleware.rb', line 71

def partial_count
  Stats.data[:partial_count] || 0
end

#stats(symbol) ⇒ Object



67
68
69
# File 'lib/devpanel/middleware.rb', line 67

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