Class: Cyborg::StaticAssets

Inherits:
Object
  • Object
show all
Defined in:
lib/cyborg/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, path, cache_control = nil, engine_name: nil) ⇒ StaticAssets

Returns a new instance of StaticAssets.



15
16
17
18
19
# File 'lib/cyborg/middleware.rb', line 15

def initialize(app, path, index: 'index', headers: {}, engine_name: nil)
  @app = app
  @engine_name = engine_name
  @file_handler = ActionDispatch::FileHandler.new(path, index: index, headers: headers)
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cyborg/middleware.rb', line 21

def call(env)
  req = Rack::Request.new env
  prefix = File.join Application.config.assets.prefix, @engine_name

  if req.get? || req.head?
    path = req.path_info.chomp('/'.freeze)

    if path.start_with? prefix
      path = path.remove /\A#{prefix}\//

      if match = @file_handler.match?(path)
        req.path_info = match
        return @file_handler.serve(req)
      end
    end
  end

  @app.call(req.env)
end