Class: Rack::WebProfiler::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/web_profiler/router.rb

Overview

Router

Show WebProfiler page if the request path match with one of the webprofiler routes.

Constant Summary collapse

BASE_PATH =
"/_rwpt".freeze

Class Method Summary collapse

Class Method Details

.response_for(request) ⇒ Rack::Reponse, false

Get response for given request.

Parameters:

Returns:

  • (Rack::Reponse, false)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rack/web_profiler/router.rb', line 15

def response_for(request)
  @request = request
  path     = Rack::Utils.unescape(request.path_info)

  # Stop process if the request path does not start
  # by the BASE_PATH.
  return false unless path.start_with?(BASE_PATH)

  path.slice!(BASE_PATH)

  route(request, path)
end

.route(request, path) ⇒ Rack::Reponse, false

Route the request.

Parameters:

Returns:

  • (Rack::Reponse, false)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rack/web_profiler/router.rb', line 34

def route(request, path)
  controller = WebProfiler::Controller.new(request)

  if request.get? && path =~ %r{^\/assets\/(.*)(\/)?$}
    serve_asset(Regexp.last_match(1))
  elsif request.get? && path =~ %r{^\/toolbar\/([a-z0-9]*)(\/)?$}
    controller.show_toolbar(Regexp.last_match(1))
  elsif request.get? && path =~ %r{^\/clean(\/)?$}
    controller.delete
  elsif request.get? && path =~ %r{^(\/)?$}
    controller.index
  elsif request.get? && path =~ %r{^\/([a-z0-9]*)(\/)?$}
    controller.show(Regexp.last_match(1))
  else
    false
  end
end

.serve_asset(path) ⇒ Rack::Response

Serve assets.

Parameters:

  • path (String)

Returns:

  • (Rack::Response)


57
58
59
60
61
62
63
64
65
# File 'lib/rack/web_profiler/router.rb', line 57

def serve_asset(path)
  rf = Rack::File.new(::File.expand_path("../../templates/assets/", __FILE__))
  request = @request.dup
  request.env[PATH_INFO] = "/#{path}"


  status, headers, body = rf.call(request.env)
  Rack::Response.new(body, status, headers)
end

.url_for_asset(path) ⇒ String

Get url for asset.

Parameters:

  • path (String)

Returns:

  • (String)


72
73
74
# File 'lib/rack/web_profiler/router.rb', line 72

def url_for_asset(path)
  "#{get_base_path}/assets/#{path}"
end

.url_for_clean_profilerString

Get url to clean webprofiler.

Returns:

  • (String)


100
101
102
# File 'lib/rack/web_profiler/router.rb', line 100

def url_for_clean_profiler
  "#{get_base_path}/clean"
end

.url_for_profiler(token = nil, panel = nil) ⇒ String

Get url for the webprofiler.

Parameters:

  • token (String, nil) (defaults to: nil)
  • panel (String, nil) (defaults to: nil)

Returns:

  • (String)


91
92
93
94
95
# File 'lib/rack/web_profiler/router.rb', line 91

def url_for_profiler(token = nil, panel = nil)
  query = ""
  query = "?panel=#{panel}" unless panel.nil?
  "#{get_base_path}/#{token}#{query}"
end

.url_for_toolbar(token) ⇒ String

Get url for toobar.

Parameters:

  • token (String)

Returns:

  • (String)


81
82
83
# File 'lib/rack/web_profiler/router.rb', line 81

def url_for_toolbar(token)
  "#{get_base_path}/toolbar/#{token}"
end