lib/bunch/rack.rb in bunch-0.0.3 vs lib/bunch/rack.rb in bunch-0.0.4

- old
+ new

@@ -1,20 +1,31 @@ module Bunch class Rack - def initialize(path) + def initialize(path, opts={}) @root = Pathname.new(path) + @headers = {} + + if opts.delete(:no_cache) + @headers['Cache-Control'] = 'private, max-age=0, must-revalidate' + @headers['Pragma'] = 'no-cache' + @headers['Expires'] = 'Thu, 01 Dec 1994 16:00:00 GMT' + end end def call(env) path = @root.join(env['PATH_INFO'].sub(/^\//, '')).to_s type = MIME::Types.type_for(path).first || 'text/plain' - [ 200, { 'Content-Type' => type.to_s }, [ generate(path) ] ] + [200, headers(type), [generate(path)]] rescue => e - [ 500, { 'Content-Type' => 'text/plain' }, [ error_log(e) ] ] + [500, headers('text/plain'), [error_log(e)]] end private + def headers(mime_type) + @headers.merge('Content-Type' => mime_type.to_s) + end + def generate(path) case when File.exist?(path) contents(path) when File.exist?(chopped_path = path.sub(%r(\.[^.]*$), ''))