Created
September 20, 2011 22:24
-
-
Save steveklabnik/1230614 to your computer and use it in GitHub Desktop.
A little router
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Router | |
| def initialize | |
| @route_list = {:get => {}, :post => {}, :put => {}, :delete => {}} | |
| end | |
| def add_route(method, route, &blk) | |
| @route_list[method][route] = blk | |
| end | |
| def call(env) | |
| begin | |
| method = env["REQUEST_METHOD"].downcase.to_sym | |
| route = env["PATH_INFO"] | |
| content = @route_list[method][route].call | |
| return [200, {"Content-Type" => "text/html"}, [content]] | |
| rescue Exception => e | |
| return [404, {"Content-Type" => "text/html"}, ["Not Found"]] | |
| end | |
| end | |
| end | |
| router = Router.new | |
| router.add_route(:get, "/hello") do | |
| "hello world!" | |
| end | |
| run router |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment