lennarb 0.1.7 → 0.4.0

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,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Released under the MIT License.
4
- # Copyright, 2023, by Aristóteles Coutinho.
5
-
6
- module Lenna
7
- class Router
8
- # This class is used to cache the routes.
9
- #
10
- # @private
11
- #
12
- class Cache
13
- def initialize = @cache = {}
14
-
15
- # This method is used to generate a key for the cache.
16
- #
17
- # @parameter [String] method
18
- # @parameter [String] path
19
- #
20
- # @return [String]
21
- #
22
- def cache_key(method, path) = "#{method} #{path}"
23
-
24
- # This method is used to add a route to the cache.
25
- #
26
- # @parameter route_key [String] The key for the route.
27
- # @parameter node [Lenna::Route::Node] The node for the route.
28
- #
29
- # @return [Lenna::Route::Node]
30
- #
31
- def add(route_key, node) = @cache[route_key] = node
32
-
33
- # This method is used to get a route from the cache.
34
- #
35
- # @parameter route_key [String] The key for the route.
36
- #
37
- # @return [Lenna::Route::Node]
38
- #
39
- def get(route_key) = @cache[route_key]
40
-
41
- # This method is used to check if a route is in the cache.
42
- #
43
- # @api public
44
- #
45
- # @parameter route_key [String] The key for the route.
46
- #
47
- # @return [Boolean]
48
- #
49
- def exist?(route_key) = @cache.key?(route_key)
50
- end
51
- end
52
- end
@@ -1,77 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Released under the MIT License.
4
- # Copyright, 2023, by Aristóteles Coutinho.
5
-
6
- module Lenna
7
- class Router
8
- # This class is used to manage the namespaces.
9
- #
10
- # @private `Since v0.1.0`
11
- #
12
- class NamespaceStack
13
- # @return [Array] The stack of namespaces
14
- #
15
- # @private
16
- #
17
- attr_reader :stack
18
-
19
- # The initialize method is used to initialize the stack of namespaces.
20
- #
21
- # @private
22
- #
23
- # @attibute stack [Array] The stack of namespaces
24
- #
25
- # @return [void]
26
- #
27
- def initialize = @stack = ['']
28
-
29
- # This method is used to push a prefix to the stack.
30
- #
31
- # @parameter prefix [String] The prefix to be pushed
32
- #
33
- # @return [void]
34
- #
35
- # ex.
36
- #
37
- # stack = NamespaceStack.new
38
- # stack.push('/users')
39
- # stack.current_prefix # => '/users'
40
- #
41
- # @see #resolve_prefix
42
- #
43
- def push(prefix)
44
- @stack.push(resolve_prefix(prefix))
45
- end
46
-
47
- # This method is used to remove the last prefix from the stack.
48
- #
49
- # @return [String] The popped prefix
50
- #
51
- def pop
52
- @stack.pop unless @stack.size == 1
53
- end
54
-
55
- # @return [String] The current prefix
56
- #
57
- def current_prefix = @stack.last
58
-
59
- # The to_s method is used to return the current prefix.
60
- #
61
- # @return [String] The current prefix
62
- #
63
- def to_s = current_prefix
64
-
65
- private
66
-
67
- # The resolve_prefix method is used to resolve the prefix.
68
- #
69
- # @parameter prefix [String] The prefix to be resolved
70
- # @return [String] The resolved prefix
71
- #
72
- def resolve_prefix(prefix)
73
- current_prefix + prefix
74
- end
75
- end
76
- end
77
- end
@@ -1,141 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Released under the MIT License.
4
- # Copyright, 2023, by Aristóteles Coutinho.
5
-
6
- module Lenna
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)
51
-
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
62
-
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) }
77
-
78
- @headers['Content-Type'] = content_type if content_type
79
- @headers
80
- end
81
-
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
87
-
88
- private
89
-
90
- # This method returns the media type.
91
- #
92
- # @return [String] the request media type
93
- #
94
- def media_type = headers['Content-Type']
95
-
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
105
-
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
116
-
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
129
-
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
141
- end