Skip to content

Commit

Permalink
create body struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Mar 4, 2020
1 parent 51e39a6 commit e91160f
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/abstracts/controller.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ module Shivneri
return @context.as(RequestHandler).route_match_info.params
end

macro get_tuple_from_body(value)
{{value}}.get_tuple_from_hash_json_any.call(@context.as(RequestHandler).body)
end
# macro get_tuple_from_body(value)
# {{value}}.get_tuple_from_hash_json_any.call(@context.as(RequestHandler).body)
# end
end
end
end
6 changes: 3 additions & 3 deletions src/abstracts/guard.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ module Shivneri
return @context.as(RequestHandler).route_match_info.params
end

macro get_tuple_from_body(value)
{{value}}.get_tuple_from_hash_json_any.call(@context.as(RequestHandler).body)
end
# macro get_tuple_from_body(value)
# {{value}}.get_tuple_from_hash_json_any.call(@context.as(RequestHandler).body)
# end

abstract def check(*args)
end
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/post_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ module Shivneri
class PostHandler < ControllerResultHandler
getter body, file

@body = {} of String => JSON::Any
@body : Body = Body.new({} of String => JSON::Any)

@file : FileManager = FileManager.new

private def parse_multi_part_data
HTTP::FormData.parse(@request) do |part|
case part.headers["Content-Type"]
when MIME_TYPE["json"]
@body.merge!(JSON.parse(part.body).as_h)
@body.merge(JSON.parse(part.body).as_h)
when MIME_TYPE["form_url_encoded"]
HTTP::Params.parse(part.body.gets_to_end).each do |key, val|
@body[key] = JSON::Any.new(val)
Expand Down Expand Up @@ -44,7 +44,7 @@ module Shivneri
else
case contentType
when MIME_TYPE["json"]
@body = JSON.parse(@request.body.as(IO)).as_h
@body.body_data = JSON.parse(@request.body.as(IO)).as_h
when MIME_TYPE["form_url_encoded"]
HTTP::Params.parse(@request.body.as(IO).gets_to_end).each do |key, val|
@body[key] = JSON::Any.new(val)
Expand Down
57 changes: 57 additions & 0 deletions src/model/body.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require "json"

module Shivneri
module MODEL
class Body
JSON.mapping(
body_data: Hash(String, JSON::Any)
)
setter body_data

@body_data : Hash(String, JSON::Any)

def initialize(@body_data)
end

def to_tuple(value)
return convert_to_tuple(value)
end

macro convert_to_tuple(value)
return {{value}}.get_tuple_from_hash_json_any.call(@body_data)
end

def as_hash
return @body_data
end

def [](key : String)
return @body_data[key]
end

def []=(key : String, value)
@body_data[key] = value
end

def merge(hash)
@body_data.merge!(hash)
end

def has_key(key : String)
@body_data.has_key?(key)
end

def has_key?(key : String)
@body_data.has_key?(key)
end

# def to_json
# return @body_data.to_json
# end

# def to_json(value : JSON::Builder)
# return @body_data.to_json
# end
end
end
end
1 change: 1 addition & 0 deletions src/model/index.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ require "./http_file"
require "./view_engine_data"
require "./view_read_option"
require "./fort"
require "./body"
8 changes: 4 additions & 4 deletions tests/general/e2e/controller/home_controller_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe "HomeController" do
"Content-Type" => "application/json",
}, nil)
response.status_code.should eq 200
response.body.should eq "{}"
response.body.should eq "{\"body_data\":{}}"
end

it "/post with empty body" do
Expand All @@ -110,7 +110,7 @@ describe "HomeController" do
"Content-Type" => "application/json",
}, ({} of String => String).to_json)
response.status_code.should eq 200
response.body.should eq "{}"
response.body.should eq "{\"body_data\":{}}"
end

it "/post with some body" do
Expand All @@ -121,7 +121,7 @@ describe "HomeController" do
key: "hello",
}.to_json)
response.status_code.should eq 200
response.body.should eq "{\"key\":\"hello\"}"
response.body.should eq "{\"body_data\":{\"key\":\"hello\"}}"
end

it "/post with xml data" do
Expand All @@ -131,7 +131,7 @@ describe "HomeController" do
}, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<key>hello</key>")
response.status_code.should eq 200
response.body.should eq "{}"
response.body.should eq "{\"body_data\":{}}"
end

it "/post with http:get" do
Expand Down
4 changes: 2 additions & 2 deletions tests/general/e2e/controller/random_controller_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ describe "UserController" do
"content-type" => "application/x-www-form-urlencoded",
}, HTTP::Params.encode({"hello" => "world"}))
response.status_code.should eq 200
response.body.should eq "{\"body\":{\"hello\":\"world\"},\"query\":{}}"
response.body.should eq "{\"body\":{\"body_data\":{\"hello\":\"world\"}},\"query\":{}}"
end

it "/form with get" do
response = http_client.get("/form?" + HTTP::Params.encode({"hello" => "world"}))
response.status_code.should eq 200
response.body.should eq "{\"body\":{},\"query\":{\"hello\":\"world\"}}"
response.body.should eq "{\"body\":{\"body_data\":{}},\"query\":{\"hello\":\"world\"}}"
end

it "/error" do
Expand Down
4 changes: 2 additions & 2 deletions tests/general/src/controllers/user_controller.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module General
# @[Shields(AuthenticationShield)]
@[Shields(AuthenticationShield)]
class UserController < Controller
@[Worker("GET")]
@[Route("/")]
Expand All @@ -13,7 +13,7 @@ module General
@[Guards(UserValidator)]
# @[Inject(as_body(NamedTuple(name: String)))]
def add_user
user = get_tuple_from_body(NamedTuple(id: Int32, name: String))
user = body.to_tuple(NamedTuple(id: Int32, name: String)) # get_tuple_from_body(NamedTuple(id: Int32, name: String))
puts typeof(user)
puts user.to_json
return text_result("ok")
Expand Down
11 changes: 6 additions & 5 deletions tests/general/src/guards/user_validator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ module General
end

def validate
user = get_tuple_from_body(
NamedTuple(id: Int32, name: String, password: String, gender: String, email: String, address: String)
)
# user = get_tuple_from_body(
# NamedTuple(id: Int32, name: String, password: String, gender: String, email: String, address: String)
# )
user = body.to_tuple(NamedTuple(id: Int32, name: String, password: String, gender: String, email: String, address: String))
if (user[:name].size < 5)
return "name should be minimum 5 characters"
elsif (user[:password].size < 5)
return "password should be minimum 5 characters"
elsif (["male", "female"].includes? user[:gender])
return "gender should be either male or female"
elsif (/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match user[:email] == nil)
return "email not valid"
# elsif (/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match user[:email] == nil)
# return "email not valid"
elsif (user[:address].size < 10 || user[:address].size > 10)
return "address length should be between 10 & 100"
end
Expand Down

0 comments on commit e91160f

Please sign in to comment.