Sha256: 846cb52ec94d192aae6ce8f410247d924b7f130ab99d659ee8254f21cdfceba9

Contents?: true

Size: 1006 Bytes

Versions: 2

Compression:

Stored size: 1006 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
    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 headers(mime_type)
        @headers.merge('Content-Type' => mime_type.to_s)
      end

    private
      def content_for(path)
        Bunch.content_for(path)
      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

2 entries across 2 versions & 1 rubygems

Version Path
bunch-0.0.6 lib/bunch/rack.rb
bunch-0.0.5 lib/bunch/rack.rb