Sha256: e7e8c26a88483b95384528e25172127f59fab6209ebefa3dc6f137f3512d44c3

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module UnifiedAssets
  class Server

    def initialize(app, options)
      @app    = app
      @root   = options[:root] or raise ArgumentError, ":root option is required"
      @assets = options[:assets]
      @minify = options[:minify]
      @debug  = options[:debug]
    end

    def call(env)

      path_info = Rack::Utils.unescape(env["PATH_INFO"])
      path      = File.join(@root, path_info)

      # redirect with trailing slash if necessary
      if File.directory?(path) && path_info !~ /\/$/
        new_path_info = path_info + "/"
        return [
          302,
          { 'Location' => new_path_info, 'Content-Type' => 'text/html' },
          [ "Redirecting you to #{new_path_info}…" ]
        ]
      end

      # add index.html if necessary
      new_env = env.dup
      if File.directory?(path) 
        path_to_index = File.join(path, "index.html")
        if File.file?(path_to_index)
          new_env["PATH_INFO"] = File.join(path_info, "index.html")
        end
      end

      # regenerate assets if necessary
      asset = path_info[1..-1] # trim leading slash
      if @assets && @assets.keys.include?(asset)
        UnifiedAssets.build(@assets[asset], asset, :minify => @minify, :debug => @debug)
      end
    
      @app.call(new_env)

    end

  end # class Server
end # module UnifiedAssets

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unified-assets-0.0.1 lib/unified_assets/server.rb