lennarb 0.1.4 → 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.
@@ -1,107 +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
- class Router
5
- # The Request class is responsible for managing the request.
6
- #
7
- # @attr headers [Hash] the request headers
8
- # @attr body [Hash] the request body
9
- # @attr params [Hash] the request params
10
- class Request < ::Rack::Request
11
- # This method is used to parse the body params.
12
- #
13
- # @return [Hash] the request params
14
- #
15
- # @api public
16
- def params = super.merge(parse_body_params)
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
33
+
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
43
+
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)
17
51
 
18
- # This method rewinds the body
19
- #
20
- # @return [String] the request body content
21
- #
22
- # @api public
23
- #
24
- # @since 0.1.0
25
- def body_content
26
- body.rewind
27
- body.read
28
- 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
29
62
 
30
- # This method returns the headers in a normalized way.
31
- #
32
- # @return [Hash] the request headers
33
- #
34
- # @api public
35
- #
36
- # @example:
37
- # Turn this:
38
- # HTTP_FOO=bar Foo=bar
39
- def headers
40
- content_type = env['CONTENT_TYPE']
41
- @headers ||= env.select { |k, _| k.start_with?('HTTP_') }
42
- .transform_keys { |k| format_header_name(k) }
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) }
43
77
 
44
- @headers['Content-Type'] = content_type if content_type
45
- @headers
46
- end
78
+ @headers['Content-Type'] = content_type if content_type
79
+ @headers
80
+ end
47
81
 
48
- # This method returns the request body in a normalized way.
49
- #
50
- # @return [Hash] the request body
51
- #
52
- # @api public
53
- 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
54
87
 
55
- private
88
+ private
56
89
 
57
- def json_request? = media_type == 'application/json'
90
+ # This method returns the media type.
91
+ #
92
+ # @return [String] the request media type
93
+ #
94
+ def media_type = headers['Content-Type']
58
95
 
59
- def parse_json_body
60
- @parsed_json_body ||= ::JSON.parse(body_content) if json_request?
61
- rescue ::JSON::ParserError
62
- {}
63
- end
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
64
105
 
65
- # This method parses the body params.
66
- #
67
- # @return [Hash] the request body params
68
- #
69
- # @api private
70
- def parse_body_params
71
- case media_type
72
- when 'application/json'
73
- parse_json_body
74
- when 'application/x-www-form-urlencoded', 'multipart/form-data'
75
- post_params
76
- else
77
- {}
78
- end
79
- 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
80
116
 
81
- # This method parses the post params.
82
- #
83
- # @return [Hash] the request post params
84
- #
85
- # @api private
86
- def post_params
87
- @post_params ||=
88
- if body_content.empty?
89
- {}
90
- else
91
- ::Rack::Utils.parse_nested_query(body_content)
92
- end
93
- 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
94
129
 
95
- # This method formats the header name.
96
- #
97
- # @param name [String] the header name
98
- #
99
- # @return [String] the formatted header name
100
- #
101
- # @api private
102
- def format_header_name(name)
103
- name.sub(/^HTTP_/, '').split('_').map(&:capitalize).join('-')
104
- end
105
- end
106
- 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
107
141
  end