asset_fingerprinter 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Asset Fingerprinter
2
+
3
+ Give credit where credit is due: this gem was Inspired by Eliot Sykes' asset_fingerprinter Rails plugin. We needed something a little more robust at Poll Everywhere that would work outside of Rails in our compass/sass deploymennt pref-flight compilations, so we wrote this little honey.
4
+
5
+ Humans are stupid and sloppy, so this library is well tested via spec and put through the production ringer at Poll Everywhere.
6
+
7
+ # Installation
8
+
9
+ Standalone:
10
+
11
+ gem install asset_fingerprinter
12
+
13
+ Gemfile:
14
+
15
+ gem 'asset_fingerprinter'
16
+
17
+ # Getting Started
18
+
19
+ The Asset Fingerprint gem was designed to work in any environment including Rails and the compass compiler.
20
+
21
+ ## Rails 2.x
22
+
23
+ Holy Caw! We made this work with Rails 2.x because we're old school. Well, almost old school; we assume that you hacked your Rails 2.x install to use Gemfiles. Just add:
24
+
25
+ gem 'asset_fingerprinter'
26
+
27
+ to your Rails 2.x Gemfile and we take care of the rest through some hickity-hack magic.
28
+
29
+ ## Rails 3.x
30
+
31
+ Change the asset_path_template in config/environments/production.rb
32
+
33
+ config.action_controller.asset_path_template = AssetFingerprinter.path_rewriter
34
+
35
+ Meh, no hack wizardry here. Curse ya Rails 3!
36
+
37
+ # Ha! I'm way more advanced then that...
38
+
39
+ Cool, we are too. If you want to get down and dirty with AssetFingerprint, RTFSC and discover configuration that looks like:
40
+
41
+ pr = PathRewriter.new do |pr|
42
+ pr.fingerprinter = Fingerprinter::Digest.new # Whatever, give us a proc or something that response to .call
43
+ pr.fingerprinter.root = File.dirname(__FILE__) # Turns out you need a file root for file digesting.
44
+ pr.pattern = PathRewriter::Format::Filename # Newbs use the query string cachebusters, we use use Filename rewriter, but you can go nuts here with anything that responds to .call
45
+ end
46
+
47
+ # OMG! It threw a file digest into my file name
48
+ pr.call('/images/i_am.will') # => '/images/i-am-will-fp-abc3984928abfedf3e2309sb.will'
49
+
50
+ # Heh, its not a magic bullet, You still need to configure nginx or Apache
51
+ # to rewrite this path. Its totally worth it though.
52
+
53
+ # Be a super hero. Help the world
54
+
55
+ We'll entertain your pull requests if you have specs around whatever you throw our way. Otherwise we spit at you!
56
+
57
+ No, We're really interested in more fingerprinting strategies, integrations with other frameworks, and other ways that make serving up HTTP assets ubber fast.
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Steel Fu, Brad Gessler"]
10
10
  s.email = ["steel@polleverywhere.com, brad@polleverywhere.com"]
11
11
  s.homepage = "http://www.polleverywhere.com/"
12
- s.summary = %q{A Gem inspired by the Rails AssetFingerPrinter plugin}
13
- s.description = %q{An effecient HTTP cache buster}
12
+ s.summary = %q{A Gem inspired by the Rails AssetFingerPrinter plugin.}
13
+ s.description = %q{The most efficient HTTP cache buster plugin evar.}
14
14
 
15
15
  s.rubyforge_project = "asset_fingerprinter"
16
16
 
@@ -1,4 +1,27 @@
1
1
  module AssetFingerprinter
2
+ autoload :Integration, 'asset_fingerprinter/integration'
2
3
  autoload :Fingerprinter, 'asset_fingerprinter/fingerprinter'
3
4
  autoload :PathRewriter, 'asset_fingerprinter/path_rewriter'
5
+
6
+ # A default path rewriter to keep things sane
7
+ def self.path_rewriter
8
+ @path_rewriter ||= PathRewriter.new do |pr|
9
+ pr.fingerprinter = Fingerprinter::Digest.new
10
+ pr.pattern = PathRewriter::Format::QueryString
11
+ end
12
+ end
13
+
14
+ # A default way to call a rewrite
15
+ def self.rewrite(path)
16
+ path_rewriter.call(path)
17
+ end
18
+
19
+ # Fake out a configuration block
20
+ def self.config
21
+ yield path_rewriter if block_given?
22
+ path_rewriter
23
+ end
24
+
25
+ # Figure out if we're running in a Rails 2.x environment
26
+ Integration.detect
4
27
  end
@@ -3,13 +3,23 @@ require 'digest/md5'
3
3
  module AssetFingerprinter
4
4
  module Fingerprinter
5
5
  class Digest
6
+ attr_accessor :root
6
7
  # Digests a file and returns the cachebusted path
8
+
7
9
  def initialize
8
- @cache = Hash.new{ |hash,key| hash[key] = ::Digest::MD5.hexdigest(File.read(key)) }
10
+ @cache = Hash.new{ |hash,key|
11
+ hash[key] = ::Digest::MD5.hexdigest(File.read(root ? File.join(root, key) : key))
12
+ }
9
13
  end
10
-
14
+
11
15
  def call(path)
12
- @cache[path]
16
+ @cache[clean_path(path)]
17
+ end
18
+
19
+ private
20
+ # Remove query strings from the incoming path.
21
+ def clean_path(path)
22
+ path.split('?').first
13
23
  end
14
24
  end
15
25
  end
@@ -0,0 +1,16 @@
1
+ module AssetFingerprinter
2
+ module Integration
3
+ module Rails2
4
+ def self.integrate
5
+ require 'asset_fingerprinter/integration/rails2'
6
+ if AssetFingerprinter.path_rewriter.fingerprinter.respond_to? :root
7
+ AssetFingerprinter.path_rewriter.fingerprinter.root = File.join(Rails.root, 'public')
8
+ end
9
+ end
10
+ end
11
+
12
+ def self.detect
13
+ Rails2.integrate if defined? Rails and Rails::VERSION::MAJOR == 2
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ # A really stupid monkey path for Rails 2.x
2
+ module ActionView
3
+ module Helpers
4
+ module AssetTagHelper
5
+ def rewrite_asset_path(path)
6
+ AssetFingerprinter.rewrite path
7
+ end
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module AssetFingerprinter
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,10 +1,65 @@
1
1
  require 'spec_helper'
2
2
  require 'digest/md5'
3
3
 
4
+ describe AssetFingerprinter do
5
+ it "should rewrite" do
6
+ AssetFingerprinter.rewrite(__FILE__).should match(/\.rb\?.+/)
7
+ end
8
+
9
+ it "should config" do
10
+ AssetFingerprinter.config do |c|
11
+ c.fingerprinter.should be_instance_of(Fingerprinter::Digest)
12
+ c.pattern.should eql(PathRewriter::Format::QueryString)
13
+ end
14
+ end
15
+ end
16
+
17
+ describe Integration do
18
+ context "Rails 2" do
19
+ # TODO setup the rails public dir here and tear it down after the test is done.
20
+
21
+ before(:each) do
22
+ Rails = Struct.new(:root).new(File.join(File.dirname(__FILE__), 'rails'))
23
+ end
24
+
25
+ after(:each) do
26
+ Object.send(:remove_const, :Rails)
27
+ end
28
+
29
+ it "should replace rails rewrite_asset_path" do
30
+ scope = Class.new {
31
+ module ::ActionView
32
+ module Helpers
33
+ module AssetTagHelper
34
+ def rewrite_asset_path(path)
35
+ "poop"
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ Integration::Rails2.integrate
42
+
43
+ include ActionView::Helpers::AssetTagHelper
44
+ }
45
+
46
+ scope.new.rewrite_asset_path('something.file').should_not eql('poop')
47
+ end
48
+ end
49
+ end
50
+
4
51
  describe Fingerprinter::Digest do
52
+ before(:each) do
53
+ @file = __FILE__
54
+ @digest = Digest::MD5.hexdigest File.read(@file)
55
+ end
56
+
5
57
  it "should fingerprint a file" do
6
- digest = Digest::MD5.hexdigest File.read(__FILE__)
7
- Fingerprinter::Digest.new.call(__FILE__).should eql(digest)
58
+ Fingerprinter::Digest.new.call(@file).should eql(@digest)
59
+ end
60
+
61
+ it "should fingerprint paths with query string params" do
62
+ Fingerprinter::Digest.new.call("#{@file}?color=red&size=large").should eql(@digest)
8
63
  end
9
64
  end
10
65
 
@@ -18,14 +73,15 @@ describe PathRewriter do
18
73
  end
19
74
 
20
75
  it "should rewrite filename path" do
21
- expected_path = File.expand_path(File.join(@dir, "#{@base}-fp-#{@digest}#{@ext}"))
76
+ expected_path = "#{@base}-fp-#{@digest}#{@ext}"
22
77
 
23
78
  pr = PathRewriter.new do |pr|
24
79
  pr.fingerprinter = Fingerprinter::Digest.new
80
+ pr.fingerprinter.root = File.dirname(__FILE__)
25
81
  pr.pattern = PathRewriter::Format::Filename
26
82
  end
27
-
28
- pr.call(@path).should eql(expected_path)
83
+
84
+ pr.call(@file).should eql(expected_path)
29
85
  end
30
86
 
31
87
  it "should rewrite queryname path" do
File without changes
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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Steel Fu, Brad Gessler
@@ -15,11 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-10 00:00:00 -07:00
18
+ date: 2011-05-12 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
- description: An effecient HTTP cache buster
22
+ description: The most efficient HTTP cache buster plugin evar.
23
23
  email:
24
24
  - steel@polleverywhere.com, brad@polleverywhere.com
25
25
  executables: []
@@ -32,13 +32,17 @@ files:
32
32
  - .gitignore
33
33
  - Gemfile
34
34
  - Guardfile
35
+ - README.md
35
36
  - Rakefile
36
37
  - asset_fingerprinter.gemspec
37
38
  - lib/asset_fingerprinter.rb
38
39
  - lib/asset_fingerprinter/fingerprinter.rb
40
+ - lib/asset_fingerprinter/integration.rb
41
+ - lib/asset_fingerprinter/integration/rails2.rb
39
42
  - lib/asset_fingerprinter/path_rewriter.rb
40
43
  - lib/asset_fingerprinter/version.rb
41
44
  - spec/asset_fingerprinter_spec.rb
45
+ - spec/rails/public/something.file
42
46
  - spec/spec_helper.rb
43
47
  has_rdoc: true
44
48
  homepage: http://www.polleverywhere.com/
@@ -70,10 +74,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
74
  requirements: []
71
75
 
72
76
  rubyforge_project: asset_fingerprinter
73
- rubygems_version: 1.6.2
77
+ rubygems_version: 1.4.2
74
78
  signing_key:
75
79
  specification_version: 3
76
- summary: A Gem inspired by the Rails AssetFingerPrinter plugin
80
+ summary: A Gem inspired by the Rails AssetFingerPrinter plugin.
77
81
  test_files:
78
82
  - spec/asset_fingerprinter_spec.rb
83
+ - spec/rails/public/something.file
79
84
  - spec/spec_helper.rb