From fa002653451016afc468755eb12bb6f51fbb68d1 Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Sat, 24 Oct 2015 22:19:46 +0300 Subject: [PATCH] Use macro to generate DSL and support delete,patch verbs --- README.md | 2 +- src/kemal/dsl.cr | 12 ++++++------ src/kemal/handler.cr | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 374b274..8e09239 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ dependencies: ## Status -Basic `get`, `put`, `post` and `head` routes can be matched, and request parameters can be obtained. +Basic `get`, `put`, `post`, `patch`, `delete` and `head` routes can be matched, and request parameters can be obtained. ## Thanks diff --git a/src/kemal/dsl.cr b/src/kemal/dsl.cr index 21c59b5..111613c 100644 --- a/src/kemal/dsl.cr +++ b/src/kemal/dsl.cr @@ -1,7 +1,7 @@ -def get(path, &block : Kemal::Context -> _) - Kemal::Handler::INSTANCE.add_route("GET", path, &block) -end +HTTP_METHODS = %w(get post put patch delete) -def post(path, &block : Kemal::Context -> _) - Kemal::Handler::INSTANCE.add_route("POST", path, &block) -end +{% for method in HTTP_METHODS %} + def {{method.id}}(path, &block : Kemal::Context -> _) + Kemal::Handler::INSTANCE.add_route({{method}}.upcase, path, &block) + end +{% end %} diff --git a/src/kemal/handler.cr b/src/kemal/handler.cr index e0c6d41..89e46e1 100644 --- a/src/kemal/handler.cr +++ b/src/kemal/handler.cr @@ -17,7 +17,7 @@ class Kemal::Handler < HTTP::Handler @routes << Route.new(method, path, &handler) end - def exec_request(request) + def exec_request(request) components = request.path.not_nil!.split "/" @routes.each do |route| params = route.match(request.method, components)