zippy_static_cache 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/zippy_static_cache.rb +41 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b23eb73b672df5153d8592eee7ce11fbb8d2a3a
4
+ data.tar.gz: 194e5dae0fcd18d2e2f9794262de9514a3695747
5
+ SHA512:
6
+ metadata.gz: da29c3235a630f28772de4b3cc139e8f10f8018c3afb34ddf63bdb3d8e76d1cba684b63174dbe8b249af29e1273e55ec830f34008852628b8b04413fa13a68c9
7
+ data.tar.gz: af8efceb0c35004b05b3410bfd6f58f03c896eb5306b6c72f7c0f909a0edaf32043cf1687efe43a97f9727cdca70f3be81e5971c0d79b323bd9772d97161c879
@@ -0,0 +1,41 @@
1
+ class ZippyStaticCache
2
+ def initialize(app, options={})
3
+ @app = app
4
+ @urls = options[:urls]
5
+ @no_cache = {}
6
+ @urls.collect! do |url|
7
+ if url =~ /\*$/
8
+ url.sub!(/\*$/, '')
9
+ @no_cache[url] = 1
10
+ end
11
+ url
12
+ end
13
+ @cache_duration = options[:duration] || 1
14
+ @duration_in_seconds = self.duration_in_seconds
15
+ @duration_in_words = self.duration_in_words
16
+ end
17
+
18
+ def call(env)
19
+ path = env["PATH_INFO"]
20
+ url = @urls.detect{ |u| path.index(u) == 0 }
21
+ status, headers, body = @app.call(env)
22
+
23
+ if !url.nil? && @no_cache[url].nil?
24
+ headers['Cache-Control'] ="max-age=#{@duration_in_seconds}, public"
25
+ headers['Expires'] = @duration_in_words
26
+ headers.delete 'Etag'
27
+ headers.delete 'Pragma'
28
+ headers.delete 'Last-Modified'
29
+ end
30
+
31
+ [status, headers, body]
32
+ end
33
+
34
+ def duration_in_words
35
+ (Time.now + self.duration_in_seconds).strftime '%a, %d %b %Y %H:%M:%S GMT'
36
+ end
37
+
38
+ def duration_in_seconds
39
+ (60 * 60 * 24 * 365 * @cache_duration).to_i
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zippy_static_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Teng-hao Chang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Modified Rack::StaticCache to work better with rack-zippy
14
+ email: zhusee2@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/zippy_static_cache.rb
20
+ homepage: https://github.com/zhusee2/zippy_static_cache
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: ZippyStaticCache
44
+ test_files: []