eredor 0.2.0 → 0.2.1
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/lib/eredor/version.rb +1 -1
- data/lib/eredor.rb +22 -36
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 15c1d4fafd9ce4bb8c38fdab4c657392cdb8af9ef2a1179eb1d615948062604a
|
|
4
|
+
data.tar.gz: 2ce2e3ce4769c37e0e41ddd1da9e0bc2222c1a16f29dec4c42cf8a04e870c6e8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cda31701aaae451ce87c7abb7661164e0952a62180d74abe967126fb737127553352a0edfc274f703396eec4f0f97841028d5bd46e715d0227b728d1e5237f30
|
|
7
|
+
data.tar.gz: 4b4f5d92c2025c212fb5026c9c7f9df3b0e7241ecf585d08c882dc4e025808e6fa9877d3de365e6f6b7d8bf1bbd06249b1c62403959f8b96f212982ce1e348bf
|
data/lib/eredor/version.rb
CHANGED
data/lib/eredor.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# frozen_string_literal:
|
|
1
|
+
# frozen_string_literal: false
|
|
2
2
|
|
|
3
3
|
require_relative "eredor/version"
|
|
4
4
|
|
|
@@ -26,18 +26,17 @@ module Eredor
|
|
|
26
26
|
def handle
|
|
27
27
|
method = @request.request_method.to_sym
|
|
28
28
|
path = @request.path
|
|
29
|
-
|
|
30
|
-
query = @request.GET
|
|
29
|
+
params = @request.params
|
|
31
30
|
|
|
32
31
|
@routes[method].each do |route|
|
|
33
32
|
if route[:pattern]
|
|
34
33
|
match = route[:pattern].match(path)
|
|
35
34
|
next unless match
|
|
36
35
|
|
|
37
|
-
params
|
|
38
|
-
return call_handler(route, params
|
|
36
|
+
params.merge!(extract_params(match))
|
|
37
|
+
return call_handler(route, params)
|
|
39
38
|
elsif route[:path] == path
|
|
40
|
-
return call_handler(route,
|
|
39
|
+
return call_handler(route, params)
|
|
41
40
|
end
|
|
42
41
|
end
|
|
43
42
|
|
|
@@ -49,34 +48,23 @@ module Eredor
|
|
|
49
48
|
def register_route(method, path, &block)
|
|
50
49
|
raise(StandardError, "A block must be given") unless block_given?
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
keys << match[1..-1].to_sym
|
|
55
|
-
"(?<#{keys.last}>[^/]+)"
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
route = { path: path, handler: block }
|
|
59
|
-
route[:pattern] = /\A#{regex_path}\z/ if keys.any?
|
|
60
|
-
route[:keys] = keys if keys.any?
|
|
51
|
+
regex_path = path.gsub(/:(\w+)/) { |m| "(?<#{m[1..-1]}>[^/]+)" }
|
|
52
|
+
pattern = /\A#{regex_path}\z/
|
|
61
53
|
|
|
62
|
-
@routes[method] <<
|
|
54
|
+
@routes[method] << {
|
|
55
|
+
path: path,
|
|
56
|
+
pattern: path.include?(":") ? pattern : nil,
|
|
57
|
+
handler: block
|
|
58
|
+
}
|
|
63
59
|
end
|
|
64
60
|
|
|
65
|
-
def extract_params(match
|
|
66
|
-
|
|
67
|
-
h[key] = match[key.to_s]
|
|
68
|
-
end
|
|
61
|
+
def extract_params(match)
|
|
62
|
+
match.named_captures.transform_keys(&:to_sym)
|
|
69
63
|
end
|
|
70
64
|
|
|
71
|
-
def call_handler(route, params
|
|
72
|
-
|
|
73
|
-
params: params,
|
|
74
|
-
body: body,
|
|
75
|
-
query: query
|
|
76
|
-
}
|
|
77
|
-
result = route[:handler].call(env)
|
|
65
|
+
def call_handler(route, params)
|
|
66
|
+
result = route[:handler].call(params)
|
|
78
67
|
|
|
79
|
-
# Permitir retorno flexível
|
|
80
68
|
case result
|
|
81
69
|
when Array || Rack::Response then result # já está no formato [status, headers, body]
|
|
82
70
|
when String then [200, { "content-type" => "text/html" }, [result]]
|
|
@@ -91,11 +79,10 @@ module Eredor
|
|
|
91
79
|
end
|
|
92
80
|
|
|
93
81
|
class BaseController
|
|
94
|
-
attr_reader :params
|
|
82
|
+
attr_reader :params
|
|
95
83
|
|
|
96
|
-
def initialize(
|
|
97
|
-
@params =
|
|
98
|
-
@request = request
|
|
84
|
+
def initialize(params)
|
|
85
|
+
@params = params
|
|
99
86
|
end
|
|
100
87
|
|
|
101
88
|
def render(file)
|
|
@@ -107,13 +94,12 @@ module Eredor
|
|
|
107
94
|
private
|
|
108
95
|
|
|
109
96
|
def get_view(file)
|
|
110
|
-
File.read("
|
|
97
|
+
File.read("./app/views/#{controller_name}/#{file}.html.erb")
|
|
111
98
|
end
|
|
112
99
|
|
|
113
100
|
def controller_name
|
|
114
|
-
controller = self.class.name
|
|
115
|
-
controller.gsub
|
|
116
|
-
controller.downcase!
|
|
101
|
+
controller = self.class.name.to_s
|
|
102
|
+
controller.gsub(/Controller/, "").downcase
|
|
117
103
|
end
|
|
118
104
|
end
|
|
119
105
|
end
|