Added param parser

This commit is contained in:
Sdogruyol 2015-10-28 20:30:27 +02:00
parent a8ad95aa67
commit 3cc50e0a7e
8 changed files with 72 additions and 52 deletions

View file

@ -20,21 +20,10 @@ class Kemal::Handler < HTTP::Handler
def exec_request(request)
components = request.path.not_nil!.split "/"
@routes.each do |route|
params = route.match(request.method, components)
if params
if query = request.query
HTTP::Params.parse(query) do |key, value|
params[key] ||= value
end
end
if body = request.body
HTTP::Params.parse(request.body.not_nil!) do |key, value|
params[key] ||= value
end
end
context = Context.new(request, params)
match = route.match?(request)
if match
params = Kemal::Params.new(route, request).parse
context = Context.new(request, params.not_nil!)
begin
body = route.handler.call(context).to_s
content_type = context.response?.try(&.content_type) || "text/plain"

View file

@ -1,5 +1,6 @@
class Kemal::Logger
LOG_LEVELS = %w(info debug error warn)
def initialize(@stream)
end
@ -10,6 +11,6 @@ class Kemal::Logger
{% end %}
def exception(e)
error "#{e.message}:\n\t#{e.backtrace.join("\n\t")}"
error "#{e.message}:\n\t#{e.backtrace.join("\n\t")}"
end
end

37
src/kemal/param_parser.cr Normal file
View file

@ -0,0 +1,37 @@
class Kemal::Params
def initialize(@route, @request)
@route_components = route.components
@request_components = request.path.not_nil!.split "/"
@params = {} of String => String
end
def parse
parse_components
parse_request
end
def parse_request
if query = @request.query
HTTP::Params.parse(query) do |key, value|
@params[key] ||= value
end
end
if body = @request.body
HTTP::Params.parse(body.not_nil!) do |key, value|
@params[key] ||= value
end
end
@params
end
def parse_components
@route_components.zip(@request_components) do |route_component, req_component|
if route_component.starts_with? ':'
@params[route_component[1..-1]] = req_component
else
return nil unless route_component == req_component
end
end
end
end

View file

@ -1,26 +1,15 @@
class Kemal::Route
getter handler
getter components
def initialize(@method, path, &@handler : Kemal::Context -> _)
@components = path.split "/"
end
def match(method, components)
return nil unless method == @method
def match?(request)
components = request.path.not_nil!.split "/"
return nil unless request.method == @method
return nil unless components.size == @components.size
params = nil
@components.zip(components) do |route_component, req_component|
if route_component.starts_with? ':'
params ||= {} of String => String
params[route_component[1..-1]] = req_component
else
return nil unless route_component == req_component
end
end
params ||= {} of String => String
params
true
end
end