Module: CdnHelpers::AssetPath

Defined in:
lib/cdn_helpers.rb

Class Method Summary collapse

Class Method Details

.asset_cacheObject



10
11
12
# File 'lib/cdn_helpers.rb', line 10

def self.asset_cache
  @@asset_cache ||= {}
end

.call(path) ⇒ Object



14
15
16
# File 'lib/cdn_helpers.rb', line 14

def self.call(path)
  hash_file(path, Rails.root.join('public'), Rails.logger)
end

.hash_file(path, public_root_path, logger) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cdn_helpers.rb', line 18

def self.hash_file(path, public_root_path, logger)
  unless asset_cache.has_key?(path)
    file_path = public_root_path.join(path.sub(/^\//, '')).to_s
    unless File.file?(file_path)
      logger.warn("Cannot rewrite URL for #{path}: File not found")
      return path
    end
    sha1 = Digest::SHA1.file(file_path).hexdigest
    extension = File.extname(path)
    return asset_cache[path] = path + "/#{sha1[0..7]}#{hash_salt}#{extension}"
  end
  asset_cache[path]
end

.hash_saltObject



32
33
34
# File 'lib/cdn_helpers.rb', line 32

def self.hash_salt
  @@hash_salt ||= 'X'
end

.set_hash_salt(value) ⇒ Object



36
37
38
# File 'lib/cdn_helpers.rb', line 36

def self.set_hash_salt(value)
  @@hash_salt = value
end