rack-http_router 0.0.31 → 0.0.33
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/rack-http_router/action.rb +34 -12
- data/lib/rack-http_router/router.rb +17 -12
- data/lib/rack-http_router.rb +4 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0c64f3cfa73ac4138ffda97860742253fe931eb53de45281627a58def572c54
|
4
|
+
data.tar.gz: 369489457adc954e84ffb7fd8c30eb9cfdea5ec3e44e64eda8a310df04779928
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 796e12240f741f5b5f771a0b97b433a79f1c2d28a0d72d6db27470d78f22ddcbcb4a20207665912af65ab6351ffd1e7b26c2901366c7553a2b8842e0bb82d1d9
|
7
|
+
data.tar.gz: 858a3256bbe6f8e2910a5934b7fe71f2d83276e30a0735e769f83f7d1d72aded1d45d3587ca0c6c1e7c9a07c857b92bd6d32ce8d8422cf3a7a05463606969ed5
|
@@ -9,18 +9,22 @@ module Rack
|
|
9
9
|
module Action
|
10
10
|
def self.included(base)
|
11
11
|
base.class_eval do
|
12
|
-
attr_reader :route, :config if self != Rack::HttpRouter
|
12
|
+
attr_reader :route, :config, :db if self != Rack::HttpRouter
|
13
13
|
|
14
|
-
def initialize(route, config)
|
14
|
+
def initialize(route: nil, config: nil)
|
15
15
|
@route = route
|
16
16
|
@config = config
|
17
|
+
@db = config[:db]
|
17
18
|
end
|
18
19
|
|
19
20
|
def view_response(a_path, a_view_params = {}, status: 200)
|
20
21
|
Rack::HttpRouter::Action.view_response(
|
21
22
|
a_path,
|
22
23
|
a_view_params,
|
23
|
-
status: status,
|
24
|
+
status: status,
|
25
|
+
config: config,
|
26
|
+
route: route,
|
27
|
+
db: db
|
24
28
|
)
|
25
29
|
end
|
26
30
|
|
@@ -30,7 +34,11 @@ module Rack
|
|
30
34
|
Rack::HttpRouter::Action.view(
|
31
35
|
a_path,
|
32
36
|
a_view_params,
|
33
|
-
status: status,
|
37
|
+
status: status,
|
38
|
+
config: config,
|
39
|
+
route: route,
|
40
|
+
db: db,
|
41
|
+
response_instance: response_instance
|
34
42
|
)
|
35
43
|
end
|
36
44
|
end
|
@@ -85,12 +93,22 @@ module Rack
|
|
85
93
|
Rack::Response.new(content, status, { 'Content-Type' => 'text/html' })
|
86
94
|
end
|
87
95
|
|
88
|
-
def view_response(
|
96
|
+
def view_response(
|
97
|
+
paths,
|
98
|
+
view_params = {},
|
99
|
+
status: 200,
|
100
|
+
config: {},
|
101
|
+
route: nil,
|
102
|
+
db: nil
|
103
|
+
)
|
89
104
|
view(
|
90
105
|
paths,
|
91
106
|
view_params,
|
92
|
-
status: status,
|
93
|
-
|
107
|
+
status: status,
|
108
|
+
config: config,
|
109
|
+
route: route,
|
110
|
+
db: db,
|
111
|
+
response_instance: true
|
94
112
|
)
|
95
113
|
end
|
96
114
|
|
@@ -98,13 +116,15 @@ module Rack
|
|
98
116
|
paths,
|
99
117
|
view_params = {},
|
100
118
|
status: 200,
|
101
|
-
|
102
|
-
route: nil
|
119
|
+
config: {},
|
120
|
+
route: nil,
|
121
|
+
db: nil,
|
122
|
+
response_instance: false
|
103
123
|
)
|
104
124
|
erb = if paths.is_a?(Array)
|
105
|
-
paths.map { |path| erb("views/#{path}", route, view_params) }.join
|
125
|
+
paths.map { |path| erb("views/#{path}", config, route, db, view_params) }.join
|
106
126
|
else
|
107
|
-
erb("views/#{paths}", route, view_params)
|
127
|
+
erb("views/#{paths}", config, route, db, view_params)
|
108
128
|
end
|
109
129
|
|
110
130
|
if response_instance
|
@@ -142,11 +162,13 @@ module Rack
|
|
142
162
|
)
|
143
163
|
end
|
144
164
|
|
145
|
-
|
165
|
+
# rubocop:disable Lint/UnusedMethodArgument
|
166
|
+
def erb(path, config, route, db, view_params = {})
|
146
167
|
@view = OpenStruct.new(view_params)
|
147
168
|
|
148
169
|
eval(Erubi::Engine.new(::File.read("#{path}.html.erb")).src)
|
149
170
|
end
|
171
|
+
# rubocop:enable Lint/UnusedMethodArgument
|
150
172
|
|
151
173
|
def redirect_response(url)
|
152
174
|
Rack::Response.new(
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
require_relative 'router/route'
|
4
2
|
require_relative 'router/build_request'
|
5
3
|
|
@@ -16,9 +14,10 @@ module Rack
|
|
16
14
|
%w[GET POST DELETE PUT TRACE OPTIONS PATCH].each do |method|
|
17
15
|
@routes[method] = { __instances: [] }
|
18
16
|
end
|
19
|
-
@route =
|
20
|
-
|
21
|
-
|
17
|
+
@route =
|
18
|
+
Hash.new do |_hash, key|
|
19
|
+
raise(UndefinedNamedRoute, "Undefined named route: '#{key}'")
|
20
|
+
end
|
22
21
|
@config = config
|
23
22
|
@scopes = []
|
24
23
|
@error = proc { |_req, e| raise e }
|
@@ -34,11 +33,14 @@ module Rack
|
|
34
33
|
return render_not_found(request_builder.call) if route_instance.nil?
|
35
34
|
|
36
35
|
if route_instance.endpoint.respond_to?(:call)
|
37
|
-
return route_instance.endpoint.call(
|
36
|
+
return route_instance.endpoint.call(
|
37
|
+
request_builder.call(route_instance)
|
38
|
+
)
|
38
39
|
end
|
39
40
|
|
40
41
|
if route_instance.endpoint.include?(Rack::HttpRouter::Action)
|
41
|
-
return route_instance.endpoint.new(@route, @config)
|
42
|
+
return route_instance.endpoint.new(route: @route, config: @config)
|
43
|
+
.call(request_builder.call(route_instance))
|
42
44
|
end
|
43
45
|
|
44
46
|
route_instance.endpoint.new.call(request_builder.call(route_instance))
|
@@ -54,7 +56,9 @@ module Rack
|
|
54
56
|
|
55
57
|
route_instance = Route.new(path_with_scopes, endpoint)
|
56
58
|
|
57
|
-
|
59
|
+
if @scopes.size >= 1
|
60
|
+
return push_to_scope(method.to_s.upcase, route_instance)
|
61
|
+
end
|
58
62
|
|
59
63
|
@routes[method.to_s.upcase][:__instances].push(route_instance)
|
60
64
|
end
|
@@ -78,7 +82,7 @@ module Rack
|
|
78
82
|
private
|
79
83
|
|
80
84
|
def push_to_scope(method, route_instance)
|
81
|
-
scopes_with_slash = @scopes + [
|
85
|
+
scopes_with_slash = @scopes + %i[__instances]
|
82
86
|
push_it(@routes[method], *scopes_with_slash, route_instance)
|
83
87
|
end
|
84
88
|
|
@@ -126,9 +130,10 @@ module Rack
|
|
126
130
|
end
|
127
131
|
|
128
132
|
if tail.empty? || found_scopes == []
|
129
|
-
return @routes[env['REQUEST_METHOD']].dig(
|
130
|
-
|
131
|
-
|
133
|
+
return @routes[env['REQUEST_METHOD']].dig(
|
134
|
+
*(found_scopes << :__instances)
|
135
|
+
)
|
136
|
+
.detect { |route_instance| route_instance.match?(env) }
|
132
137
|
end
|
133
138
|
|
134
139
|
match_route(env, tail, found_scopes)
|
data/lib/rack-http_router.rb
CHANGED
@@ -8,7 +8,6 @@ module Rack
|
|
8
8
|
include Action
|
9
9
|
|
10
10
|
def initialize(config = {})
|
11
|
-
p "oi"
|
12
11
|
@router = Router.new(config)
|
13
12
|
end
|
14
13
|
|
@@ -26,6 +25,10 @@ module Rack
|
|
26
25
|
@router.config
|
27
26
|
end
|
28
27
|
|
28
|
+
def db
|
29
|
+
@router.config[:db]
|
30
|
+
end
|
31
|
+
|
29
32
|
def scope(name, &block)
|
30
33
|
@router.append_scope(name)
|
31
34
|
instance_eval(&block)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-http_router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.33
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrique F. Teixeira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-07-
|
11
|
+
date: 2023-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: erubi
|