Class: Rack::Zippy::AssetServer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, path, max_age_fallback: :day) ⇒ AssetServer

Returns a new instance of AssetServer.

Parameters:

  • app (#call(env))

    the Rack app

  • path (String)

    the path to the public directory, usually where favicon.ico lives

  • max_age_fallback (Fixnum) (defaults to: :day)

    time for Cache-Control header. Defaults to 1 day (in seconds).



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

def initialize(app, path, max_age_fallback: :day)
  assert_path_valid path

  cache_control = cache_control(max_age_fallback)

  @app = app
  blank_app = ->(env) { }
  @static_middleware = ::ActionDispatch::Static.new(blank_app, path, cache_control)
end

Instance Attribute Details

#static_middlewareObject (readonly)

Returns the value of attribute static_middleware.



16
17
18
# File 'lib/rack-zippy.rb', line 16

def static_middleware
  @static_middleware
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  path_info = env[PATH_INFO]
  return not_found_response if illegal_path?(path_info)

  if try_static?(path_info)
    static_response = static_middleware.call(env)
  end

  if static_response
    after_static_responds(env, static_response)
  else
    @app.call(env)
  end
end