Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Created September 20, 2011 22:24
Show Gist options
  • Select an option

  • Save steveklabnik/1230614 to your computer and use it in GitHub Desktop.

Select an option

Save steveklabnik/1230614 to your computer and use it in GitHub Desktop.
A little router
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