rack-page_caching 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb99cfbdf37f440d76310e72aa7678ef50755dad
4
+ data.tar.gz: f9c25904f971fd111f2de264031bf260ab04593c
5
+ SHA512:
6
+ metadata.gz: 61f3b351f508dcf1940953c5285857dfa8ef8b41945e1f0fbeac636693a910ec7c4ab05e85a97cfd9d48a10891e12835c8f9dfc3db59a7204044accdb4898929
7
+ data.tar.gz: 6e621c8fee7c6b5a533f78c56f5855a08638d6f5d74de9dd8df7e2b36698f96d728802fef70c3332af8c41e820cbd750a373fa57ca73bac316a9cae5f6857e48
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ gemfiles/*.gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.1
6
+ - 2.1.2
7
+ - jruby
8
+ gemfile:
9
+ - gemfiles/rails_3.gemfile
10
+ - gemfiles/rails_4.gemfile
data/Appraisals ADDED
@@ -0,0 +1,7 @@
1
+ appraise "rails-3" do
2
+ gem "rails", github: 'rails/rails', branch: '3-2-stable'
3
+ end
4
+
5
+ appraise "rails-4" do
6
+ gem "rails", github: 'rails/rails', branch: '4-0-stable'
7
+ end
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-page_caching.gemspec
4
+ gemspec
5
+
6
+ gem 'coveralls', require: false
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Wayne See
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,113 @@
1
+ # Rack::PageCaching
2
+
3
+ [![Dependency Status](https://gemnasium.com/weynsee/rack-page_caching.svg)](https://gemnasium.com/weynsee/rack-page_caching)
4
+ [![Build Status](https://travis-ci.org/weynsee/rack-page_caching.svg?branch=master)](https://travis-ci.org/weynsee/rack-page_caching)
5
+ [![Coverage Status](https://img.shields.io/coveralls/weynsee/rack-page_caching.svg)](https://coveralls.io/r/weynsee/rack-page_caching?branch=master)
6
+
7
+ Rack::PageCaching is a Rack middleware that aids in static page caching. It serves
8
+ the same purpose as [page caching in Rails](https://github.com/rails/actionpack-page_caching)
9
+ and was designed to be a drop-in replacement to Rails page caching.
10
+
11
+ Rack::PageCaching provides a few differences from Rails page caching. It is
12
+ implemented as a Rack middleware, which means transformations done on the
13
+ response by other middlewares will be present in the generated static page.
14
+ Examples of middlewares that work well with page caching include
15
+ [htmlcompressor](https://github.com/paolochiodi/htmlcompressor) and
16
+ [rack-pjax](https://github.com/eval/rack-pjax).
17
+ Implementing it in Rack also means that it can also be used in other
18
+ Rack-compatible frameworks like Sinatra.
19
+
20
+ While it was designed to be a drop-in replacement for Rails page caching, it
21
+ provides additional features like including the hostname in the path of the
22
+ generated static page.
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ gem 'rack-page_caching'
29
+
30
+ And then execute:
31
+
32
+ $ bundle
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install rack-page_caching
37
+
38
+ Tell your app to use the Rack::PageCaching middleware.
39
+ For Rails 3+ apps:
40
+
41
+ ```ruby
42
+ # In config/application.rb
43
+ config.middleware.use Rack::PageCaching
44
+ ```
45
+
46
+ Or for Rackup files:
47
+
48
+ ```ruby
49
+ # In config.ru
50
+ use Rack::PageCaching
51
+ ```
52
+
53
+ When you want page caching to be applied to a response modified by other
54
+ middlewares, make sure you place Rack::PageCaching *before* those
55
+ middlewares.
56
+
57
+ It has been tested on Rails 3 and 4, and on Ruby 1.9.3 and 2.x.
58
+
59
+ ## Usage
60
+
61
+ Rack::PageCaching accepts all the options accepted by Rails page caching:
62
+ ```ruby
63
+ config.middleware.use Rack::PageCaching,
64
+ # Directory where the pages are stored. Defaults to the public folder in
65
+ # Rails, but you'll probably want to customize this
66
+ page_cache_directory: Rails.root.join('public', 'page_cache'),
67
+ # Gzipped version of the files are generated with compression level
68
+ # specified. It accepts the symbol versions of the constants in Zlib,
69
+ # e.g. :best_speed and :best_compression. To turn off gzip, pass in false.
70
+ gzip: :best_speed,
71
+ # Hostnames can be included in the path of the page cache. Default is false.
72
+ include_hostname: false
73
+ ```
74
+ Rack::PageCaching respects `config.action_controller.perform_caching` and
75
+ will skip cache generation if it is false.
76
+
77
+ To configure your web server to serve the generated pages directly, point it to
78
+ `page_cache_directory`. For Nginx, it would look something like:
79
+ ```
80
+ if (-f $document_root/page_cache/$host/$uri/index.html) {
81
+ rewrite (.*) /page_cache/$host/$1/index.html break;
82
+ }
83
+
84
+ if (-f $document_root/page_cache/$host/$uri.html) {
85
+ rewrite (.*) /page_cache/$host/$1.html break;
86
+ }
87
+ ```
88
+
89
+ Rack::PageCaching implements the same class methods Rails page caching provides,
90
+ so if you were using it previously you can leave it as is:
91
+ ```ruby
92
+ class WeblogController < ActionController::Base
93
+ caches_page :show, :new
94
+ end
95
+ ```
96
+ Expiration is also supported the same way:
97
+ ```ruby
98
+ class WeblogController < ActionController::Base
99
+ def update
100
+ List.update(params[:list][:id], params[:list])
101
+ expire_page action: 'show', id: params[:list][:id]
102
+ redirect_to action: 'show', id: params[:list][:id]
103
+ end
104
+ end
105
+ ```
106
+
107
+ ## Contributing
108
+
109
+ 1. Fork it ( https://github.com/[my-github-username]/rack-page_caching/fork )
110
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
111
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
112
+ 4. Push to the branch (`git push origin my-new-feature`)
113
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/**/*_spec.rb"
6
+ t.libs << 'test'
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "coveralls", :require => false
6
+ gem "rails", :github => "rails/rails", :branch => "3-2-stable"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "coveralls", :require => false
6
+ gem "rails", :github => "rails/rails", :branch => "4-0-stable"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,61 @@
1
+ module Rack
2
+ class PageCaching
3
+ module ActionController
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def caches_page(*actions)
8
+ return unless perform_caching
9
+ options = actions.extract_options!
10
+ gzip_level = options.fetch(:gzip, Zlib::BEST_COMPRESSION)
11
+ gzip_level = Rack::PageCaching::Utils.gzip_level(gzip_level)
12
+ after_filter({ only: actions }.merge(options)) do |c|
13
+ c.request.env['rack.page_caching.perform_caching'] = true
14
+ c.request.env['rack.page_caching.compression'] = gzip_level
15
+ end
16
+ end
17
+ end
18
+
19
+ def expire_page(options = {})
20
+ return unless self.class.perform_caching
21
+
22
+ if options.is_a?(Hash)
23
+ if options[:action].is_a?(Array)
24
+ options[:action].each do |action|
25
+ Rack::PageCaching::Cache.delete(url_for(options.merge(only_path: true, action: action)))
26
+ end
27
+ else
28
+ Rack::PageCaching::Cache.delete(url_for(options.merge(only_path: true)))
29
+ end
30
+ else
31
+ Rack::PageCaching::Cache.delete(options)
32
+ end
33
+ end
34
+
35
+ def cache_page(content = nil, options = nil, gzip = Zlib::BEST_COMPRESSION)
36
+ return unless self.class.perform_caching
37
+
38
+ path = case options
39
+ when Hash
40
+ url_for(options.merge(only_path: true, format: params[:format]))
41
+ when String
42
+ options
43
+ else
44
+ request.path
45
+ end
46
+ path = path.chomp('/')
47
+
48
+ if (type = ::Mime::LOOKUP[self.content_type]) && (type_symbol = type.symbol).present?
49
+ path = "#{path}.#{type_symbol}" unless /#{type_symbol}\z/.match(path)
50
+ end
51
+
52
+ Rack::PageCaching::Cache.write_file(
53
+ Array(content || response.body), path,
54
+ Rack::PageCaching::Utils.gzip_level(gzip)
55
+ )
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ ActionController::Base.send(:include, Rack::PageCaching::ActionController)
@@ -0,0 +1,74 @@
1
+ require 'fileutils'
2
+ require 'zlib'
3
+
4
+ module Rack
5
+ class PageCaching
6
+ class Cache
7
+ def initialize(response)
8
+ @response = response
9
+ end
10
+
11
+ def store
12
+ if path = page_cache_path
13
+ self.class.write_file(@response.body, path, gzip_level)
14
+ end
15
+ end
16
+
17
+ def page_cache_path
18
+ path = Rack::Utils.unescape(@response.path.chomp('/'))
19
+ if !path.include?('..')
20
+ type = Rack::PageCaching::MimeTypes.extension_for @response.content_type
21
+ path = '/index' if path.empty?
22
+ path = "#{path}#{type}" unless /#{Regexp.quote(type)}\z/.match(path)
23
+ path = "/#{@response.host}#{path}" if config.include_hostname?
24
+ path
25
+ end
26
+ end
27
+
28
+ def gzip_level
29
+ @response.gzip_level || config.page_cache_compression
30
+ end
31
+
32
+ def self.store(response)
33
+ Rack::PageCaching::Cache.new(response).store
34
+ end
35
+
36
+ def self.write_file(content, path, gzip_level)
37
+ expand_path('write_page', path) do |full_path|
38
+ FileUtils.makedirs(::File.dirname(full_path))
39
+ ::File.open(full_path, 'wb+') { |f| content.each { |c| f.write(c) } }
40
+ if gzip_level
41
+ Zlib::GzipWriter.open(full_path + '.gz', gzip_level) do |f|
42
+ content.each do |c|
43
+ f.write(c)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ def self.delete(path)
51
+ expand_path('expire_page', path) do |full_path|
52
+ Dir[full_path].each do |file|
53
+ ::File.delete(file)
54
+ ::File.delete(file + '.gz') if ::File.exist?(file + '.gz')
55
+ end
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def self.expand_path(name, path)
62
+ env = Rack::PageCaching.environment
63
+ full_path = ::File.join(env.page_cache_directory, path)
64
+ env.instrument name, full_path do
65
+ yield full_path
66
+ end
67
+ end
68
+
69
+ def config
70
+ Rack::PageCaching.environment
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,64 @@
1
+ module Rack
2
+ class PageCaching
3
+ class Environment
4
+ def initialize(options = {})
5
+ @options = options
6
+ set_defaults
7
+ end
8
+
9
+ def page_cache_compression
10
+ @options[:page_cache_compression]
11
+ end
12
+
13
+ def page_cache_directory
14
+ @options[:page_cache_directory]
15
+ end
16
+
17
+ def include_hostname?
18
+ @options[:include_hostname]
19
+ end
20
+
21
+ def enabled?
22
+ @options[:enable]
23
+ end
24
+
25
+ def instrument(name, path)
26
+ if defined? ActiveSupport::Notifications
27
+ ActiveSupport::Notifications.instrument("#{name}.action_controller", path: path) do
28
+ yield
29
+ end
30
+ else
31
+ yield
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def set_defaults
38
+ set_rails_defaults if defined? ::Rails
39
+ normalize_values
40
+ toggle_caching
41
+ end
42
+
43
+ def normalize_values
44
+ @options.merge!(
45
+ page_cache_compression: Rack::PageCaching::Utils.gzip_level(
46
+ @options[:page_cache_compression]),
47
+ page_cache_directory: @options[:page_cache_directory].to_s,
48
+ )
49
+ end
50
+
51
+ def toggle_caching
52
+ @options[:enable] = false if @options[:page_cache_directory].strip == ''
53
+ @options[:enable] = true unless @options.has_key?(:enable)
54
+ end
55
+
56
+ def set_rails_defaults
57
+ @options = {
58
+ page_cache_directory: ::Rails.root.join('public'),
59
+ enable: ::Rails.application.config.action_controller.perform_caching
60
+ }.merge(@options)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,24 @@
1
+ require 'rack/mime'
2
+
3
+ module Rack
4
+ class PageCaching
5
+ class MimeTypes
6
+ def self.load!
7
+ mime_types = Rack::Mime::MIME_TYPES
8
+ extensions = Hash.new { |hash, key| hash[key] = [] }
9
+ mime_types.each do |extension, content_type|
10
+ extensions[content_type] << extension
11
+ end
12
+ @extension_lookup = extensions
13
+ end
14
+
15
+ def self.register(content_type, extension)
16
+ @extension_lookup[content_type] = [extension]
17
+ end
18
+
19
+ def self.extension_for(content_type)
20
+ @extension_lookup[content_type].first
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,50 @@
1
+ require 'rack/request'
2
+
3
+ module Rack
4
+ class PageCaching
5
+ class Response
6
+ def initialize(response, env)
7
+ @response, @env = response, env
8
+ end
9
+
10
+ def cacheable?
11
+ perform_caching? && caching_allowed?
12
+ end
13
+
14
+ def path
15
+ @path ||= request.path
16
+ end
17
+
18
+ def body
19
+ @response[2]
20
+ end
21
+
22
+ def content_type
23
+ @response[1]['Content-Type'].split(';').first
24
+ end
25
+
26
+ def gzip_level
27
+ @env['rack.page_caching.compression'].to_i if @env['rack.page_caching.compression']
28
+ end
29
+
30
+ def host
31
+ request.host
32
+ end
33
+
34
+ private
35
+
36
+ def request
37
+ @request ||= Rack::Request.new(@env)
38
+ end
39
+
40
+ def caching_allowed?
41
+ method = @env['REQUEST_METHOD']
42
+ (method == 'GET' || method == 'HEAD') && @response[0] == 200
43
+ end
44
+
45
+ def perform_caching?
46
+ @env['rack.page_caching.perform_caching'].to_s == 'true'
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,16 @@
1
+ module Rack
2
+ class PageCaching
3
+ module Utils
4
+ def self.gzip_level(gzip)
5
+ case gzip
6
+ when Symbol
7
+ Zlib.const_get(gzip.to_s.upcase)
8
+ when Fixnum
9
+ gzip
10
+ else
11
+ false
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class PageCaching
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ require "rack/page_caching/version"
2
+ require "rack/page_caching/utils"
3
+ require "rack/page_caching/environment"
4
+ require "rack/page_caching/response"
5
+ require "rack/page_caching/cache"
6
+ require "rack/page_caching/mime_types"
7
+
8
+ require "rack/page_caching/action_controller" if defined?(::Rails)
9
+
10
+ module Rack
11
+ class PageCaching
12
+
13
+ MimeTypes.load!
14
+ MimeTypes.register 'text/html', '.html'
15
+ MimeTypes.register 'text/plain', '.txt'
16
+
17
+ def initialize(app, options = {})
18
+ @app = app
19
+ self.class.environment = Rack::PageCaching::Environment.new(options)
20
+ end
21
+
22
+ def call(env)
23
+ rack_response = @app.call(env)
24
+ if self.class.environment.enabled?
25
+ response = Rack::PageCaching::Response.new(rack_response, env)
26
+ Rack::PageCaching::Cache.store(response) if response.cacheable?
27
+ end
28
+ rack_response
29
+ end
30
+
31
+ class << self
32
+ attr_accessor :environment
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/page_caching/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-page_caching"
8
+ spec.version = Rack::PageCaching::VERSION
9
+ spec.authors = ["Wayne See"]
10
+ spec.email = ["weynsee@gmail.com"]
11
+ spec.summary = %q{Page caching for Rack.}
12
+ spec.description = %q{Rack middleware to generate pages that can be served by a webserver. Compatible with Rails page caching.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 1.9.2'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "rack-test"
27
+ spec.add_development_dependency "appraisal"
28
+ spec.add_development_dependency "rails", ">= 3"
29
+ spec.add_dependency "rack"
30
+ end