rack-http_router 0.0.2 → 0.0.32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rack-http_router/action.rb +4 -2
- data/lib/rack-http_router/router.rb +20 -14
- data/lib/rack-http_router.rb +10 -2
- 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: b8cff48c0ee5cfa89a5de1274be3303220345822527d05a090be207148a8b9b5
|
4
|
+
data.tar.gz: 60aa48e2d132770c406419b713582b0c7f88a5d833fbc48763a29ed8d58989b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26edd2abbaff17cab89906192014d14e151aca4edd6f6cd7f81d8e8e2ed674d129e728ccdd87ff8e7557ba88ae1323084dca434b8fd06643f501a476cf9a46fc
|
7
|
+
data.tar.gz: 6ce7a9c0fd92aaa12e9a7065c28594b20ac9af89634e34cef962b421ae4c702863c917ce1154c55f0854e4e0108ec2bdc52ef40ee6d00d66f923a4406fc60aca
|
@@ -9,10 +9,12 @@ module Rack
|
|
9
9
|
module Action
|
10
10
|
def self.included(base)
|
11
11
|
base.class_eval do
|
12
|
-
attr_reader :route if self != Rack::HttpRouter
|
12
|
+
attr_reader :route, :config, :db if self != Rack::HttpRouter
|
13
13
|
|
14
|
-
def initialize(route)
|
14
|
+
def initialize(route: nil, config: nil)
|
15
15
|
@route = route
|
16
|
+
@config = config
|
17
|
+
@db = config[:db]
|
16
18
|
end
|
17
19
|
|
18
20
|
def view_response(a_path, a_view_params = {}, status: 200)
|
@@ -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
|
|
@@ -9,16 +7,18 @@ module Rack
|
|
9
7
|
class UndefinedNamedRoute < StandardError; end
|
10
8
|
|
11
9
|
attr_writer :not_found
|
12
|
-
attr_reader :route
|
10
|
+
attr_reader :route, :config
|
13
11
|
|
14
|
-
def initialize
|
12
|
+
def initialize(config = {})
|
15
13
|
@routes = {}
|
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
|
21
|
+
@config = config
|
22
22
|
@scopes = []
|
23
23
|
@error = proc { |_req, e| raise e }
|
24
24
|
@not_found = proc { [404, {}, ['Not found']] }
|
@@ -33,11 +33,14 @@ module Rack
|
|
33
33
|
return render_not_found(request_builder.call) if route_instance.nil?
|
34
34
|
|
35
35
|
if route_instance.endpoint.respond_to?(:call)
|
36
|
-
return route_instance.endpoint.call(
|
36
|
+
return route_instance.endpoint.call(
|
37
|
+
request_builder.call(route_instance)
|
38
|
+
)
|
37
39
|
end
|
38
40
|
|
39
41
|
if route_instance.endpoint.include?(Rack::HttpRouter::Action)
|
40
|
-
return route_instance.endpoint.new(@route)
|
42
|
+
return route_instance.endpoint.new(route: @route, config: @config)
|
43
|
+
.call(request_builder.call(route_instance))
|
41
44
|
end
|
42
45
|
|
43
46
|
route_instance.endpoint.new.call(request_builder.call(route_instance))
|
@@ -53,7 +56,9 @@ module Rack
|
|
53
56
|
|
54
57
|
route_instance = Route.new(path_with_scopes, endpoint)
|
55
58
|
|
56
|
-
|
59
|
+
if @scopes.size >= 1
|
60
|
+
return push_to_scope(method.to_s.upcase, route_instance)
|
61
|
+
end
|
57
62
|
|
58
63
|
@routes[method.to_s.upcase][:__instances].push(route_instance)
|
59
64
|
end
|
@@ -77,7 +82,7 @@ module Rack
|
|
77
82
|
private
|
78
83
|
|
79
84
|
def push_to_scope(method, route_instance)
|
80
|
-
scopes_with_slash = @scopes + [
|
85
|
+
scopes_with_slash = @scopes + %i[__instances]
|
81
86
|
push_it(@routes[method], *scopes_with_slash, route_instance)
|
82
87
|
end
|
83
88
|
|
@@ -125,9 +130,10 @@ module Rack
|
|
125
130
|
end
|
126
131
|
|
127
132
|
if tail.empty? || found_scopes == []
|
128
|
-
return @routes[env['REQUEST_METHOD']].dig(
|
129
|
-
|
130
|
-
|
133
|
+
return @routes[env['REQUEST_METHOD']].dig(
|
134
|
+
*(found_scopes << :__instances)
|
135
|
+
)
|
136
|
+
.detect { |route_instance| route_instance.match?(env) }
|
131
137
|
end
|
132
138
|
|
133
139
|
match_route(env, tail, found_scopes)
|
data/lib/rack-http_router.rb
CHANGED
@@ -7,8 +7,8 @@ module Rack
|
|
7
7
|
class HttpRouter
|
8
8
|
include Action
|
9
9
|
|
10
|
-
def initialize(
|
11
|
-
@router =
|
10
|
+
def initialize(config = {})
|
11
|
+
@router = Router.new(config)
|
12
12
|
end
|
13
13
|
|
14
14
|
def call(&block)
|
@@ -21,6 +21,14 @@ module Rack
|
|
21
21
|
@router.route
|
22
22
|
end
|
23
23
|
|
24
|
+
def config
|
25
|
+
@router.config
|
26
|
+
end
|
27
|
+
|
28
|
+
def db
|
29
|
+
@router.config[:db]
|
30
|
+
end
|
31
|
+
|
24
32
|
def scope(name, &block)
|
25
33
|
@router.append_scope(name)
|
26
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.32
|
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-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: erubi
|