Sha256: e62d0a6524203b99bc6d809ccafb3a4c32c6312039f19f4d3f70449330d43f14

Contents?: true

Size: 946 Bytes

Versions: 3

Compression:

Stored size: 946 Bytes

Contents

module Bunch
  class Rack
    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

      Bunch.load_ignores(@root)
    end

    def call(env)
      path = @root.join(env['PATH_INFO'].sub(/^\//, '')).to_s
      type = MIME::Types.type_for(path).first || 'text/plain'

      [200, headers(type), [content_for(path)]]
    rescue => e
      [500, headers('text/plain'), [error_log(e)]]
    end

    private
      def content_for(path)
        Bunch.content_for(path, :root => @root)
      end

      def headers(mime_type)
        @headers.merge('Content-Type' => mime_type.to_s)
      end

      def error_log(e)
        "#{e.class}: #{e.message}\n  #{e.backtrace.join("\n  ")}"
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bunch-0.2.2 lib/bunch/rack.rb
bunch-0.2.1 lib/bunch/rack.rb
bunch-0.2.0 lib/bunch/rack.rb