Sha256: 59dcd5c2cab172d877274ad7885d17ad3145a839b601bdac553140778ba00251

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

require 'wunderbar'
require 'rack'

module Wunderbar
  class RackApp
    # entry point for Rack
    def call(env)
      @_env = env
      @_request = Rack::Request.new(env)
      @_response = Rack::Response.new
      Wunderbar.logger = @_request.logger
      file = Wunderbar.files[env['PATH_INFO']]

      if file
        mime = file[:mime] ||
          Rack::Mime::MIME_TYPES[File.extname(env['PATH_INFO'])]
        @_response.set_header('Content-Type', mime) if mime
        @_response.write(file[:content] || file[:source].call)
      elsif Wunderbar.safe? and $SAFE==0
        Proc.new { $SAFE=1; Wunderbar::CGI.call(self) }.call
      else
        Wunderbar::CGI.call(self)
      end
      @_response.finish
    end

    # redirect the output produced
    def out(headers,&block)
      status = headers.delete('status')
      @_response.status = status if status

      headers = Wunderbar::CGI.headers(headers)
      headers.each {|key, value| @_response[key] = value}

      @_response.write block.call unless @_request.head?
    end

    def env
      @_env
    end

    def params
      @_request.params
    end

    def request
      @_request
    end

    def response
      @_response
    end

    %w(delete get head options post put trace).each do |http_method|
      define_method "#{http_method}?" do
        @_env['REQUEST_METHOD'].to_s.downcase == http_method
      end
    end
  end
end

class Rack::Builder
  include Wunderbar::API

  def _app
    Wunderbar::RackApp.new
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wunderbar-1.3.3 lib/wunderbar/rack.rb
wunderbar-1.3.2 lib/wunderbar/rack.rb
wunderbar-1.3.1 lib/wunderbar/rack.rb