From 596452f838a5ec27b601a840010b201d98dbeddf Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Fri, 3 Mar 2017 23:56:29 +0300 Subject: [PATCH] More docs --- src/kemal.cr | 3 ++ src/kemal/handler.cr | 99 ++++++++++++++++++++------------------- src/kemal/init_handler.cr | 4 +- src/kemal/param_parser.cr | 4 -- src/kemal/request.cr | 1 - 5 files changed, 57 insertions(+), 54 deletions(-) diff --git a/src/kemal.cr b/src/kemal.cr index fab959c..fc43022 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -1,4 +1,7 @@ require "http" +require "json" +require "uri" +require "tempfile" require "./kemal/*" require "./kemal/helpers/*" diff --git a/src/kemal/handler.cr b/src/kemal/handler.cr index 15e23b7..76e99be 100644 --- a/src/kemal/handler.cr +++ b/src/kemal/handler.cr @@ -1,65 +1,70 @@ -class Kemal::Handler - include HTTP::Handler - @@only_routes_tree = Radix::Tree(String).new - @@exclude_routes_tree = Radix::Tree(String).new +module Kemal + # `Kemal::Handler` is a subclass of `HTTP::Handler`. + # It adds `only`, `only_match?`, `exclude`, `exclude_match?`. + # These methods are useful for custom handlers for conditional execution. + class Handler + include HTTP::Handler + @@only_routes_tree = Radix::Tree(String).new + @@exclude_routes_tree = Radix::Tree(String).new - macro only(paths, method = "GET") + macro only(paths, method = "GET") class_name = {{@type.name}} {{paths}}.each do |path| @@only_routes_tree.add "#{class_name}/#{{{method}}.downcase}#{path}", "/#{{{method}}.downcase}#{path}" end end - macro exclude(paths, method = "GET") + macro exclude(paths, method = "GET") class_name = {{@type.name}} {{paths}}.each do |path| @@exclude_routes_tree.add "#{class_name}/#{{{method}}.downcase}#{path}", "/#{{{method}}.downcase}#{path}" end end - def call(env) - call_next(env) - end + def call(env) + call_next(env) + end - # Processes the path based on `only` paths which is a `Array(String)`. - # If the path is not found on `only` conditions the handler will continue processing. - # If the path is found in `only` conditions it'll stop processing and will pass the request - # to next handler. - # - # However this is not done automatically. All handlers must inherit from `Kemal::Handler`. - # - # class OnlyHandler < Kemal::Handler - # only ["/"] - # - # def call(env) - # return call_next(env) unless only_match?(env) - # puts "If the path is / i will be doing some processing here." - # end - # end - def only_match?(env) - @@only_routes_tree.find(radix_path(env.request.method, env.request.path)).found? - end + # Processes the path based on `only` paths which is a `Array(String)`. + # If the path is not found on `only` conditions the handler will continue processing. + # If the path is found in `only` conditions it'll stop processing and will pass the request + # to next handler. + # + # However this is not done automatically. All handlers must inherit from `Kemal::Handler`. + # + # class OnlyHandler < Kemal::Handler + # only ["/"] + # + # def call(env) + # return call_next(env) unless only_match?(env) + # puts "If the path is / i will be doing some processing here." + # end + # end + def only_match?(env) + @@only_routes_tree.find(radix_path(env.request.method, env.request.path)).found? + end - # Processes the path based on `exclude` paths which is a `Array(String)`. - # If the path is not found on `exclude` conditions the handler will continue processing. - # If the path is found in `exclude` conditions it'll stop processing and will pass the request - # to next handler. - # - # However this is not done automatically. All handlers must inherit from `Kemal::Handler`. - # - # class ExcludeHandler < Kemal::Handler - # exclude ["/"] - # - # def call(env) - # return call_next(env) if exclude_match?(env) - # puts "If the path is not / i will be doing some processing here." - # end - # end - def exclude_match?(env) - @@exclude_routes_tree.find(radix_path(env.request.method, env.request.path)).found? - end + # Processes the path based on `exclude` paths which is a `Array(String)`. + # If the path is not found on `exclude` conditions the handler will continue processing. + # If the path is found in `exclude` conditions it'll stop processing and will pass the request + # to next handler. + # + # However this is not done automatically. All handlers must inherit from `Kemal::Handler`. + # + # class ExcludeHandler < Kemal::Handler + # exclude ["/"] + # + # def call(env) + # return call_next(env) if exclude_match?(env) + # puts "If the path is not / i will be doing some processing here." + # end + # end + def exclude_match?(env) + @@exclude_routes_tree.find(radix_path(env.request.method, env.request.path)).found? + end - private def radix_path(method : String, path) - "#{self.class}/#{method.downcase}#{path}" + private def radix_path(method : String, path) + "#{self.class}/#{method.downcase}#{path}" + end end end diff --git a/src/kemal/init_handler.cr b/src/kemal/init_handler.cr index 322d8f7..ce39b0a 100644 --- a/src/kemal/init_handler.cr +++ b/src/kemal/init_handler.cr @@ -1,6 +1,6 @@ module Kemal - # Kemal::InitHandler is the first handler thus initializes the context with default values such as - # Content-Type, X-Powered-By. + # Kemal::InitHandler is the first handler thus initializes the context with default values. + # Such as *Content-Type*, *X-Powered-By* headers. class InitHandler include HTTP::Handler INSTANCE = new diff --git a/src/kemal/param_parser.cr b/src/kemal/param_parser.cr index bfd963a..f95fb24 100644 --- a/src/kemal/param_parser.cr +++ b/src/kemal/param_parser.cr @@ -1,7 +1,3 @@ -require "json" -require "uri" -require "tempfile" - module Kemal # ParamParser parses the request contents including query_params and body # and converts them into a params hash which you can within the environment diff --git a/src/kemal/request.cr b/src/kemal/request.cr index 18fee5e..f027de5 100644 --- a/src/kemal/request.cr +++ b/src/kemal/request.cr @@ -1,4 +1,3 @@ -# Opening HTTP::Request to add override_method property class HTTP::Request property override_method property url_params : Hash(String, String)?