Class: Rack::Zippy::AssetServer

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-zippy.rb

Constant Summary collapse

STATIC_EXTENSION_REGEX =

Font extensions: woff, woff2, ttf, eot, otf

/\.(?:css|js|html|htm|txt|ico|png|jpg|jpeg|gif|pdf|svg|zip|gz|eps|psd|ai|woff|woff2|ttf|eot|otf|swf)\z/i
HTTP_STATUS_CODE_OK =
200

Instance Method Summary collapse

Constructor Details

#initialize(app, asset_root = nil) ⇒ AssetServer

Returns a new instance of AssetServer.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rack-zippy.rb', line 17

def initialize(app, asset_root=nil)
  if asset_root.nil?
    if RailsAssetCompiler.rails_env?
      asset_root = ::Rails.public_path
    else
      raise ArgumentError.new 'Please specify asset_root when initializing Rack::Zippy::AssetServer ' +
        '(asset_root is the path to your public directory, often the one with favicon.ico in it)'
    end
  end
  @app = app
  @asset_root = asset_root
  @asset_compiler = resolve_asset_compiler
end

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rack-zippy.rb', line 31

def call(env)
  path_info = env['PATH_INFO']

  return not_found_response if path_info =~ ILLEGAL_PATH_REGEX

  serveable_file = ServeableFile.find_first(
      :path_info => path_info,
      :asset_root => @asset_root,
      :asset_compiler => @asset_compiler,
      :include_gzipped => client_accepts_gzip?(env)
  )

  if serveable_file
    return [HTTP_STATUS_CODE_OK, serveable_file.headers, serveable_file.response_body]
  end

  @app.call(env)
end