Sha256: f9a258ebb531ba9b3273de0131663bceacedeeb47dfe50dd1f7e7b2c6e00abf7

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

module Rubai
  class RackApp

    def call(env)
      @env = env

      rack_response = RackResponse.new

      if File.file?(path)
        response = success_response
      else
        response = not_found_response
      end

      rack_response.respond_with(response)
    end

    private

    def path
      if env["PATH_INFO"] == "/"
        path = "index.html"
      else
        path = "./#{env["PATH_INFO"]}"
      end
      path
    end

    def env
      @env
    end

    def not_found_response
      response = Response.new
      response.status = 404
      response.content_type = MimeType.new(:html).content_type
      # TODO: this response should be configurable
      response << "<html>"
      response << "  <body>"
      response << "    NO!"
      response << "  </body>"
      response << "</html>"
      response
    end

    def success_response
      extension = File.extname(path)
      mime_type = MimeType.new(extension)
      Response.new 200, mime_type.content_type, [File.read(path)]
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubai-0.1.2 lib/rubai/rack_app.rb
rubai-0.1.1 lib/rubai/rack_app.rb