Sha256: 97c53dfb30867675e01059b9776c516fd2b22b2de475c9d0e0060cfe88dea8e3

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

module SmartAssets
  class Rack

    def initialize(app, prefix, cache_control)
      @app = app
      @prefix = prefix
      @cache_control = cache_control
    end

    def call(env)
      unless is_asset?(env) && (digest_asset=digest_name(env))
        return @app.call(env)
      end

      digest_path = File.join(manifest.dir, digest_asset)
      if File.exists?(digest_path)
        env['PATH_INFO'] = "#{@prefix}/#{digest_asset}"
      end
      status, headers, body = @app.call(env)
      if [200, 206].include?(status)
        headers['Cache-Control'] = @cache_control
        headers['ETag'] ||= %("#{digest(digest_asset)}")
      end
      [status, headers, body]
    end


    def is_asset?(env)
      case env['REQUEST_METHOD']
      when 'GET', 'HEAD'
        env['PATH_INFO'].start_with?(@prefix)
      else
        false
      end
    end

    def base_path(env)
      env['PATH_INFO'].sub(%r{^#{@prefix}/}, '')
    end

    def digest_name(env)
      manifest.assets[base_path(env)]
    end

    def digest(name)
      manifest.files[name]['digest']
    end

    def manifest
      ActionView::Base.assets_manifest
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
smart_assets-0.4.0 lib/smart_assets/rack.rb
smart_assets-0.3.0 lib/smart_assets/rack.rb
smart_assets-0.2.0 lib/smart_assets/rack.rb
smart_assets-0.1.0 lib/smart_assets/rack.rb
smart_assets-0.0.1 lib/smart_assets/rack.rb