From e53ba1d9da3be00007f204250a62c7ff4648825e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Kadir=20Ak=C4=B1n?= Date: Sun, 6 Mar 2016 14:08:59 +0200 Subject: [PATCH] Decouple ParamContainer which makes the code cleaner, and fix kemal.cr url -- by @sdogruyol --- src/kemal.cr | 2 +- src/kemal/param_parser.cr | 34 +++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/kemal.cr b/src/kemal.cr index f89976e..db3ea3b 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -18,7 +18,7 @@ at_exit do # This route serves the built-in images for not_found and exceptions. get "/__kemal__/:image" do |env| - image = env.params["image"] + image = env.params.url["image"] file_path = File.expand_path("libs/kemal/images/#{image}", Dir.current) if File.exists? file_path env.response.headers.add "Content-Type", "application/octet-stream" diff --git a/src/kemal/param_parser.cr b/src/kemal/param_parser.cr index 37f24dc..673c806 100644 --- a/src/kemal/param_parser.cr +++ b/src/kemal/param_parser.cr @@ -6,12 +6,16 @@ require "json" alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type) class Kemal::ParamContainer - getter url - getter query - getter body - getter json + property url + property query + property body + property json - def initialize(@url, @query, @body, @json) + def initialize + @url = {} of String => String + @query = {} of String => String + @body = {} of String => String + @json = {} of String => AllParamTypes end def all @@ -24,15 +28,12 @@ class Kemal::ParamParser APPLICATION_JSON = "application/json" def initialize(@request) - @url = {} of String => String - @query = {} of String => String - @body = {} of String => String - @json = {} of String => AllParamTypes + @param_container = Kemal::ParamContainer.new end def params parse_request - Kemal::ParamContainer.new(@url, @query, @body, @json) + @param_container end def parse_request @@ -44,17 +45,17 @@ class Kemal::ParamParser def parse_body return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil? - @body = parse_part(@request.body) + @param_container.body = parse_part(@request.body) end def parse_query - @query = parse_part(@request.query) + @param_container.query = parse_part(@request.query) end def parse_url_params if params = @request.url_params params.each do |key, value| - @url[key as String] = value as String + @param_container.url[key as String] = value as String end end end @@ -70,10 +71,10 @@ class Kemal::ParamParser case json = JSON.parse(body).raw when Hash json.each do |key, value| - @json[key as String] = value as AllParamTypes + @param_container.json[key as String] = value as AllParamTypes end when Array - @json["_json"] = json + @param_container.json["_json"] = json end end @@ -86,4 +87,7 @@ class Kemal::ParamParser end part_params end + + delegate url, body, query, json, @param_container end +