Sha256: 72ff893cfe169ce265358314c3743861f0e94442db219c3a9b5ffe69b1621c7b

Contents?: true

Size: 905 Bytes

Versions: 1

Compression:

Stored size: 905 Bytes

Contents

module Bunch
  class Rack
    def initialize(path)
      @root = Pathname.new(path)
    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) ] ]
    rescue => e
      [ 500, { 'Content-Type' => 'text/plain' }, [ error_log(e)   ] ]
    end

    private
      def generate(path)
        if File.exist?(path)
          Bunch::Tree(path).contents
        elsif File.exist?(chopped_path = path.sub(%r(\.[^/]*?$), ''))
          Bunch::Tree(chopped_path).contents
        elsif File.basename(path).start_with?('all.')
          Bunch::Tree(File.dirname(path)).contents
        else
          raise Errno::ENOENT, path
        end
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bunch-0.0.1 lib/bunch/rack.rb