Sha256: f21ba5f23a6b79ffb5955ff09d3cdf1473c1dfdd2625286eb41e8051a4b47067

Contents?: true

Size: 1.1 KB

Versions: 16

Compression:

Stored size: 1.1 KB

Contents

module Sinatra
  module AssetPack
    module BusterHelpers
      extend self
      # Returns the cache buster suffix for given file(s).
      # This implementation somewhat obfuscates the mtime to not reveal deployment dates.
      def cache_buster_hash(*files)
        i = mtime_for(files)
        (i * 4567).to_s.reverse[0...6]  if i
      end

      # Returns the maximum mtime for a given list of files.
      # It will return nil if none of them are found.
      def mtime_for(files)
        files.map { |f| File.mtime(f).to_i  if f.is_a?(String) && File.file?(f) }.compact.max
      end

      # 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')
      #
      def add_cache_buster(path, *files)
        hash = cache_buster_hash *files

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

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
sinatra-assetpack-0.1.7 lib/sinatra/assetpack/buster_helpers.rb
sundawg-sinatra-assetpack-fork-0.1.6 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.1.6 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.1.5 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.1.4 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.1.3 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.1.2 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.1.1 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.1.0 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-flexible-compression-0.0.1 lib/sinatra/assetpack/buster_helpers.rb
sundawg-sinatra-assetpack-fork-0.0.12.pre1 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.0.12.pre1 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.0.11 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.0.10 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.0.9 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.0.8 lib/sinatra/assetpack/buster_helpers.rb