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

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

  response = Rack::Response.new(body, status, headers)

  inject_button(response) if okay_to_modify?(env, response)

  return response.to_a
end

#inject_button(response) ⇒ Object



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

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)


23
24
25
26
27
# File 'lib/rack/insight/enable-button.rb', line 23

def okay_to_modify?(env, response)
  return false unless response.ok?
  req = Rack::Request.new(env)
  return MIME_TYPES.include?(req.media_type) && !req.xhr?
end

#renderObject



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

def render
  render_template("enable-button")
end