Created
March 6, 2012 18:52
-
-
Save ox/1988159 to your computer and use it in GitHub Desktop.
A rack application that allows for some rudimentary routing using procs
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 | |
attr_accessor :routes | |
def initialize | |
@routes = {} | |
end | |
def add_route(path, &block) | |
@routes[path] = block | |
end | |
def call(env) | |
puts "calling #{env['REQUEST_PATH']}" | |
if @routes[env["REQUEST_PATH"]] | |
@routes[env["REQUEST_PATH"]].call | |
else | |
@routes["/"].call | |
end | |
end | |
end | |
router = Router.new | |
router.add_route "/" do | |
[200, {"Content-Type" => "text/html"}, [1+4]] | |
end | |
router.add_route "/something" do | |
[200, {"Content-Type" => "text/html"}, ["something"]] | |
end | |
run router |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment