expire_me 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/License +20 -0
- data/README.md +27 -0
- data/Rakefile +37 -0
- data/expire_me.gemspec +42 -0
- data/lib/expire_me/adapter.rb +30 -0
- data/lib/expire_me/adapters/varnish.rb +42 -0
- data/lib/expire_me/expire.rb +47 -0
- data/lib/expire_me/railtie.rb +32 -0
- data/lib/expire_me/response.rb +17 -0
- data/lib/expire_me/version.rb +42 -0
- data/lib/expire_me.rb +17 -0
- data/spec/expire_me_spec.rb +5 -0
- data/test/test.rb +5 -0
- data/test/test_helper.rb +7 -0
- metadata +86 -0
data/License
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Colin Young
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
'Software'), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
I have no clue what this library does.
|
2
|
+
|
3
|
+
# Pro Tip
|
4
|
+
|
5
|
+
To not have too many boilerplate commits (so you avoid ppl from thinking "WTF?
|
6
|
+
Why did he/she start with rspec but instantly switched to minitest, and what's
|
7
|
+
all that nonsense in the readme?") use `git commit --amend` to make changes in
|
8
|
+
the initial commit rather than creating new commits. Just saying.
|
9
|
+
|
10
|
+
# Usage
|
11
|
+
|
12
|
+
``` ruby
|
13
|
+
require 'expire_me'
|
14
|
+
fail("Dunno how to use %p" % ExpireMe)
|
15
|
+
```
|
16
|
+
|
17
|
+
# Installation
|
18
|
+
|
19
|
+
gem install expire_me
|
20
|
+
|
21
|
+
# TODO
|
22
|
+
|
23
|
+
* Write code and documentation
|
24
|
+
* Fix project description in gemspec
|
25
|
+
* Change testing framework if necessary (see Rakefile, currently RSpec)
|
26
|
+
* Adjust expire_me.gemspec if your github name is not colinyoung
|
27
|
+
* Adjust License if your real name is not Colin Young
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler'
|
5
|
+
Bundler::GemHelper.install_tasks
|
6
|
+
rescue LoadError => e
|
7
|
+
$stderr.puts e
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "run specs"
|
11
|
+
task(:spec) { ruby '-S rspec spec' }
|
12
|
+
|
13
|
+
desc "generate gemspec"
|
14
|
+
task 'expire_me.gemspec' do
|
15
|
+
require 'expire_me/version'
|
16
|
+
content = File.read 'expire_me.gemspec'
|
17
|
+
|
18
|
+
fields = {
|
19
|
+
:authors => `git shortlog -sn`.scan(/[^\d\s].*/),
|
20
|
+
:email => `git shortlog -sne`.scan(/[^<]+@[^>]+/),
|
21
|
+
:files => `git ls-files`.split("\n").reject { |f| f =~ /^(\.|Gemfile)/ }
|
22
|
+
}
|
23
|
+
|
24
|
+
fields.each do |field, values|
|
25
|
+
updated = " s.#{field} = ["
|
26
|
+
updated << values.map { |v| "\n %p" % v }.join(',')
|
27
|
+
updated << "\n ]"
|
28
|
+
content.sub!(/ s\.#{field} = \[\n( .*\n)* \]/, updated)
|
29
|
+
end
|
30
|
+
|
31
|
+
content.sub! /(s\.version.*=\s+).*/, "\\1\"#{ExpireMe::VERSION}\""
|
32
|
+
File.open('expire_me.gemspec', 'w') { |f| f << content }
|
33
|
+
end
|
34
|
+
|
35
|
+
task :gemspec => 'expire_me.gemspec'
|
36
|
+
task :default => :spec
|
37
|
+
task :test => :spec
|
data/expire_me.gemspec
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Run `rake expire_me.gemspec` to update the gemspec.
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
# general infos
|
4
|
+
s.name = "expire_me"
|
5
|
+
s.version = "0.0.1"
|
6
|
+
s.description = "control varnish expiration"
|
7
|
+
s.homepage = "http://github.com/colinyoung/expire_me"
|
8
|
+
s.summary = s.description
|
9
|
+
|
10
|
+
# generated from git shortlog -sn
|
11
|
+
s.authors = [
|
12
|
+
"Colin Young"
|
13
|
+
]
|
14
|
+
|
15
|
+
# generated from git shortlog -sne
|
16
|
+
s.email = [
|
17
|
+
"me@colinyoung.com"
|
18
|
+
]
|
19
|
+
|
20
|
+
s.add_dependency 'activesupport', ['>= 2.3.9', '< 4']
|
21
|
+
|
22
|
+
# generated from git ls-files
|
23
|
+
s.files = [
|
24
|
+
"License",
|
25
|
+
"README.md",
|
26
|
+
"Rakefile",
|
27
|
+
"expire_me.gemspec",
|
28
|
+
"lib/expire_me.rb",
|
29
|
+
"lib/expire_me/adapter.rb",
|
30
|
+
"lib/expire_me/adapters/varnish.rb",
|
31
|
+
"lib/expire_me/expire.rb",
|
32
|
+
"lib/expire_me/railtie.rb",
|
33
|
+
"lib/expire_me/response.rb",
|
34
|
+
"lib/expire_me/version.rb",
|
35
|
+
"spec/expire_me_spec.rb",
|
36
|
+
"test/test.rb",
|
37
|
+
"test/test_helper.rb"
|
38
|
+
]
|
39
|
+
|
40
|
+
# dependencies
|
41
|
+
s.add_development_dependency "rspec", "~> 2.0"
|
42
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ExpireMe
|
2
|
+
class Adapter
|
3
|
+
|
4
|
+
attr_accessor :response
|
5
|
+
|
6
|
+
def set_expiration(r, seconds=0)
|
7
|
+
self.response = r
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.select(adapter_name)
|
11
|
+
return self.new if self.name != Adapter.to_s
|
12
|
+
|
13
|
+
(ExpireMe.const_get adapter_name.to_s.classify).new
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Regexp
|
20
|
+
def unregex
|
21
|
+
puts "unr: #{self.to_s}"
|
22
|
+
s = self.to_s.gsub('?i-mx:', ''). # Remove initial ruby stuff
|
23
|
+
gsub(/(\=\)|)\(\[0-9\]\+\)/, '='). # Empties the value subgroup, heavily escaped because we're operating on a regex
|
24
|
+
gsub(/[^\d\w\-\=]/, '') # Replaces all nondigits and words
|
25
|
+
puts s
|
26
|
+
s
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
Dir[File.expand_path("../adapters/*.rb", __FILE__)].each {|f| require f }
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module ExpireMe
|
2
|
+
class Varnish < Adapter
|
3
|
+
|
4
|
+
PARTS = {
|
5
|
+
:age => /(max\-age=)([0-9]+)/i
|
6
|
+
}
|
7
|
+
|
8
|
+
def set_expiration(response, seconds)
|
9
|
+
super
|
10
|
+
|
11
|
+
puts "Caching page for seconds: #{seconds}"
|
12
|
+
replace_caching_header :age => seconds
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def replace_caching_header(h)
|
18
|
+
needle = PARTS[h.keys.first]
|
19
|
+
value = h.values.first
|
20
|
+
return if value.nil? or needle.nil?
|
21
|
+
|
22
|
+
if header =~ needle
|
23
|
+
self.header.interpolate! needle, value
|
24
|
+
else
|
25
|
+
if header.empty?
|
26
|
+
self.header = needle.unregex << value.to_s
|
27
|
+
else
|
28
|
+
self.header = self.header << header[needle, 1] << value.to_s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def header
|
34
|
+
response.headers['Cache-Control'] || ""
|
35
|
+
end
|
36
|
+
|
37
|
+
def header=(s)
|
38
|
+
response.headers['Cache-Control'] = s
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module ExpireMe
|
2
|
+
|
3
|
+
class << self
|
4
|
+
|
5
|
+
@@seconds = 0
|
6
|
+
|
7
|
+
def expire=(descriptor, options={})
|
8
|
+
|
9
|
+
seconds = 0 # Reset for each request.
|
10
|
+
|
11
|
+
if options.empty?
|
12
|
+
self.send :in, descriptor
|
13
|
+
else
|
14
|
+
self.send descriptor, options
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def expire_http_response(http_response)
|
19
|
+
Response.new({
|
20
|
+
:http_response => http_response,
|
21
|
+
:seconds => @@seconds,
|
22
|
+
:adapter => adapter
|
23
|
+
})
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def adapter
|
29
|
+
# Maybe we'll support others later.
|
30
|
+
ExpireMe::Adapter.select(:varnish)
|
31
|
+
end
|
32
|
+
|
33
|
+
def at time
|
34
|
+
@@seconds = (time - Time.now).to_i
|
35
|
+
end
|
36
|
+
|
37
|
+
def in seconds
|
38
|
+
@@seconds = seconds
|
39
|
+
end
|
40
|
+
|
41
|
+
def now
|
42
|
+
@@seconds = 0
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module ExpireMe
|
4
|
+
|
5
|
+
module Controller
|
6
|
+
def expire_me(descriptor, options={})
|
7
|
+
ExpireMe.expire = descriptor, options
|
8
|
+
|
9
|
+
include ExpireMe::Controller::InstanceMethods
|
10
|
+
after_filter :update_cache_expiration
|
11
|
+
end
|
12
|
+
|
13
|
+
def no_cache
|
14
|
+
expire_me :now
|
15
|
+
end
|
16
|
+
|
17
|
+
module InstanceMethods
|
18
|
+
|
19
|
+
def update_cache_expiration
|
20
|
+
ExpireMe.expire_http_response(response)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Railtie < Rails::Railtie
|
27
|
+
config.to_prepare do
|
28
|
+
ApplicationController.send(:extend, ExpireMe::Controller)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ExpireMe
|
2
|
+
class Response
|
3
|
+
|
4
|
+
attr_accessor :hash
|
5
|
+
|
6
|
+
def initialize(h={ :adapter => nil, :seconds => 0, :http_response => nil })
|
7
|
+
self.hash = h
|
8
|
+
adapter.set_expiration(http_response, seconds)
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_missing(t)
|
12
|
+
hash[t]
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module ExpireMe
|
2
|
+
def self.version
|
3
|
+
VERSION
|
4
|
+
end
|
5
|
+
|
6
|
+
module VERSION
|
7
|
+
extend Comparable
|
8
|
+
|
9
|
+
MAJOR = 0
|
10
|
+
MINOR = 0
|
11
|
+
TINY = 1
|
12
|
+
SIGNATURE = [MAJOR, MINOR, TINY]
|
13
|
+
STRING = SIGNATURE.join '.'
|
14
|
+
|
15
|
+
def self.major; MAJOR end
|
16
|
+
def self.minor; MINOR end
|
17
|
+
def self.tiny; TINY end
|
18
|
+
def self.to_s; STRING end
|
19
|
+
|
20
|
+
def self.hash
|
21
|
+
STRING.hash
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.<=>(other)
|
25
|
+
other = other.split('.').map { |i| i.to_i } if other.respond_to? :split
|
26
|
+
SIGNATURE <=> Array(other)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.inspect
|
30
|
+
STRING.inspect
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.respond_to?(meth, *)
|
34
|
+
meth.to_s !~ /^__|^to_str$/ and STRING.respond_to? meth unless super
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.method_missing(meth, *args, &block)
|
38
|
+
return super unless STRING.respond_to?(meth)
|
39
|
+
STRING.send(meth, *args, &block)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/expire_me.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
classes = \
|
5
|
+
[
|
6
|
+
:version,
|
7
|
+
:railtie,
|
8
|
+
:expire,
|
9
|
+
:adapter,
|
10
|
+
:response
|
11
|
+
]
|
12
|
+
|
13
|
+
classes.map {|c| require File.expand_path("../expire_me/#{c}", __FILE__) }
|
14
|
+
|
15
|
+
module ExpireMe
|
16
|
+
|
17
|
+
end
|
data/test/test.rb
ADDED
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: expire_me
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Colin Young
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-03 00:00:00.000000000 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
requirement: &70338508618260 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.3.9
|
23
|
+
- - <
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '4'
|
26
|
+
type: :runtime
|
27
|
+
prerelease: false
|
28
|
+
version_requirements: *70338508618260
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: rspec
|
31
|
+
requirement: &70338508617140 !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ~>
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '2.0'
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: *70338508617140
|
40
|
+
description: control varnish expiration
|
41
|
+
email:
|
42
|
+
- me@colinyoung.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- License
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- expire_me.gemspec
|
51
|
+
- lib/expire_me.rb
|
52
|
+
- lib/expire_me/adapter.rb
|
53
|
+
- lib/expire_me/adapters/varnish.rb
|
54
|
+
- lib/expire_me/expire.rb
|
55
|
+
- lib/expire_me/railtie.rb
|
56
|
+
- lib/expire_me/response.rb
|
57
|
+
- lib/expire_me/version.rb
|
58
|
+
- spec/expire_me_spec.rb
|
59
|
+
- test/test.rb
|
60
|
+
- test/test_helper.rb
|
61
|
+
has_rdoc: true
|
62
|
+
homepage: http://github.com/colinyoung/expire_me
|
63
|
+
licenses: []
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.6.2
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: control varnish expiration
|
86
|
+
test_files: []
|