Skip to content

Instantly share code, notes, and snippets.

@nata79
Created October 21, 2013 21:30
Show Gist options
  • Save nata79/7091356 to your computer and use it in GitHub Desktop.
Save nata79/7091356 to your computer and use it in GitHub Desktop.
class App
def initialize
router.get '/sparta' do
[200, { 'Content-Type' => 'text/plain' }, ["This is Rack!"]]
end
router.get '/knock' do
[200, { 'Content-Type' => 'text/plain' }, ["Who's there?"]]
end
end
def call(env)
router.call(env)
end
private
def router
@router ||= Router.new
end
end
class Router
def get(path, &block)
register("GET #{path}", &block)
end
def call(env)
method_and_path = "#{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
if routes[method_and_path].nil?
[404, { 'Content-Type' => 'text/plain' }, []]
else
routes[method_and_path].call(env)
end
end
private
def register(method_and_path, &block)
routes[method_and_path] = block
end
def routes
@routes ||= {}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment