stefl-rack-cache-purge 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ module Rack
2
+ module Cache
3
+ class Purge
4
+ class << self
5
+ def allow_http_purge!
6
+ include Rack::Cache::Purge::Http
7
+ end
8
+ end
9
+
10
+ PURGE_HEADER = 'rack-cache.purge'
11
+ PURGER_HEADER = 'rack-cache.purger'
12
+
13
+ autoload :Base, 'rack/cache/purge/base'
14
+ autoload :Http, 'rack/cache/purge/http'
15
+ autoload :Purger, 'rack/cache/purge/purger'
16
+
17
+ include Base
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ module Rack
2
+ module Cache
3
+ class Purge
4
+ module Base
5
+ attr_reader :app, :purger
6
+
7
+ def initialize(app, options = {})
8
+ self.class.allow_http_purge! if options[:allow_http_purge]
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ purger = Purger.new(env)
14
+ status, headers, body = app.call(env.merge(PURGER_HEADER => purger))
15
+
16
+ # TODO we probably should remove the headers here but i can't figure out
17
+ # how to get the middlewares correctly pushed to the rails middleware stack
18
+ # purger.purge(headers.delete(PURGE_HEADER)) if headers.key?(PURGE_HEADER)
19
+
20
+ purger.purge(headers[PURGE_HEADER]) if headers.key?(PURGE_HEADER)
21
+ [status, headers, body]
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ module Rack
2
+ module Cache
3
+ class Purge
4
+ module Http
5
+ def call(env)
6
+ http_purge?(env) ? http_purge(env) : super
7
+ end
8
+
9
+ protected
10
+
11
+ def http_purge?(env)
12
+ env['REQUEST_METHOD'] == 'PURGE'
13
+ end
14
+
15
+ def http_purge(env)
16
+ Purger.new(env).purge(Rack::Request.new(env).url)
17
+ [200, {}, 'Purged']
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,48 @@
1
+ require 'rack/cache/key'
2
+ require 'rack/mock'
3
+
4
+ module Rack
5
+ module Cache
6
+ class Purge
7
+ class Purger
8
+ attr_reader :env
9
+
10
+ def initialize(env)
11
+ @env = env
12
+ end
13
+
14
+ def purge(uris)
15
+ normalize_uris(uris).map do |uri|
16
+ key = key_for(uri)
17
+ metastore.purge(key)
18
+ entitystore.purge(key)
19
+ end
20
+ end
21
+
22
+ protected
23
+
24
+ def normalize_uris(uris)
25
+ Array(uris).flatten.join("\n").split("\n")
26
+ end
27
+
28
+ def key_for(uri)
29
+ Rack::Cache::Key.call(Rack::Cache::Request.new(env_for(uri)))
30
+ end
31
+
32
+ def env_for(*args)
33
+ Rack::MockRequest.env_for(*args)
34
+ end
35
+
36
+ def metastore
37
+ @metastore ||= Rack::Cache::Storage.instance.resolve_metastore_uri(env['rack-cache.metastore'])
38
+ end
39
+
40
+ def entitystore
41
+ @entitystore ||= Rack::Cache::Storage.instance.resolve_entitystore_uri(env['rack-cache.entitystore'])
42
+ end
43
+ end
44
+
45
+ include Base
46
+ end
47
+ end
48
+ end
@@ -0,0 +1 @@
1
+ require 'rack/cache/purge'
@@ -0,0 +1,3 @@
1
+ module RackCachePurge
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stefl-rack-cache-purge
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Sven Fuchs
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-18 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rack-cache
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 1
32
+ - 0
33
+ version: "1.0"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: ruby-debug
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: test_declarative
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ type: :development
63
+ version_requirements: *id003
64
+ description: "[description]"
65
+ email: svenfuchs@artweb-design.de
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files: []
71
+
72
+ files:
73
+ - lib/rack/cache/purge/base.rb
74
+ - lib/rack/cache/purge/http.rb
75
+ - lib/rack/cache/purge/purger.rb
76
+ - lib/rack/cache/purge.rb
77
+ - lib/rack_cache_purge/version.rb
78
+ - lib/rack_cache_purge.rb
79
+ has_rdoc: true
80
+ homepage: http://github.com/svenfuchs/rack-cache-purge
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options: []
85
+
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ requirements: []
107
+
108
+ rubyforge_project: "[none]"
109
+ rubygems_version: 1.3.7
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: "[summary]"
113
+ test_files: []
114
+