Class: Rack::Insight::EnableButton

Inherits:
Struct
  • Object
show all
Includes:
Render
Defined in:
lib/rack/insight/enable-button.rb

Constant Summary collapse

CONTENT_TYPE_REGEX =
/text\/(html|plain)|application\/xhtml\+xml/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Render

#compile, #compile!, #compiled_source, #method_name, #method_name_without_locals, #render_template, #signed_params

Methods included from Logging

logger, verbose, verbosity

Instance Attribute Details

#appObject

Returns the value of attribute app

Returns:

  • (Object)

    the current value of app



2
3
4
# File 'lib/rack/insight/enable-button.rb', line 2

def app
  @app
end

#insightObject

Returns the value of attribute insight

Returns:

  • (Object)

    the current value of insight



2
3
4
# File 'lib/rack/insight/enable-button.rb', line 2

def insight
  @insight
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
# File 'lib/rack/insight/enable-button.rb', line 7

def call(env)
  status, headers, response = app.call(env)

  if okay_to_modify?(env, headers)
    body = response.inject("") do |memo, part|
      memo << part
      memo
    end
    index = body.rindex("</body>")
    if index
      body.insert(index, render)
      headers["Content-Length"] = body.bytesize.to_s
      response = [body]
    end
  end

  [status, headers, response]
end

#match_path_filters?(path_filters, path) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rack/insight/enable-button.rb', line 31

def match_path_filters?(path_filters, path)
  to_regex(path_filters).find { |filter| path =~ filter }
end

#okay_to_modify?(env, headers) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/rack/insight/enable-button.rb', line 26

def okay_to_modify?(env, headers)
  return false unless headers["Content-Type"] =~ CONTENT_TYPE_REGEX
  return !match_path_filters?(env["rack-insight.path_filters"], env["REQUEST_PATH"])
end

#renderObject



39
40
41
# File 'lib/rack/insight/enable-button.rb', line 39

def render
  render_template("enable-button")
end

#to_regex(filters) ⇒ Object



35
36
37
# File 'lib/rack/insight/enable-button.rb', line 35

def to_regex(filters)
  (filters || []).map { |str| %r(^#{str}) }
end