Sha256: fa3c5bd3b685f0cc413cec2262c14a106138598c62902ada7be8c868c61bfe93

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-insight-0.5.30 lib/rack/insight/enable-button.rb