asset_fingerprinter 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Guardfile +14 -0
- data/lib/asset_fingerprinter.rb +3 -13
- data/lib/asset_fingerprinter/fingerprinter.rb +16 -0
- data/lib/asset_fingerprinter/path_rewriter.rb +18 -0
- data/lib/asset_fingerprinter/version.rb +1 -1
- data/spec/asset_fingerprinter_spec.rb +41 -0
- data/spec/spec_helper.rb +5 -0
- metadata +11 -5
data/Guardfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :version => 2, :cli => '--colour' do
|
5
|
+
watch(%r{^spec/.+_spec\.rb})
|
6
|
+
watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
end
|
9
|
+
|
10
|
+
guard 'bundler' do
|
11
|
+
watch('Gemfile')
|
12
|
+
# Uncomment next line if Gemfile contain `gemspec' command
|
13
|
+
# watch(/^.+\.gemspec/)
|
14
|
+
end
|
data/lib/asset_fingerprinter.rb
CHANGED
@@ -1,14 +1,4 @@
|
|
1
|
-
require 'digest/md5'
|
2
|
-
|
3
1
|
module AssetFingerprinter
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
@cache = Hash.new{ |hash,key| hash[key] = Digest::MD5.hexdigest(File.read(key)) }
|
8
|
-
end
|
9
|
-
|
10
|
-
def call(path)
|
11
|
-
@cache[path]
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
2
|
+
autoload :Fingerprinter, 'asset_fingerprinter/fingerprinter'
|
3
|
+
autoload :PathRewriter, 'asset_fingerprinter/path_rewriter'
|
4
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
|
3
|
+
module AssetFingerprinter
|
4
|
+
module Fingerprinter
|
5
|
+
class Digest
|
6
|
+
# Digests a file and returns the cachebusted path
|
7
|
+
def initialize
|
8
|
+
@cache = Hash.new{ |hash,key| hash[key] = ::Digest::MD5.hexdigest(File.read(key)) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(path)
|
12
|
+
@cache[path]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module AssetFingerprinter
|
2
|
+
class PathRewriter
|
3
|
+
module Format
|
4
|
+
Filename = lambda{|path, token| "#{$1}-fp-#{token}#{$2}" if path =~ /^(.*)(\.\w+)$/ }
|
5
|
+
QueryString = lambda{|path, token| "#{path}?#{token}" }
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_accessor :fingerprinter, :pattern
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
yield self if block_given?
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(path)
|
15
|
+
pattern.call(path, fingerprinter.call(path))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'digest/md5'
|
3
|
+
|
4
|
+
describe Fingerprinter::Digest do
|
5
|
+
it "should fingerprint a file" do
|
6
|
+
digest = Digest::MD5.hexdigest File.read(__FILE__)
|
7
|
+
Fingerprinter::Digest.new.call(__FILE__).should eql(digest)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe PathRewriter do
|
12
|
+
before(:each) do
|
13
|
+
@path = __FILE__
|
14
|
+
@dir, @file = File.split(@path)
|
15
|
+
@ext = File.extname(@file)
|
16
|
+
@base = File.basename(@file, @ext)
|
17
|
+
@digest = Digest::MD5.hexdigest File.read(__FILE__)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should rewrite filename path" do
|
21
|
+
expected_path = File.expand_path(File.join(@dir, "#{@base}-fp-#{@digest}#{@ext}"))
|
22
|
+
|
23
|
+
pr = PathRewriter.new do |pr|
|
24
|
+
pr.fingerprinter = Fingerprinter::Digest.new
|
25
|
+
pr.pattern = PathRewriter::Format::Filename
|
26
|
+
end
|
27
|
+
|
28
|
+
pr.call(@path).should eql(expected_path)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should rewrite queryname path" do
|
32
|
+
expected_path = "#{__FILE__}?#{@digest}"
|
33
|
+
|
34
|
+
pr = PathRewriter.new do |pr|
|
35
|
+
pr.fingerprinter = Fingerprinter::Digest.new
|
36
|
+
pr.pattern = PathRewriter::Format::QueryString
|
37
|
+
end
|
38
|
+
|
39
|
+
pr.call(@path).should eql(expected_path)
|
40
|
+
end
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: asset_fingerprinter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Steel Fu, Brad Gessler
|
@@ -31,10 +31,15 @@ extra_rdoc_files: []
|
|
31
31
|
files:
|
32
32
|
- .gitignore
|
33
33
|
- Gemfile
|
34
|
+
- Guardfile
|
34
35
|
- Rakefile
|
35
36
|
- asset_fingerprinter.gemspec
|
36
37
|
- lib/asset_fingerprinter.rb
|
38
|
+
- lib/asset_fingerprinter/fingerprinter.rb
|
39
|
+
- lib/asset_fingerprinter/path_rewriter.rb
|
37
40
|
- lib/asset_fingerprinter/version.rb
|
41
|
+
- spec/asset_fingerprinter_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
38
43
|
has_rdoc: true
|
39
44
|
homepage: http://www.polleverywhere.com/
|
40
45
|
licenses: []
|
@@ -69,5 +74,6 @@ rubygems_version: 1.6.2
|
|
69
74
|
signing_key:
|
70
75
|
specification_version: 3
|
71
76
|
summary: A Gem inspired by the Rails AssetFingerPrinter plugin
|
72
|
-
test_files:
|
73
|
-
|
77
|
+
test_files:
|
78
|
+
- spec/asset_fingerprinter_spec.rb
|
79
|
+
- spec/spec_helper.rb
|