Sha256: 04a3e3360761d01ce0806a6972f2deddde9aec0b9236ce347a4d008834ca214c

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

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

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

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

      if EnoughFields.notification?
        if status == 200 and !response.body.frozen? and check_html?(headers, response)
          response_body = response.body << EnoughFields.gather_inline_notifications
          headers['Content-Length'] = response_body.length.to_s
        end
        EnoughFields.perform_out_of_channel_notifications
      end
      response_body ||= response.body
      EnoughFields.end_request
      no_browser_cache(headers) if EnoughFields.disable_browser_cache
      [status, headers, [response_body]]
    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 check_html?(headers, response)
      headers['Content-Type'] and headers['Content-Type'].include? 'text/html' and response.body =~ %r{<html.*</html>}m
    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

3 entries across 3 versions & 1 rubygems

Version Path
enough_fields-0.0.3 lib/enough_fields/rack.rb
enough_fields-0.0.2 lib/enough_fields/rack.rb
enough_fields-0.0.1 lib/enough_fields/rack.rb