class Rack::WebProfiler::Controller

Controller

Generate the views of the WebProfiler.

Public Class Methods

new(request) click to toggle source

Initialize.

@param request [Rack::WebProfiler::Request]

# File lib/rack/web_profiler/controller.rb, line 13
def initialize(request)
  @request = request
end

Public Instance Methods

delete() click to toggle source

Clean the webprofiler.

@return [Rack::Response]

# File lib/rack/web_profiler/controller.rb, line 72
def delete
  Rack::WebProfiler::Model.clean!

  redirect WebProfiler::Router.url_for_profiler
end
index() click to toggle source

List the webprofiler history.

@return [Rack::Response]

# File lib/rack/web_profiler/controller.rb, line 20
def index
  @collections = Rack::WebProfiler::Model::CollectionRecord.order(Sequel.desc(:created_at))
    .limit(20)

  return json(@collections, to_json_opts: {
    only: [:token, :http_method, :http_status, :url, :ip]
  }) if prefer_json?

  erb "panel/index.erb", layout: "panel/layout.erb"
end
show(token) click to toggle source

Show the webprofiler panel.

@param token [String] The collection token

@return [Rack::Response]

# File lib/rack/web_profiler/controller.rb, line 36
def show(token)
  @collection = Rack::WebProfiler::Model::CollectionRecord[token: token]
  return not_found if @collection.nil?

  @collectors = Rack::WebProfiler.config.collectors.all
  @collector  = nil

  unless @request.params["panel"].nil?
    @collector = @collectors[@request.params["panel"].to_sym]
  else
    @collector = @collectors.values.first
  end

  return not_found if @collector.nil?

  return json(@collection) if prefer_json?
  erb "panel/show.erb", layout: "panel/layout.erb"
end
show_toolbar(token) click to toggle source

Print the webprofiler toolbar.

@param token [String] The collection token

@return [Rack::Response]

# File lib/rack/web_profiler/controller.rb, line 60
def show_toolbar(token)
  @collection = Rack::WebProfiler::Model::CollectionRecord[token: token]
  return erb nil, status: 404 if @collection.nil?

  @collectors = Rack::WebProfiler.config.collectors.all

  erb "profiler.erb"
end

Private Instance Methods

erb(path, layout: nil, variables: binding, status: 200) click to toggle source

Render a HTML reponse from an ERB template.

@param path [String] Path to the ERB template @option layout [String, nil] Path to the ERB layout @option variables [Hash, nil] List of variables to the view @option status [Integer] HTTP status code

@return [Rack::Response]

@private

# File lib/rack/web_profiler/controller.rb, line 130
def erb(path, layout: nil, variables: binding, status: 200)
  v = WebProfiler::View.new(path, layout: layout)

  Rack::Response.new(v.result(variables) || "", status, {
    "Content-Type" => "text/html",
  })
end
json(data = {}, status: 200, to_json_opts: {}) click to toggle source

Render a JSON response from an Array or a Hash.

@param data [Array, Hash] Data @option status [Integer] @option to_json_opts [Hash]

@return [Rack::Response]

@private

# File lib/rack/web_profiler/controller.rb, line 147
def json(data = {}, status: 200, to_json_opts: {})
  Rack::Response.new(data.send(:to_json, to_json_opts), status, {
    "Content-Type" => "application/json",
  })
end
not_found() click to toggle source

404 page.

@return [Rack::Response]

@private

# File lib/rack/web_profiler/controller.rb, line 116
def not_found
  erb "404.erb", layout: "panel/layout.erb", status: 404
end
prefer_json?() click to toggle source

Is “application/json” reponse is prefered?

@return [Boolean]

@private

# File lib/rack/web_profiler/controller.rb, line 85
def prefer_json?
  prefered_http_accept == "application/json"
end
prefered_http_accept() click to toggle source

Returns the prefered Content-Type response between html and json.

@return [String]

@private

# File lib/rack/web_profiler/controller.rb, line 94
def prefered_http_accept
  Rack::Utils.best_q_match(@request.env["HTTP_ACCEPT"], %w(text/html application/json))
end
redirect(path) click to toggle source

Redirection.

@param path [string]

@return [Rack::Response]

@private

# File lib/rack/web_profiler/controller.rb, line 105
def redirect(path)
  Rack::Response.new([], 302, {
    "Location" => "#{@request.base_url}#{path}",
  })
end