Sha256: 28c2d9745e430ca32299f5e962eccb49faa90a1f32da5989f1ca01bce56c5044

Contents?: true

Size: 775 Bytes

Versions: 1

Compression:

Stored size: 775 Bytes

Contents

module Pushify
  class Rack
    def initialize(app)
      @app = app
    end
    
    def call(env)
      path = env["REQUEST_URI"]
      if (Pushify::Rails::Assets.includes?(path))
        Pushify::Rails::Assets.response(path)
      else
        status, headers, response = @app.call(env)

        is_html = !response.is_a?(Array) && (headers["Content-Type"].nil? || headers["Content-Type"].include?("text/html"))

        if (is_html && response.body.match(/<\/body>/))
          pushify_src = Pushify.javascript_src
          response.body = response.body.gsub(/(<\/body>)/, "#{pushify_src}</body>")
          headers["Content-Length"] = (headers["Content-Length"].to_i + pushify_src.size).to_s
        end

        [status, headers, response]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pushify-1.0.0 lib/pushify/rack.rb