Handle POST request, and some refactors

This commit is contained in:
Ary Borenszweig 2014-07-30 19:58:48 -03:00
parent e601730a0c
commit fc4f648c1a
6 changed files with 39 additions and 14 deletions

View file

@ -1,3 +1,7 @@
def get(path, &block : Frank::Request -> String)
Frank::Handler::INSTANCE.add_route(path, block)
Frank::Handler::INSTANCE.add_route("GET", path, &block)
end
def post(path, &block : Frank::Request -> String)
Frank::Handler::INSTANCE.add_route("POST", path, &block)
end

View file

@ -19,15 +19,16 @@ class Frank::Handler < HTTP::Handler
end
end
def add_route(path, handler)
@routes << Route.new(path, handler)
def add_route(method, path, &handler : Frank::Request -> String)
@routes << Route.new(method, path, &handler)
end
def exec_request(request)
components = request.path.split "/"
@routes.each do |route|
frank_request = route.match(request, components)
if frank_request
params = route.match(request.method, components)
if params
frank_request = Request.new(params)
return route.handler.call(frank_request)
end
end

View file

@ -1,11 +1,12 @@
class Frank::Route
getter handler
def initialize(path, @handler)
def initialize(@method, path, &@handler : Frank::Request -> String)
@components = path.split "/"
end
def match(request, components)
def match(method, components)
return nil unless method == @method
return nil unless components.length == @components.length
params = nil
@ -20,6 +21,6 @@ class Frank::Route
end
params ||= {} of String => String
Request.new(params)
params
end
end