Class: UnifiedAssets::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/unified_assets/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ Server

Returns a new instance of Server.



4
5
6
7
8
9
10
# File 'lib/unified_assets/server.rb', line 4

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

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/unified_assets/server.rb', line 12

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