#!/usr/bin/env ruby require 'rubygems' require 'mongrel' class SaidHandler < Mongrel::HttpHandler def process(request, response) @request, @params = request, nil response.start(200) do |head, out| head["Content-Type"] = "text/html" out.write action end end def action if (id = params['cont_id']) && @cont[id] block, @b = @cont[id] return block.call end said end def params unless @params @params = @request.class.query_parse(@request.params['QUERY_STRING']) if @request.params['REQUEST_METHOD'] == "POST" @request.class.query_parse(@request.body.read).each { |key, value| @params[key] = value } end end @params end def arg(key) (@b || params)[key.to_s] end def said aform(input("foo"), submit) { w_link("click here") { "you said: #{arg :foo}"}} end def aform(*args, &block) "
" end def input(*args) "" end def w_link(*args, &block) "#{args.join}" end def submit '' end def make_cont(block) @cont ||= {} id = loop { x = rand(100).to_s; break(x) unless @cont.keys.include?(x) } @cont[id] = [block, params] id end end h = Mongrel::HttpServer.new("0.0.0.0", "3000") h.register("/said", SaidHandler.new) h.run.join