Skip to content

Instantly share code, notes, and snippets.

@ox
Created March 6, 2012 18:52
Show Gist options
  • Save ox/1988159 to your computer and use it in GitHub Desktop.
Save ox/1988159 to your computer and use it in GitHub Desktop.
A rack application that allows for some rudimentary routing using procs
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