Module: Sinatra::AssetPack::BusterHelpers

Extended by:
BusterHelpers
Included in:
BusterHelpers, Package
Defined in:
lib/sinatra/assetpack/buster_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_cache_buster(path, *files) ⇒ Object

Adds a cache buster for the given path.

The 2nd parameter (and beyond) are the files to take mtime from. If the files are not found, the paths will be left as they are.

add_cache_buster('/images/email.png', '/var/www/x/public/images/email.png')


25
26
27
28
29
30
31
32
33
# File 'lib/sinatra/assetpack/buster_helpers.rb', line 25

def add_cache_buster(path, *files)
  hash = cache_buster_hash *files

  if hash
    path.gsub(/(\.[^.]+)$/) { |ext| ".#{hash}#{ext}" }
  else
    path
  end
end

#cache_buster_hash(*files) ⇒ Object

Returns the cache buster suffix for given file(s). This implementation somewhat obfuscates the mtime to not reveal deployment dates.



7
8
9
10
# File 'lib/sinatra/assetpack/buster_helpers.rb', line 7

def cache_buster_hash(*files)
  i = mtime_for(files)
  (i * 4567).to_s.reverse[0...6]  if i
end

#mtime_for(files) ⇒ Object

Returns the maximum mtime for a given list of files. It will return nil if none of them are found.



14
15
16
# File 'lib/sinatra/assetpack/buster_helpers.rb', line 14

def mtime_for(files)
  files.map { |f| File.mtime(f).to_i  if f.is_a?(String) && File.file?(f) }.compact.max
end