Sha256: 0db04794682f3c8dae1d23e02ac056994aa849de74500ff421fcc28821f1b20f

Contents?: true

Size: 941 Bytes

Versions: 1

Compression:

Stored size: 941 Bytes

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)

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

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

      return response.to_a
    end

    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

    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.28 lib/rack/insight/enable-button.rb