Class: Rack::Staticifier
- Inherits:
-
Object
- Object
- Rack::Staticifier
- Defined in:
- lib/rack-staticifier.rb
Overview
Rack::Staticifier is Rack middleware for staticly caching responses.
Usage
# this will cache ALL responses in a 'cache' directory
use Rack::Staticifier
# this will cache ALL responses in a 'public/my/cached/stuff' directory
use Rack::Staticifier, :root => 'public/my/cached/stuff'
# this will only cache requests with 'foo' in the URL
use Rack::Staticifier do |env, response|
env['PATH_INFO'].include?('foo')
end
# this will only cache requests with 'hi' in the response body
use Rack::Staticifier do |env, response|
# response is a regular Rack response, eg. [200, {}, ['hi there']]
body = ''
response.last.each {|string| body << string }
body.include?('hi')
end
# this will only cache requests with 'foo' in the URL (incase you don't want to pass a block)
use Rack::Staticifier, :cache_if => lambda { |env, response| env['PATH_INFO'].include?('foo') }
Constant Summary collapse
- STATUS_CODES_NOT_TO_CACHE =
[ 304 ]
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
the Rack application.
-
#config ⇒ Object
readonly
configuration options.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, config_options = nil, &block) ⇒ Staticifier
constructor
A new instance of Staticifier.
Constructor Details
#initialize(app, config_options = nil, &block) ⇒ Staticifier
Returns a new instance of Staticifier.
39 40 41 42 43 44 45 |
# File 'lib/rack-staticifier.rb', line 39 def initialize app, = nil, &block @app = app @config = config.merge!() if config[:cache_if] = block if block end |
Instance Attribute Details
#app ⇒ Object (readonly)
the Rack application
34 35 36 |
# File 'lib/rack-staticifier.rb', line 34 def app @app end |
#config ⇒ Object (readonly)
configuration options
37 38 39 |
# File 'lib/rack-staticifier.rb', line 37 def config @config end |
Instance Method Details
#call(env) ⇒ Object
47 48 49 50 51 |
# File 'lib/rack-staticifier.rb', line 47 def call env response = @app.call env cache_response(env, response) if should_cache_response?(env, response) response end |