#!/usr/bin/env ruby require 'rack' require 'time' class NavelApp def call(env) send(:"handle_#{env['REQUEST_METHOD'].downcase}", Rack::Request.new(env)) end private def handle_get(request) case request.path_info when '/affirmation' return [200, {'Content-Type' => 'text/plain'}, ["yup\n"]] else handle_404(request) end end def handle_post(request) case request.path_info when '/dinner/new' body = request.body.read if body.split("\n").include?('soup=splitpea') return [201, {'Content-Type' => 'text/plain'}, ["soup is on!\n"]] else return [ 400, {'Content-Type' => 'text/plain'}, ["invalid dinner:\n" << body << "\n"] ] end else handle_404(request) end end def handle_delete(request) case request.path_info when '/pestilence' return [204, {'Content-Type' => 'text/plain'}, ["orphanage saved!\n"]] else handle_404(client) end end def handle_404(client) [404, {'Content-Type' => 'text/plain'} ["nope\n"]] end end def main(argv = []) port = Integer(argv.first || 19080) $stdout.sync = true $stderr.sync = true Rack::Handler::Thin.run(NavelApp.new, Port: port) end if __FILE__ == $0 main(ARGV) end