Sha256: 84fca66639b61e1acf6c40d958ba7a37ecd3ad67fe96161e16da0d2d2636f298

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

require 'rack'
require 'nokogiri'

module Rack
  class PageSpeed
    attr_reader :config
    load "#{::File.dirname(__FILE__)}/pagespeed/filters/all.rb"
    autoload :Config, 'rack/pagespeed/config'

    def initialize app, options, &block
      @app = app
      @config = Config.new options, &block
    end

    def call env
      if match = %r(^/rack-pagespeed-(.*)).match(env['PATH_INFO'])
        respond_with match[1]
      else
        status, headers, @response = @app.call(env)
        return [status, headers, @response] unless headers['Content-Type'] =~ /html/
        body = ""; @response.each do |part| body << part end
        @document = Nokogiri::HTML(body)
        @config.filters.each do |filter|
          filter.execute! @document
        end
        body = @document.to_html
        headers['Content-Length'] = body.length.to_s if headers['Content-Length'] # still UTF-8 unsafe
        [status, headers, [body]]        
      end
    end
    
    def respond_with asset_id
      store = @config.store
      if asset = store[asset_id]
        [
          200,
          { 'Content-Type' => (Rack::Mime.mime_type(::File.extname(asset_id))) },
          [asset]
        ]
      else
        [404, {'Content-Type' => 'text/plain'}, ['Not found']]
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rack-pagespeed-1.0.5 lib/rack/pagespeed.rb
rack-pagespeed-1.0.4 lib/rack/pagespeed.rb
rack-pagespeed-1.0.3 lib/rack/pagespeed.rb
rack-pagespeed-1.0.2 lib/rack/pagespeed.rb
rack-pagespeed-1.0.1 lib/rack/pagespeed.rb
rack-pagespeed-1.0.0 lib/rack/pagespeed.rb
rack-pagespeed-0.2.0 lib/rack/pagespeed.rb