Class: Insight::EnableButton

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

Constant Summary

MIME_TYPES =
["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

Constructor Details

- (EnableButton) initialize(app, insight)

A new instance of EnableButton



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

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

Instance Method Details

- (Object) call(env)



12
13
14
15
16
17
18
19
20
21
# File 'lib/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

- (Object) inject_button(response)



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

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

- (Boolean) okay_to_modify?(env, response)

Returns:

  • (Boolean)


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

def okay_to_modify?(env, response)
  req = Rack::Request.new(env)
  content_type, charset = response.content_type.split(";")

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

- (Object) render



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

def render
  render_template("enable-button")
end