Created
October 21, 2013 21:30
-
-
Save nata79/7091356 to your computer and use it in GitHub Desktop.
This file contains 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 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