Skip to content

Commit e91160f

Browse files
create body struct
1 parent 51e39a6 commit e91160f

File tree

9 files changed

+81
-22
lines changed

9 files changed

+81
-22
lines changed

src/abstracts/controller.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ module Shivneri
1111
return @context.as(RequestHandler).route_match_info.params
1212
end
1313

14-
macro get_tuple_from_body(value)
15-
{{value}}.get_tuple_from_hash_json_any.call(@context.as(RequestHandler).body)
16-
end
14+
# macro get_tuple_from_body(value)
15+
# {{value}}.get_tuple_from_hash_json_any.call(@context.as(RequestHandler).body)
16+
# end
1717
end
1818
end
1919
end

src/abstracts/guard.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ module Shivneri
99
return @context.as(RequestHandler).route_match_info.params
1010
end
1111

12-
macro get_tuple_from_body(value)
13-
{{value}}.get_tuple_from_hash_json_any.call(@context.as(RequestHandler).body)
14-
end
12+
# macro get_tuple_from_body(value)
13+
# {{value}}.get_tuple_from_hash_json_any.call(@context.as(RequestHandler).body)
14+
# end
1515

1616
abstract def check(*args)
1717
end

src/handlers/post_handler.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ module Shivneri
55
class PostHandler < ControllerResultHandler
66
getter body, file
77

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

1010
@file : FileManager = FileManager.new
1111

1212
private def parse_multi_part_data
1313
HTTP::FormData.parse(@request) do |part|
1414
case part.headers["Content-Type"]
1515
when MIME_TYPE["json"]
16-
@body.merge!(JSON.parse(part.body).as_h)
16+
@body.merge(JSON.parse(part.body).as_h)
1717
when MIME_TYPE["form_url_encoded"]
1818
HTTP::Params.parse(part.body.gets_to_end).each do |key, val|
1919
@body[key] = JSON::Any.new(val)
@@ -44,7 +44,7 @@ module Shivneri
4444
else
4545
case contentType
4646
when MIME_TYPE["json"]
47-
@body = JSON.parse(@request.body.as(IO)).as_h
47+
@body.body_data = JSON.parse(@request.body.as(IO)).as_h
4848
when MIME_TYPE["form_url_encoded"]
4949
HTTP::Params.parse(@request.body.as(IO).gets_to_end).each do |key, val|
5050
@body[key] = JSON::Any.new(val)

src/model/body.cr

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require "json"
2+
3+
module Shivneri
4+
module MODEL
5+
class Body
6+
JSON.mapping(
7+
body_data: Hash(String, JSON::Any)
8+
)
9+
setter body_data
10+
11+
@body_data : Hash(String, JSON::Any)
12+
13+
def initialize(@body_data)
14+
end
15+
16+
def to_tuple(value)
17+
return convert_to_tuple(value)
18+
end
19+
20+
macro convert_to_tuple(value)
21+
return {{value}}.get_tuple_from_hash_json_any.call(@body_data)
22+
end
23+
24+
def as_hash
25+
return @body_data
26+
end
27+
28+
def [](key : String)
29+
return @body_data[key]
30+
end
31+
32+
def []=(key : String, value)
33+
@body_data[key] = value
34+
end
35+
36+
def merge(hash)
37+
@body_data.merge!(hash)
38+
end
39+
40+
def has_key(key : String)
41+
@body_data.has_key?(key)
42+
end
43+
44+
def has_key?(key : String)
45+
@body_data.has_key?(key)
46+
end
47+
48+
# def to_json
49+
# return @body_data.to_json
50+
# end
51+
52+
# def to_json(value : JSON::Builder)
53+
# return @body_data.to_json
54+
# end
55+
end
56+
end
57+
end

src/model/index.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ require "./http_file"
99
require "./view_engine_data"
1010
require "./view_read_option"
1111
require "./fort"
12+
require "./body"

tests/general/e2e/controller/home_controller_spec.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe "HomeController" do
101101
"Content-Type" => "application/json",
102102
}, nil)
103103
response.status_code.should eq 200
104-
response.body.should eq "{}"
104+
response.body.should eq "{\"body_data\":{}}"
105105
end
106106

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

116116
it "/post with some body" do
@@ -121,7 +121,7 @@ describe "HomeController" do
121121
key: "hello",
122122
}.to_json)
123123
response.status_code.should eq 200
124-
response.body.should eq "{\"key\":\"hello\"}"
124+
response.body.should eq "{\"body_data\":{\"key\":\"hello\"}}"
125125
end
126126

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

137137
it "/post with http:get" do

tests/general/e2e/controller/random_controller_spec.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ describe "UserController" do
4343
"content-type" => "application/x-www-form-urlencoded",
4444
}, HTTP::Params.encode({"hello" => "world"}))
4545
response.status_code.should eq 200
46-
response.body.should eq "{\"body\":{\"hello\":\"world\"},\"query\":{}}"
46+
response.body.should eq "{\"body\":{\"body_data\":{\"hello\":\"world\"}},\"query\":{}}"
4747
end
4848

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

5555
it "/error" do

tests/general/src/controllers/user_controller.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module General
2-
# @[Shields(AuthenticationShield)]
2+
@[Shields(AuthenticationShield)]
33
class UserController < Controller
44
@[Worker("GET")]
55
@[Route("/")]
@@ -13,7 +13,7 @@ module General
1313
@[Guards(UserValidator)]
1414
# @[Inject(as_body(NamedTuple(name: String)))]
1515
def add_user
16-
user = get_tuple_from_body(NamedTuple(id: Int32, name: String))
16+
user = body.to_tuple(NamedTuple(id: Int32, name: String)) # get_tuple_from_body(NamedTuple(id: Int32, name: String))
1717
puts typeof(user)
1818
puts user.to_json
1919
return text_result("ok")

tests/general/src/guards/user_validator.cr

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ module General
2020
end
2121

2222
def validate
23-
user = get_tuple_from_body(
24-
NamedTuple(id: Int32, name: String, password: String, gender: String, email: String, address: String)
25-
)
23+
# user = get_tuple_from_body(
24+
# NamedTuple(id: Int32, name: String, password: String, gender: String, email: String, address: String)
25+
# )
26+
user = body.to_tuple(NamedTuple(id: Int32, name: String, password: String, gender: String, email: String, address: String))
2627
if (user[:name].size < 5)
2728
return "name should be minimum 5 characters"
2829
elsif (user[:password].size < 5)
2930
return "password should be minimum 5 characters"
3031
elsif (["male", "female"].includes? user[:gender])
3132
return "gender should be either male or female"
32-
elsif (/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match user[:email] == nil)
33-
return "email not valid"
33+
# elsif (/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match user[:email] == nil)
34+
# return "email not valid"
3435
elsif (user[:address].size < 10 || user[:address].size > 10)
3536
return "address length should be between 10 & 100"
3637
end

0 commit comments

Comments
 (0)