Class: Asset::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/assets/router.rb

Constant Summary collapse

MIME =

Mime types

{'js' => 'application/javascript; charset=UTF-8', 'css' => 'text/css; charset=UTF-8', 'txt' => 'text/plain; charset=UTF-8'}

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Router

Init



8
9
10
# File 'lib/assets/router.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Call



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
45
46
# File 'lib/assets/router.rb', line 13

def call(env)
  # Setting up request
  @request = Rack::Request.new(env)

  # The routes
  case @request.path_info

  # Match /assets?/:type/path
  when /^(\/assets)?\/(js|css)\/(.+)/
    type, path = $2, $3
    path =~ /-([a-f0-9]{1,32})/
    path.gsub!("-#{@key}", '') if (@key = $1)

    # Find the item
    item = ::Asset.manifest.find{|i| i.path == path and i.type == type}

    # Not found if no item, wrong key or no content
    return not_found if !item or (@key and @key != item.key) or !item.content(@key)

    found(item)

  # Bounce favicon requests
  when (::Asset.favicon and /^\/favicon\.ico$/)
    not_found

  # Return a standard robots.txt
  when (::Asset.robots and /^\/robots\.txt$/)
    robots

  else
    # No routes found, pass down the middleware stack
    @app.call(env)
  end
end