Class: Asset::Router

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

Overview

The Router class is a small Rack middleware that matches the asset URLs and serves the content, compressed if you are in production mode.

Constant Summary collapse

MIME =

Mime types for responses

{'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



10
11
12
# File 'lib/assets/router.rb', line 10

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Call



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
47
48
# File 'lib/assets/router.rb', line 15

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)\/(.+)/
    # Extract type and path
    type, path = $2, $3

    # Extract digest key and remove from path
    path.gsub!("-#{@key = $1}", '') if path =~ /-([a-f0-9]{32})\.(css|js)$/

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

    # Return the content or not found
    item ? found(item) : not_found

  # 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