Class: AssetLink::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
8
# File 'lib/asset_link/middleware.rb', line 5

def initialize(app)
  @app = app
  @manifest = load_manifest
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/asset_link/middleware.rb', line 10

def call(env)
  request = Rack::Request.new(env)
  code, headers, body = @app.call(env)

  if URI.unescape(request.path).start_with?('/assets/') && code == 200
    entry = body.respond_to?(:to_a) ? body.to_a.first : body

    asset = case entry
              when Sprockets::Asset
                entry
              when Rack::File::Iterator
                find_asset(entry.path)
            end

    if asset && asset.filename.ends_with?('.link')
      link = File.read(asset.filename).strip # the file contains a link to the actual asset

      open(link) do |f|
        headers = f.meta
        body.close if body.respond_to?(:closed?) && !body.closed?
        body = [f.read]
      end
    end
  end
  [code, headers, body]
end