etag_for 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +26 -0
- data/LICENSE.txt +22 -0
- data/README.md +118 -0
- data/Rakefile +10 -0
- data/etag_for.gemspec +24 -0
- data/lib/etag_for/etag_for.rb +41 -0
- data/lib/etag_for/railtie.rb +11 -0
- data/lib/etag_for/version.rb +3 -0
- data/lib/etag_for.rb +4 -0
- data/test/etag_for_test.rb +99 -0
- data/test/helper.rb +9 -0
- metadata +110 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm 1.9.3-p286@etag_for --create
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
etag_for (0.0.1)
|
5
|
+
activesupport (>= 3.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (3.2.11)
|
11
|
+
i18n (~> 0.6)
|
12
|
+
multi_json (~> 1.0)
|
13
|
+
i18n (0.6.1)
|
14
|
+
metaclass (0.0.1)
|
15
|
+
minitest (4.4.0)
|
16
|
+
mocha (0.13.1)
|
17
|
+
metaclass (~> 0.0.1)
|
18
|
+
multi_json (1.5.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
etag_for!
|
25
|
+
minitest
|
26
|
+
mocha
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Tom Fakes
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
# EtagFor
|
2
|
+
|
3
|
+
Allow an ActionController action to include CSS, Javascript, View and Layout code as pieces for the calculation of an action's ETAG header. This allows
|
4
|
+
browsers to get new responses from the server if the view code that renders the response changes.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'etag_for'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install etag_for
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Typical etag usage for an Rails controller action looks like this:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class FooController < ApplicationController
|
26
|
+
|
27
|
+
def show
|
28
|
+
@post = Post.find(params[:id])
|
29
|
+
fresh_when :etag => @post, :last_modified => @post.updated_at, :public => true
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
This works great, any time the @post object gets updated, the ETag header won't match, and a new response will be generated.
|
36
|
+
|
37
|
+
This doesn't catch the case where the view code, **post.html.erb** is updated. Clients with an existing response, including proxy caches, will never get an
|
38
|
+
updated response, as the ETag calculation doesn't include this part of the response.
|
39
|
+
|
40
|
+
With the provided **etag_for** method, the rendering templates used by your application can be used as part of the ETag calculation.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
class FooController < ApplicationController
|
44
|
+
|
45
|
+
def show
|
46
|
+
@post = Post.find(params[:id])
|
47
|
+
fresh_when :etag => etag_for(@post, :layout => 'application.html.erb', :view => 'post/show.html.erb'), :last_modified => @post.updated_at, :public => true
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
## etag_for(item_or_items, options = {})
|
54
|
+
|
55
|
+
Returns an array of strings that can be used as an input to **fresh_when** and **stale?** to calculate an ETag value for the current request.
|
56
|
+
|
57
|
+
### item_or_items parameter
|
58
|
+
|
59
|
+
The single item or items array that is the basic data retrieved to be rendered. In the example below, the index method has an array of posts, and the show method
|
60
|
+
provides a single post object
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class FooController < ApplicationController
|
64
|
+
|
65
|
+
def index
|
66
|
+
@posts = Post.all
|
67
|
+
fresh_when :etag => etag_for(@posts, :layout => 'application.html.erb', :view => 'post/index.html.erb'), :last_modified => @posts.first.updated_at, :public => true
|
68
|
+
end
|
69
|
+
|
70
|
+
def show
|
71
|
+
@post = Post.find(params[:id])
|
72
|
+
fresh_when :etag => etag_for(@post, :layout => 'application.html.erb', :view => 'post/show.html.erb'), :last_modified => @post.updated_at, :public => true
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
### options
|
79
|
+
|
80
|
+
#### :css
|
81
|
+
|
82
|
+
Specifiy the CSS file used by the layout for this action. The asset pipeline name will be substituted, to get the hash digest of the CSS file as part of the
|
83
|
+
ETag
|
84
|
+
|
85
|
+
default value if none is specified is **application**. Supply an empty string for no CSS file.
|
86
|
+
|
87
|
+
#### :js
|
88
|
+
|
89
|
+
Specifiy the JS file used by the layout for this action. The asset pipeline name will be substituted, to get the hash digest of the JS file as part of the
|
90
|
+
ETag
|
91
|
+
|
92
|
+
default value if none is specified is **application**. Supply an empty string for no JS file.
|
93
|
+
|
94
|
+
#### :layout
|
95
|
+
|
96
|
+
The name of the layout file used by this action. An MD5 digest of this file will be part of the ETag calculation
|
97
|
+
|
98
|
+
e.g. 'application.html.erb'
|
99
|
+
|
100
|
+
#### :view
|
101
|
+
|
102
|
+
The path and name of the view file used by this action. An MD5 digest of this file will be part of the ETag calculation
|
103
|
+
|
104
|
+
e.g. 'posts/show.html.erb'
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
1. Fork it
|
109
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
110
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
111
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
112
|
+
5. Create new Pull Request
|
113
|
+
|
114
|
+
## Credits
|
115
|
+
|
116
|
+
[<img src="http://d1za39ny3bo0r4.cloudfront.net/assets/craz8-logo-8e807e1c44376d564da419a6d82ec5be.png">](http://craz8.com)
|
117
|
+
|
118
|
+
EtagFor is maintained by CRAZ8
|
data/Rakefile
ADDED
data/etag_for.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'etag_for/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "etag_for"
|
8
|
+
gem.version = EtagFor::VERSION
|
9
|
+
gem.authors = ["Tom Fakes"]
|
10
|
+
gem.email = ["tom@craz8.com"]
|
11
|
+
gem.description = "Provide etag support for actions with views and layouts"
|
12
|
+
gem.summary = "Allow an action to include CSS, Javascript, View and Layout code as pieces for the calculation of an action's ETAG header"
|
13
|
+
gem.homepage = "https://github.com/craz8/etag_for"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "activesupport", ">= 3.0"
|
21
|
+
|
22
|
+
gem.add_development_dependency "minitest"
|
23
|
+
gem.add_development_dependency "mocha"
|
24
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module EtagFor
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
def etag_for(item_or_items, options = {})
|
5
|
+
css_file = options[:css] || 'application'
|
6
|
+
js_file = options[:js] || 'application'
|
7
|
+
|
8
|
+
files = []
|
9
|
+
files << "layouts/#{options[:layout]}" if options[:layout]
|
10
|
+
files << options[:view] if options[:view]
|
11
|
+
files += options[:files] if options[:files]
|
12
|
+
|
13
|
+
[ extract_keys(item_or_items) ].flatten + [ css_path(css_file), js_path(js_file) ] + digests_of(files)
|
14
|
+
end
|
15
|
+
|
16
|
+
protected
|
17
|
+
def digests_of(file_list)
|
18
|
+
file_list.map do |file|
|
19
|
+
Digest::MD5.hexdigest(File.read("#{Rails.root}/app/views/#{file}"))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def css_path(css_file)
|
24
|
+
view_context.stylesheet_path(css_file) if css_file && css_file.length > 0
|
25
|
+
end
|
26
|
+
|
27
|
+
def js_path(js_file)
|
28
|
+
view_context.javascript_path(js_file) if js_file && js_file.length > 0
|
29
|
+
end
|
30
|
+
|
31
|
+
def extract_keys(item_or_items)
|
32
|
+
if item_or_items.respond_to?(:map)
|
33
|
+
item_or_items.map do |item|
|
34
|
+
item.respond_to?(:cache_key) ? item.cache_key : item.to_param
|
35
|
+
end
|
36
|
+
else
|
37
|
+
item_or_items.respond_to?(:cache_key) ? item_or_items.cache_key : item_or_items.to_param
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/lib/etag_for.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Rails
|
4
|
+
def self.root
|
5
|
+
''
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestController
|
10
|
+
include EtagFor
|
11
|
+
|
12
|
+
attr_accessor :view_context_mock
|
13
|
+
def view_context
|
14
|
+
@view_context_mock
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe EtagFor do
|
19
|
+
|
20
|
+
it "must convert file list to list of digests" do
|
21
|
+
file_list = [ "file_1", "file_2" ]
|
22
|
+
|
23
|
+
File.stub :read, "12345678" do
|
24
|
+
digests = TestController.new.send(:digests_of, file_list)
|
25
|
+
digests.must_be_instance_of Array
|
26
|
+
digests.size.must_equal 2
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "must lookup stylesheet path if stylesheet given" do
|
31
|
+
view_context = MiniTest::Mock.new
|
32
|
+
view_context.expect :stylesheet_path, 'css-name', [ 'application' ]
|
33
|
+
|
34
|
+
controller = TestController.new
|
35
|
+
controller.view_context_mock = view_context
|
36
|
+
|
37
|
+
controller.send(:css_path, "application").must_equal "css-name"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "must not lookup stylesheet path if no stylesheet given" do
|
41
|
+
TestController.new.send(:css_path, "").must_be_nil
|
42
|
+
end
|
43
|
+
|
44
|
+
it "must lookup javascript path if javascript given" do
|
45
|
+
view_context = MiniTest::Mock.new
|
46
|
+
view_context.expect :javascript_path, 'js-name', [ 'application' ]
|
47
|
+
|
48
|
+
controller = TestController.new
|
49
|
+
controller.view_context_mock = view_context
|
50
|
+
|
51
|
+
controller.send(:js_path, "application").must_equal "js-name"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "must not lookup javascript path if no javascript given" do
|
55
|
+
TestController.new.send(:js_path, "").must_be_nil
|
56
|
+
end
|
57
|
+
|
58
|
+
it "must extract keys from item array with cache_key" do
|
59
|
+
item_1 = MiniTest::Mock.new
|
60
|
+
item_1.expect :cache_key, "foo"
|
61
|
+
|
62
|
+
item_2 = MiniTest::Mock.new
|
63
|
+
item_2.expect :cache_key, "bar"
|
64
|
+
|
65
|
+
TestController.new.send(:extract_keys, [ item_1, item_2 ]).must_equal [ "foo", "bar" ]
|
66
|
+
end
|
67
|
+
|
68
|
+
it "must extract key from a single item with cache_key" do
|
69
|
+
item_1 = MiniTest::Mock.new
|
70
|
+
item_1.expect :cache_key, "foo"
|
71
|
+
|
72
|
+
TestController.new.send(:extract_keys, item_1).must_equal "foo"
|
73
|
+
end
|
74
|
+
|
75
|
+
it "must extract keys from item array with out cache_key" do
|
76
|
+
item_1 = MiniTest::Mock.new
|
77
|
+
item_1.expect :to_param, "foo"
|
78
|
+
|
79
|
+
item_2 = MiniTest::Mock.new
|
80
|
+
item_2.expect :to_param, "bar"
|
81
|
+
|
82
|
+
TestController.new.send(:extract_keys, [ item_1, item_2 ]).must_equal [ "foo", "bar" ]
|
83
|
+
end
|
84
|
+
|
85
|
+
it "must extract key from a single item with out cache_key" do
|
86
|
+
item_1 = MiniTest::Mock.new
|
87
|
+
item_1.expect :to_param, "foo"
|
88
|
+
|
89
|
+
TestController.new.send(:extract_keys, item_1).must_equal "foo"
|
90
|
+
end
|
91
|
+
|
92
|
+
it "must return array from etag_for" do
|
93
|
+
item = MiniTest::Mock.new
|
94
|
+
item.expect :to_param, "foo"
|
95
|
+
|
96
|
+
TestController.new.etag_for(item, :css => "", :js => "").must_equal [ "foo", nil, nil ]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: etag_for
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tom Fakes
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: minitest
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: mocha
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Provide etag support for actions with views and layouts
|
63
|
+
email:
|
64
|
+
- tom@craz8.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rvmrc
|
71
|
+
- Gemfile
|
72
|
+
- Gemfile.lock
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- etag_for.gemspec
|
77
|
+
- lib/etag_for.rb
|
78
|
+
- lib/etag_for/etag_for.rb
|
79
|
+
- lib/etag_for/railtie.rb
|
80
|
+
- lib/etag_for/version.rb
|
81
|
+
- test/etag_for_test.rb
|
82
|
+
- test/helper.rb
|
83
|
+
homepage: https://github.com/craz8/etag_for
|
84
|
+
licenses: []
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.8.24
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: Allow an action to include CSS, Javascript, View and Layout code as pieces
|
107
|
+
for the calculation of an action's ETAG header
|
108
|
+
test_files:
|
109
|
+
- test/etag_for_test.rb
|
110
|
+
- test/helper.rb
|