Class: Rack::Insight::EnableButton

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

Constant Summary collapse

MIME_TYPES =
["text/plain", "text/html", "application/xhtml+xml"]

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

Constructor Details

#initialize(app, insight) ⇒ EnableButton

Returns a new instance of EnableButton.



7
8
9
10
# File 'lib/rack/insight/enable-button.rb', line 7

def initialize(app, insight)
  @app = app
  @insight = insight
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rack/insight/enable-button.rb', line 12

def call(env)
  @env = env
  status, headers, body = @app.call(@env)

  if !body.nil? && !body.empty?
    response = Rack::Response.new(body, status, headers)
    inject_button(response) if okay_to_modify?(env, response)

    response.to_a
  else
    # Do not inject into assets served by rails or other detritus without a body.
    [status, headers, body]
  end
end

#inject_button(response) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/rack/insight/enable-button.rb', line 38

def inject_button(response)
  full_body = response.body.join
  full_body.sub! /<\/body>/, render + "</body>"

  response["Content-Length"] = full_body.bytesize.to_s

  response.body = [full_body]
end

#okay_to_modify?(env, response) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
# File 'lib/rack/insight/enable-button.rb', line 27

def okay_to_modify?(env, response)
  return false unless response.ok?

  req = Rack::Request.new(env)
  content_type, charset = response.content_type.split(";")
  filters = (env['rack-insight.path_filters'] || []).map { |str| %r(^#{str}) }
  filter = filters.find { |filter| env['REQUEST_PATH'] =~ filter }

  !filter && MIME_TYPES.include?(content_type) && !req.xhr?
end

#renderObject



47
48
49
# File 'lib/rack/insight/enable-button.rb', line 47

def render
  render_template("enable-button")
end