lennarb 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/changelog.md +165 -0
- data/lib/lenna/application.rb +53 -0
- data/lib/lenna/middleware/app.rb +107 -92
- data/lib/lenna/middleware/default/error_handler.rb +184 -179
- data/lib/lenna/middleware/default/logging.rb +79 -81
- data/lib/lenna/router/builder.rb +111 -86
- data/lib/lenna/router/cache.rb +44 -30
- data/lib/lenna/router/namespace_stack.rb +66 -62
- data/lib/lenna/router/request.rb +125 -101
- data/lib/lenna/router/response.rb +505 -375
- data/lib/lenna/router/route_matcher.rb +56 -57
- data/lib/lenna/router.rb +186 -154
- data/lib/lennarb/array_extensions.rb +25 -11
- data/lib/lennarb/version.rb +5 -2
- data/lib/lennarb.rb +8 -1
- data/license.md +21 -0
- data/readme.md +31 -0
- metadata +64 -56
- data/CHANGELOG.md +0 -62
- data/LICENCE +0 -24
- data/README.md +0 -229
- data/lib/lenna/base.rb +0 -52
data/lib/lenna/router/request.rb
CHANGED
@@ -1,117 +1,141 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2023, by Aristóteles Coutinho.
|
5
|
+
|
3
6
|
module Lenna
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
class Router
|
8
|
+
# The Request class is responsible for managing the request.
|
9
|
+
#
|
10
|
+
# @attr headers [Hash] the request headers
|
11
|
+
# @attr body [Hash] the request body
|
12
|
+
# @attr params [Hash] the request params
|
13
|
+
#
|
14
|
+
# @public `Since v0.1.0`
|
15
|
+
#
|
16
|
+
class Request < ::Rack::Request
|
17
|
+
# This method is used to set the request headers.
|
18
|
+
#
|
19
|
+
# @parameter name [String] the header name
|
20
|
+
# @parameter value [String] the header value
|
21
|
+
#
|
22
|
+
# @return [String] the header value
|
23
|
+
#
|
24
|
+
# @public
|
25
|
+
#
|
26
|
+
# ex.
|
27
|
+
# request.put_header('Foo', 'bar')
|
28
|
+
#
|
29
|
+
# request.headers
|
30
|
+
# # => { 'Foo' => 'bar' }
|
31
|
+
#
|
32
|
+
def put_header(name, value) = headers[name] = value
|
11
33
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
34
|
+
# This method is used to set the request params.
|
35
|
+
#
|
36
|
+
# @parameter params [Hash] the request params
|
37
|
+
#
|
38
|
+
# @return [Hash] the request params
|
39
|
+
#
|
40
|
+
# @public
|
41
|
+
#
|
42
|
+
def assign_params(params) = @params = params
|
20
43
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
44
|
+
# This method is used to parse the body params.
|
45
|
+
#
|
46
|
+
# @return [Hash] the request params
|
47
|
+
#
|
48
|
+
# @public
|
49
|
+
#
|
50
|
+
def params = super.merge(parse_body_params)
|
27
51
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
52
|
+
# This method rewinds the body
|
53
|
+
#
|
54
|
+
# @return [String] the request body content
|
55
|
+
#
|
56
|
+
# @public
|
57
|
+
#
|
58
|
+
def body
|
59
|
+
super.rewind
|
60
|
+
super.read
|
61
|
+
end
|
39
62
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
63
|
+
# This method returns the headers in a normalized way.
|
64
|
+
#
|
65
|
+
# @public
|
66
|
+
#
|
67
|
+
# @return [Hash] the request headers
|
68
|
+
#
|
69
|
+
# ex.
|
70
|
+
# Turn this:
|
71
|
+
# HTTP_FOO=bar Foo=bar
|
72
|
+
#
|
73
|
+
def headers
|
74
|
+
content_type = env['CONTENT_TYPE']
|
75
|
+
@headers ||= env.select { |k, _| k.start_with?('HTTP_') }
|
76
|
+
.transform_keys { |k| format_header_name(k) }
|
53
77
|
|
54
|
-
|
55
|
-
|
56
|
-
|
78
|
+
@headers['Content-Type'] = content_type if content_type
|
79
|
+
@headers
|
80
|
+
end
|
57
81
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
def json_body = @json_body ||= parse_body_params
|
82
|
+
# This method returns the request body like a json.
|
83
|
+
#
|
84
|
+
# @return [Hash] the request body
|
85
|
+
#
|
86
|
+
def json_body = @json_body ||= parse_body_params
|
64
87
|
|
65
|
-
|
88
|
+
private
|
66
89
|
|
67
|
-
|
90
|
+
# This method returns the media type.
|
91
|
+
#
|
92
|
+
# @return [String] the request media type
|
93
|
+
#
|
94
|
+
def media_type = headers['Content-Type']
|
68
95
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
96
|
+
# This method parses the json body.
|
97
|
+
#
|
98
|
+
# @return [Hash] the request json body
|
99
|
+
#
|
100
|
+
def parse_json_body
|
101
|
+
@parsed_json_body ||= ::JSON.parse(body)
|
102
|
+
rescue ::JSON::ParserError
|
103
|
+
{}
|
104
|
+
end
|
74
105
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
post_params
|
86
|
-
else
|
87
|
-
{}
|
88
|
-
end
|
89
|
-
end
|
106
|
+
# This method parses the body params.
|
107
|
+
#
|
108
|
+
# @return [Hash] the request body params
|
109
|
+
#
|
110
|
+
def parse_body_params
|
111
|
+
case media_type
|
112
|
+
in 'application/json' then parse_json_body
|
113
|
+
else post_params
|
114
|
+
end
|
115
|
+
end
|
90
116
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
end
|
117
|
+
# This method parses the post params.
|
118
|
+
#
|
119
|
+
# @return [Hash] the request post params
|
120
|
+
#
|
121
|
+
def post_params
|
122
|
+
@post_params ||=
|
123
|
+
if body.empty?
|
124
|
+
{}
|
125
|
+
else
|
126
|
+
::Rack::Utils.parse_nested_query(body)
|
127
|
+
end
|
128
|
+
end
|
104
129
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
end
|
130
|
+
# This method formats the header name.
|
131
|
+
#
|
132
|
+
# @parameter name [String] the header name
|
133
|
+
#
|
134
|
+
# @return [String] the formatted header name
|
135
|
+
#
|
136
|
+
def format_header_name(name)
|
137
|
+
name.sub(/^HTTP_/, '').split('_').map(&:capitalize).join('-')
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
117
141
|
end
|