Skip to content

Instantly share code, notes, and snippets.

@IdahoEv
Created February 3, 2015 22:11
Show Gist options
  • Save IdahoEv/59bfcaaf9e5c3d05037b to your computer and use it in GitHub Desktop.
Save IdahoEv/59bfcaaf9e5c3d05037b to your computer and use it in GitHub Desktop.
module Rack::Insight
class EnableButton
include Render
MIME_TYPES = ["text/plain", "text/html", "application/xhtml+xml"]
def initialize(app, insight)
@app = app
@insight = insight
end
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
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
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
def render
render_template("enable-button")
end
end
end
NoMethodError (undefined method `empty?' for #<ActionDispatch::Response:0x007fd8fac249a0>):
rack-insight (0.5.30) lib/rack/insight/enable-button.rb:16:in `call'
rack-insight (0.5.30) lib/rack/insight/app.rb:62:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment