Decouple ParamContainer which makes the code cleaner, and fix kemal.cr

url -- by @sdogruyol
This commit is contained in:
Fatih Kadir Akın 2016-03-06 14:08:59 +02:00
parent 8267ffe2c5
commit e53ba1d9da
2 changed files with 20 additions and 16 deletions

View file

@ -18,7 +18,7 @@ at_exit do
# This route serves the built-in images for not_found and exceptions. # This route serves the built-in images for not_found and exceptions.
get "/__kemal__/:image" do |env| 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) file_path = File.expand_path("libs/kemal/images/#{image}", Dir.current)
if File.exists? file_path if File.exists? file_path
env.response.headers.add "Content-Type", "application/octet-stream" env.response.headers.add "Content-Type", "application/octet-stream"

View file

@ -6,12 +6,16 @@ require "json"
alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type) alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type)
class Kemal::ParamContainer class Kemal::ParamContainer
getter url property url
getter query property query
getter body property body
getter json 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 end
def all def all
@ -24,15 +28,12 @@ class Kemal::ParamParser
APPLICATION_JSON = "application/json" APPLICATION_JSON = "application/json"
def initialize(@request) def initialize(@request)
@url = {} of String => String @param_container = Kemal::ParamContainer.new
@query = {} of String => String
@body = {} of String => String
@json = {} of String => AllParamTypes
end end
def params def params
parse_request parse_request
Kemal::ParamContainer.new(@url, @query, @body, @json) @param_container
end end
def parse_request def parse_request
@ -44,17 +45,17 @@ class Kemal::ParamParser
def parse_body def parse_body
return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil? return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil?
@body = parse_part(@request.body) @param_container.body = parse_part(@request.body)
end end
def parse_query def parse_query
@query = parse_part(@request.query) @param_container.query = parse_part(@request.query)
end end
def parse_url_params def parse_url_params
if params = @request.url_params if params = @request.url_params
params.each do |key, value| params.each do |key, value|
@url[key as String] = value as String @param_container.url[key as String] = value as String
end end
end end
end end
@ -70,10 +71,10 @@ class Kemal::ParamParser
case json = JSON.parse(body).raw case json = JSON.parse(body).raw
when Hash when Hash
json.each do |key, value| json.each do |key, value|
@json[key as String] = value as AllParamTypes @param_container.json[key as String] = value as AllParamTypes
end end
when Array when Array
@json["_json"] = json @param_container.json["_json"] = json
end end
end end
@ -86,4 +87,7 @@ class Kemal::ParamParser
end end
part_params part_params
end end
delegate url, body, query, json, @param_container
end end