juicer 1.0.22 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.22
1
+ 1.1.0
@@ -3,7 +3,7 @@ require "logger"
3
3
  module Juicer
4
4
 
5
5
  # :stopdoc:
6
- VERSION = "1.0.22"
6
+ VERSION = "1.1.0"
7
7
  LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
8
8
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
9
9
  LOGGER = Logger.new(STDOUT)
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ require 'digest/md5'
2
3
 
3
4
  module Juicer
4
5
  #
@@ -80,7 +81,7 @@ module Juicer
80
81
  #
81
82
  def self.path(file, type = :soft, parameter = DEFAULT_PARAMETER)
82
83
  return file if file =~ /data:.*;base64/
83
- type = [:soft, :hard, :rails].include?(type) ? type : :soft
84
+ type = [:soft, :hard, :rails, :md5].include?(type) ? type : :soft
84
85
  parameter = nil if type == :rails
85
86
  file = self.clean(file, parameter)
86
87
  filename = file.split("?").first
@@ -92,11 +93,25 @@ module Juicer
92
93
  return "#{file}#{file.index('?') ? '&' : '?'}#{parameter}#{mtime}"
93
94
  elsif type == :rails
94
95
  return "#{file}#{file.index('?') ? '' : "?#{mtime}"}"
96
+ elsif type == :md5
97
+ md5 = Digest::MD5.hexdigest(File.read(filename))
98
+ return file.sub(/(\.[^\.]+$)/, "-#{parameter}#{md5}" + '\1')
95
99
  end
96
100
 
97
101
  file.sub(/(\.[^\.]+$)/, "-#{parameter}#{mtime}" + '\1')
98
102
  end
99
103
 
104
+ #
105
+ # Add a md5 cache buster to a filename. The parameter is an optional prefix
106
+ # that is added before the md5 digest. It results in filenames of the form:
107
+ # <tt>file-[parameter name][md5].suffix</tt>, ie
108
+ # <tt>images/logo-cb4fdbd4c637ad377adf0fc0c88f6854b3.png</tt> which is the case for the default
109
+ # parameter name "cb" (as in *c*ache *b*uster).
110
+ #
111
+ def self.md5(file, parameter = DEFAULT_PARAMETER)
112
+ self.path(file, :md5, parameter)
113
+ end
114
+
100
115
  #
101
116
  # Add a hard cache buster to a filename. The parameter is an optional prefix
102
117
  # that is added before the mtime timestamp. It results in filenames of the form:
@@ -140,7 +155,7 @@ module Juicer
140
155
  new_file = file.sub(/#{query_param}\d+&?/, "").sub(/(\?|&)$/, "")
141
156
  return new_file unless new_file == file
142
157
 
143
- file.sub(/-#{parameter}\d+(\.\w+)($|\?)/, '\1\2')
158
+ file.sub(/-#{parameter}[0-9a-f]+(\.\w+)($|\?)/, '\1\2')
144
159
  end
145
160
  end
146
161
  end
@@ -75,11 +75,11 @@ the compressor the path should be the path to where the jar file is found.
75
75
  opt.on("-b", "--absolute-urls", "Convert all referenced URLs to absolute URLs. Requires --document-root.\n" +
76
76
  (" " * 37) + "Works with cycled asset hosts. Only valid for CSS files") { |t| @absolute_urls = true }
77
77
  opt.on("-d", "--document-root dir", "Path to resolve absolute URLs relative to") { |path| @document_root = path }
78
- opt.on("-c", "--cache-buster type", "none, soft, rails, or hard. Default is soft, which adds timestamps to\n" +
78
+ opt.on("-c", "--cache-buster type", "none, soft, rails, hard or md5. Default is soft, which adds timestamps to\n" +
79
79
  (" " * 37) + "reference URLs as query parameters. None leaves URLs untouched, rails adds\n" +
80
- (" " * 37) + "timestamps in the same format as Rails' image_tag helper, and hard alters\n" +
80
+ (" " * 37) + "timestamps in the same format as Rails' image_tag helper. Hard and md5 alters\n" +
81
81
  (" " * 37) + "file names") do |type|
82
- @cache_buster = [:soft, :hard, :rails].include?(type.to_sym) ? type.to_sym : nil
82
+ @cache_buster = [:soft, :hard, :rails, :md5].include?(type.to_sym) ? type.to_sym : nil
83
83
  end
84
84
  opt.on("-e", "--embed-images type", "none or data_uri. Default is none. Data_uri embeds images using Base64 encoding\n" +
85
85
  (" " * 37) + "None leaves URLs untouched. Candiate images must be flagged with '?embed=true to be considered") do |embed|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: juicer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.22
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: