Sha256: b5d436ba066412bb5e67e32902e6b6b8d724600c7b4d3b977a06a1d4a229be11

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

module Bullet
  class Rack
    def initialize(app)
      @app = app
    end

    def call(env)
      return @app.call(env) unless Bullet.enable?

      Bullet.start_request
      status, headers, response = @app.call(env)
      return [status, headers, response] if empty?(response)

      response_body = nil
      if Bullet.notification?
        if status == 200 && !response.body.frozen? && html_request?(headers, response)
          response_body = response.body << Bullet.gather_inline_notifications
          headers['Content-Length'] = response_body.length.to_s
        end
        Bullet.perform_out_of_channel_notifications(env)
      end
      Bullet.end_request
      no_browser_cache(headers) if Bullet.disable_browser_cache
      [status, headers, response_body ? [response_body] : response]
    end

    # fix issue if response's body is a Proc
    def empty?(response)
      # response may be ["Not Found"], ["Move Permanently"], etc.
      (response.is_a?(Array) && response.size <= 1) ||
      !response.body.is_a?(String) || response.body.empty?
    end

    def html_request?(headers, response)
      headers['Content-Type'] &&
        headers['Content-Type'].include?('text/html') &&
        response.body.include?("<html>") &&
        response.body.include?("</html>")
    end

    def no_browser_cache(headers)
      headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
      headers["Pragma"] = "no-cache"
      headers["Expires"] = "Wed, 09 Sep 2009 09:09:09 GMT"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bullet-4.1.0 lib/bullet/rack.rb
bullet-4.0.0 lib/bullet/rack.rb
bullet-2.3.1 lib/bullet/rack.rb
bullet-2.3.0 lib/bullet/rack.rb