remi-rack-staticifier 0.1.0 → 0.1.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/README.rdoc +23 -2
- data/VERSION.yml +1 -1
- data/examples/my-sinatra-blog.rb +28 -0
- data/lib/rack-staticifier.rb +27 -1
- data/spec/rack-staticifier_spec.rb +21 -0
- metadata +2 -1
data/README.rdoc
CHANGED
@@ -18,8 +18,29 @@ You can use Rack::Staticifier to statically cache responses to your app.
|
|
18
18
|
|
19
19
|
== Install
|
20
20
|
|
21
|
-
|
21
|
+
$ gem sources -a http://gems.github.com
|
22
|
+
$ sudo gem install remi-rack-staticifier
|
22
23
|
|
23
24
|
== Usage
|
24
25
|
|
25
|
-
|
26
|
+
# this will cache ALL responses in a 'cache' directory
|
27
|
+
use Rack::Staticifier
|
28
|
+
|
29
|
+
# this will cache ALL responses in a 'public/my/cached/stuff' directory
|
30
|
+
use Rack::Staticifier, :root => 'public/my/cached/stuff'
|
31
|
+
|
32
|
+
# this will only cache requests with 'foo' in the URL
|
33
|
+
use Rack::Staticifier do |env, response|
|
34
|
+
env['PATH_INFO'].include?('foo')
|
35
|
+
end
|
36
|
+
|
37
|
+
# this will only cache requests with 'hi' in the response body
|
38
|
+
use Rack::Staticifier do |env, response|
|
39
|
+
# response is a regular Rack response, eg. [200, {}, ['hi there']]
|
40
|
+
body = ''
|
41
|
+
response.last.each {|string| body << string }
|
42
|
+
body.include?('hi')
|
43
|
+
end
|
44
|
+
|
45
|
+
# this will only cache requests with 'foo' in the URL (incase you don't want to pass a block)
|
46
|
+
use Rack::Staticifier, :cache_if => lambda { |env, response| env['PATH_INFO'].include?('foo') }
|
data/VERSION.yml
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
%w( rubygems sinatra rack/staticifier ).each {|lib| require lib }
|
3
|
+
|
4
|
+
# sinatra serves out of the public directory by default
|
5
|
+
use Rack::Staticifier, :root => 'public' do |env, response|
|
6
|
+
response.first == 200 # only staticly cache 200 responses, meaning /non-existent-page-name should render OK
|
7
|
+
end
|
8
|
+
|
9
|
+
$posts = {
|
10
|
+
'hello-world' => 'Hello World!',
|
11
|
+
'hi-there' => 'Hi from my blog post text'
|
12
|
+
}
|
13
|
+
|
14
|
+
get '/' do
|
15
|
+
"Home Page rendered at #{ Time.now }"
|
16
|
+
end
|
17
|
+
|
18
|
+
get '/:post.html' do
|
19
|
+
name = params['post']
|
20
|
+
|
21
|
+
if $posts.keys.include?(name)
|
22
|
+
$posts[name]
|
23
|
+
|
24
|
+
else
|
25
|
+
status 404
|
26
|
+
"Cannot find page: #{ name }"
|
27
|
+
end
|
28
|
+
end
|
data/lib/rack-staticifier.rb
CHANGED
@@ -1,6 +1,31 @@
|
|
1
1
|
module Rack #:nodoc:
|
2
2
|
|
3
|
-
# Rack::Staticifier
|
3
|
+
# Rack::Staticifier is Rack middleware for staticly caching responses.
|
4
|
+
#
|
5
|
+
# ==== Usage
|
6
|
+
#
|
7
|
+
# # this will cache ALL responses in a 'cache' directory
|
8
|
+
# use Rack::Staticifier
|
9
|
+
#
|
10
|
+
# # this will cache ALL responses in a 'public/my/cached/stuff' directory
|
11
|
+
# use Rack::Staticifier, :root => 'public/my/cached/stuff'
|
12
|
+
#
|
13
|
+
# # this will only cache requests with 'foo' in the URL
|
14
|
+
# use Rack::Staticifier do |env, response|
|
15
|
+
# env['PATH_INFO'].include?('foo')
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# # this will only cache requests with 'hi' in the response body
|
19
|
+
# use Rack::Staticifier do |env, response|
|
20
|
+
# # response is a regular Rack response, eg. [200, {}, ['hi there']]
|
21
|
+
# body = ''
|
22
|
+
# response.last.each {|string| body << string }
|
23
|
+
# body.include?('hi')
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# # this will only cache requests with 'foo' in the URL (incase you don't want to pass a block)
|
27
|
+
# use Rack::Staticifier, :cache_if => lambda { |env, response| env['PATH_INFO'].include?('foo') }
|
28
|
+
#
|
4
29
|
class Staticifier
|
5
30
|
|
6
31
|
# the Rack application
|
@@ -37,6 +62,7 @@ module Rack #:nodoc:
|
|
37
62
|
|
38
63
|
def cache_response env, response
|
39
64
|
request_path = env['PATH_INFO']
|
65
|
+
request_path << 'index.html' if request_path.end_with?('/')
|
40
66
|
|
41
67
|
basename = ::File.basename request_path
|
42
68
|
dirname = ::File.join config[:root], ::File.dirname(request_path) # TODO grab 'public' from the config options
|
@@ -11,6 +11,27 @@ describe Rack::Staticifier do
|
|
11
11
|
@app = lambda {|env| [200, {}, ["hello from #{env['PATH_INFO']}"]] }
|
12
12
|
end
|
13
13
|
|
14
|
+
after :all do
|
15
|
+
%w( public cache foo ).each {|dir| FileUtils.rm_rf dir } # clean up!
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should render index.html for any requests ending in a slash' do
|
19
|
+
app = Rack::Staticifier.new @app
|
20
|
+
|
21
|
+
# for the '/' route
|
22
|
+
File.file?("cache/index.html").should be_false
|
23
|
+
RackBox.request app, "/"
|
24
|
+
File.file?("cache/index.html").should be_true
|
25
|
+
File.read("cache/index.html").should == "hello from /"
|
26
|
+
|
27
|
+
%w( foo/ bar/ ).each do |uri|
|
28
|
+
File.file?("cache/#{uri}index.html").should be_false
|
29
|
+
RackBox.request app, "/#{uri}"
|
30
|
+
File.file?("cache/#{uri}index.html").should be_true
|
31
|
+
File.read("cache/#{uri}index.html").should == "hello from /#{uri}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
14
35
|
it 'should cache all requests by default (in cache directory)' do
|
15
36
|
app = Rack::Staticifier.new @app
|
16
37
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remi-rack-staticifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- remi
|
@@ -29,6 +29,7 @@ files:
|
|
29
29
|
- lib/rack
|
30
30
|
- lib/rack/staticifier.rb
|
31
31
|
- spec/rack-staticifier_spec.rb
|
32
|
+
- examples/my-sinatra-blog.rb
|
32
33
|
has_rdoc: true
|
33
34
|
homepage: http://github.com/remi/rack-staticifier
|
34
35
|
post_install_message:
|