Class: SmartAssets::Rack

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

Instance Method Summary collapse

Constructor Details

#initialize(app, prefix, cache_control) ⇒ Rack

Returns a new instance of Rack.



4
5
6
7
8
# File 'lib/smart_assets/rack.rb', line 4

def initialize(app, prefix, cache_control)
  @app = app
  @prefix = prefix
  @cache_control = cache_control
end

Instance Method Details

#base_path(env) ⇒ Object



37
38
39
# File 'lib/smart_assets/rack.rb', line 37

def base_path(env)
  env['PATH_INFO'].sub(%r{^#{@prefix}/}, '')
end

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/smart_assets/rack.rb', line 10

def call(env)
  unless is_asset?(env) && (digest_asset=digest_name(env))
    return @app.call(env)
  end

  digest_path = File.join(manifest.dir, digest_asset)
  if File.exists?(digest_path)
    env['PATH_INFO'] = "#{@prefix}/#{digest_asset}"
  end
  status, headers, body = @app.call(env)
  if [200, 206].include?(status)
    headers['Cache-Control'] = @cache_control
    headers['ETag'] ||= %("#{digest(digest_asset)}")
  end
  [status, headers, body]
end

#digest(name) ⇒ Object



45
46
47
# File 'lib/smart_assets/rack.rb', line 45

def digest(name)
  manifest.files[name]['digest']
end

#digest_name(env) ⇒ Object



41
42
43
# File 'lib/smart_assets/rack.rb', line 41

def digest_name(env)
  manifest.assets[base_path(env)]
end

#is_asset?(env) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
# File 'lib/smart_assets/rack.rb', line 28

def is_asset?(env)
  case env['REQUEST_METHOD']
  when 'GET', 'HEAD'
    env['PATH_INFO'].start_with?(@prefix)
  else
    false
  end
end

#manifestObject



49
50
51
# File 'lib/smart_assets/rack.rb', line 49

def manifest
  ActionView::Base.assets_manifest
end