class Asset::Router

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

Constants

MIME

Mime types for responses

Public Class Methods

new(app) click to toggle source

Init

# File lib/assets/router.rb, line 10
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source

Thread safe

# File lib/assets/router.rb, line 15
def call(env)
  dup.call!(env)
end
call!(env) click to toggle source

Call

# File lib/assets/router.rb, line 20
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

Private Instance Methods

found(item) click to toggle source

Found

# File lib/assets/router.rb, line 58
def found(item)
  content = item.content(!!@key)
  [ 200, {'Content-Type' => MIME[item.type],
    'Content-Length' => content.bytesize,
    'Cache-Control' => 'public, max-age=86400',
    'Expires' => (Time.now.utc + (86400 * 30)).httpdate,
    'Last-Modified' => item.modified.httpdate,
  }, [content]]
end
not_found() click to toggle source

Not found

# File lib/assets/router.rb, line 69
def not_found
  [404, {'Content-Type' => MIME['txt'], 'Content-Length' => 0}, []]
end
robots() click to toggle source

Robots

# File lib/assets/router.rb, line 74
def robots
  s = %{Sitemap: #{@request.scheme}://#{@request.host}/sitemap.xml}
  [200, {'Content-Type' => MIME['txt'],'Content-Length' => s.bytesize}, [s]]
end