Class: Rack::WebProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/web_profiler.rb,
lib/rack/web_profiler/view.rb,
lib/rack/web_profiler/model.rb,
lib/rack/web_profiler/version.rb,
lib/rack/web_profiler/collector.rb,
lib/rack/web_profiler/controller.rb

Overview

WebProfiler

Defined Under Namespace

Modules: Rouge Classes: Collector, Collectors, Config, Controller, Engine, Model, Request, Response, Router, RuntimeError, Utils, View

Constant Summary collapse

ENV_RUNTIME_START =

Env key constants.

"rack_webprofiler.runtime_start".freeze
ENV_RUNTIME =
"rack_webprofiler.runtime".freeze
ENV_EXCEPTION =
"rack_webprofiler.exception".freeze
VERSION =
"0.1.3".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, tmp_dir: nil) ⇒ WebProfiler

Initialize.

Parameters:

  • app (Proc)
  • tmp_dir (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (tmp_dir:):

  • (String)


92
93
94
95
96
97
# File 'lib/rack/web_profiler.rb', line 92

def initialize(app, tmp_dir: nil)
  @app = app

  WebProfiler.config.tmp_dir = tmp_dir unless tmp_dir.nil?
  WebProfiler.config(&Proc.new) if block_given?
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



86
87
88
# File 'lib/rack/web_profiler.rb', line 86

def data
  @data
end

Class Method Details

.config { ... } ⇒ Rack::WebProfiler::Config

Configure the WebProfiler.

Yields:

  • the Config object.

Returns:



38
39
40
41
42
# File 'lib/rack/web_profiler.rb', line 38

def config
  @config ||= Config.new
  @config.build!(&Proc.new) if block_given?
  @config
end

.data(k = nil, v = :undefined) ⇒ Object

Data container.

Parameters:

  • key (String, Symbol, nil)
  • value

Returns:



71
72
73
74
75
76
77
78
# File 'lib/rack/web_profiler.rb', line 71

def data(k = nil, v = :undefined)
  @data ||= {}

  return @data if k === nil

  @data[k] = v unless v === :undefined
  @data[k] if @data.key?(k)
end

.register_collector(collector_class) ⇒ Object Also known as: register_collectors

Register one or many collectors.

Parameters:

  • collector_class (Array, Class)


52
53
54
# File 'lib/rack/web_profiler.rb', line 52

def register_collector(collector_class)
  config.collectors.add_collector collector_class
end

.reset_collectors!Object

Reset collectors.



45
46
47
# File 'lib/rack/web_profiler.rb', line 45

def reset_collectors!
  config.collectors.reset!
end

.reset_data!Object

Reset data container.



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

def reset_data!
  @data = {}
end

.unregister_collector(collector_class) ⇒ Object Also known as: unregister_collectors

Unregister one or many collectors.

Parameters:

  • collector_class (Array, Class)


60
61
62
# File 'lib/rack/web_profiler.rb', line 60

def unregister_collector(collector_class)
  config.collectors.remove_collector collector_class
end

Instance Method Details

#call(env) ⇒ Array

Call.

Parameters:

  • env (Hash)

Returns:

  • (Array)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rack/web_profiler.rb', line 104

def call(env)
  WebProfiler.reset_data!

  begin
    request = WebProfiler::Request.new(env)
    env[ENV_RUNTIME_START] = Time.now.to_f

    response = WebProfiler::Router.response_for(request)
    return response.finish if response.is_a? Rack::Response

    status, headers, body = @app.call(env)
  rescue => e
    process(request, body, status, headers, e)
    raise
  end

  process(request, body, status, headers)
end