rackr 0.0.41 → 0.0.44
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/rackr/action.rb +1 -1
- data/lib/rackr/callback.rb +2 -0
- data/lib/rackr/router/errors.rb +8 -3
- data/lib/rackr/router/route.rb +11 -3
- data/lib/rackr/router.rb +41 -14
- data/lib/rackr.rb +28 -7
- 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: 3e5867f9d9dab3c8331510c345df1c57e8fb00628493796b7f6b031646b0d7e3
|
4
|
+
data.tar.gz: 2a390c59f7d5a0405522f4448d94db6f9d0e8c0bee775543d017aec010844af8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8739b713136d2c97394ce64a3e45b385c5df7c4481796575d38f43411974f5e9168e2730e1f955b0608a0c3c045f95154137386a6c66107bcfaf447f335de656
|
7
|
+
data.tar.gz: c13efed3e12031b84129ce74351e710be2a6b36a49a31db2392060a88ef58631821fa4b1be9af7bf27a4e49a8e90edbb38bb5d0265a4a6fc72e3904b11f8c6ce
|
data/lib/rackr/action.rb
CHANGED
data/lib/rackr/callback.rb
CHANGED
data/lib/rackr/router/errors.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Rackr
|
2
4
|
class Router
|
3
5
|
module Errors
|
@@ -25,13 +27,15 @@ class Rackr
|
|
25
27
|
def check_as(as, path)
|
26
28
|
return if as.is_a?(String) || as.is_a?(Symbol) || as.nil?
|
27
29
|
|
28
|
-
raise(InvalidNamedRouteError,
|
30
|
+
raise(InvalidNamedRouteError,
|
31
|
+
"as: argument in routes and branches must be a `string` or a `symbol`, got: '#{as}' for '#{path}'")
|
29
32
|
end
|
30
33
|
|
31
34
|
def check_callbacks(callbacks, path)
|
32
35
|
check = lambda { |callback|
|
33
36
|
unless callback.nil? || callback.respond_to?(:call) || (callback.respond_to?(:new) && callback.instance_methods.include?(:call))
|
34
|
-
raise(InvalidCallbackError,
|
37
|
+
raise(InvalidCallbackError,
|
38
|
+
"Callbacks must respond to a `call` method or be a class with a `call` instance method, got: '#{callback.inspect}' for '#{path}'")
|
35
39
|
end
|
36
40
|
}
|
37
41
|
|
@@ -43,7 +47,8 @@ class Rackr
|
|
43
47
|
return
|
44
48
|
end
|
45
49
|
|
46
|
-
raise(InvalidEndpointError,
|
50
|
+
raise(InvalidEndpointError,
|
51
|
+
"Endpoints must respond to a `call` method or be a class with a `call` instance method, got: '#{endpoint.inspect}' for '#{path}'")
|
47
52
|
end
|
48
53
|
end
|
49
54
|
end
|
data/lib/rackr/router/route.rb
CHANGED
@@ -3,9 +3,15 @@
|
|
3
3
|
class Rackr
|
4
4
|
class Router
|
5
5
|
class Route
|
6
|
-
attr_reader :endpoint,
|
7
|
-
|
8
|
-
|
6
|
+
attr_reader :endpoint,
|
7
|
+
:splitted_path,
|
8
|
+
:has_params,
|
9
|
+
:has_befores,
|
10
|
+
:befores,
|
11
|
+
:has_afters,
|
12
|
+
:afters
|
13
|
+
|
14
|
+
def initialize(path, endpoint, befores: [], afters: [])
|
9
15
|
@path = path
|
10
16
|
@splitted_path = @path.split('/')
|
11
17
|
@endpoint = endpoint
|
@@ -13,6 +19,8 @@ class Rackr
|
|
13
19
|
@has_params = @params != []
|
14
20
|
@befores = befores
|
15
21
|
@has_befores = befores != []
|
22
|
+
@afters = afters
|
23
|
+
@has_afters = afters != []
|
16
24
|
end
|
17
25
|
|
18
26
|
def match?(env)
|
data/lib/rackr/router.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'router/errors'
|
2
4
|
require_relative 'router/route'
|
3
5
|
require_relative 'router/build_request'
|
@@ -7,7 +9,7 @@ class Rackr
|
|
7
9
|
attr_writer :not_found
|
8
10
|
attr_reader :route, :config
|
9
11
|
|
10
|
-
def initialize(config = {})
|
12
|
+
def initialize(config = {}, before: [], after: [])
|
11
13
|
@routes = {}
|
12
14
|
%w[GET POST DELETE PUT TRACE OPTIONS PATCH].each do |method|
|
13
15
|
@routes[method] = { __instances: [] }
|
@@ -18,8 +20,10 @@ class Rackr
|
|
18
20
|
end
|
19
21
|
@config = config
|
20
22
|
@branches = []
|
21
|
-
@befores =
|
23
|
+
@befores = ensure_array(before)
|
22
24
|
@branches_befores = {}
|
25
|
+
@afters = ensure_array(after)
|
26
|
+
@branches_afters = {}
|
23
27
|
@nameds_as = []
|
24
28
|
@branches_named_as = {}
|
25
29
|
@error = proc { |_req, e| raise e }
|
@@ -43,19 +47,33 @@ class Rackr
|
|
43
47
|
before_result = call_endpoint(befores[i], rack_request)
|
44
48
|
return before_result unless before_result.is_a?(Rack::Request)
|
45
49
|
|
50
|
+
rack_request = before_result
|
51
|
+
|
52
|
+
i += 1
|
53
|
+
end
|
54
|
+
|
55
|
+
endpoint_result = call_endpoint(route_instance.endpoint, before_result || rack_request)
|
56
|
+
|
57
|
+
afters = route_instance.afters
|
58
|
+
i = 0
|
59
|
+
while i < afters.size
|
60
|
+
call_endpoint(afters[i], endpoint_result)
|
46
61
|
i += 1
|
47
62
|
end
|
48
63
|
|
49
|
-
|
64
|
+
endpoint_result
|
65
|
+
rescue Rackr::NotFound
|
66
|
+
render_not_found(request_builder.call)
|
50
67
|
rescue Exception => e
|
51
68
|
@error.call(request_builder.call, e)
|
52
69
|
end
|
53
70
|
|
54
|
-
def add(method, path, endpoint, as
|
71
|
+
def add(method, path, endpoint, as: nil, route_befores: [], route_afters: [])
|
55
72
|
Errors.check_path(path)
|
56
73
|
Errors.check_endpoint(endpoint, path)
|
57
74
|
Errors.check_as(as, path)
|
58
75
|
Errors.check_callbacks(route_befores, path)
|
76
|
+
Errors.check_callbacks(route_afters, path)
|
59
77
|
|
60
78
|
method = :get if method == :head
|
61
79
|
|
@@ -63,11 +81,14 @@ class Rackr
|
|
63
81
|
add_named_route(path_with_branches, as)
|
64
82
|
|
65
83
|
route_instance =
|
66
|
-
Route.new(
|
84
|
+
Route.new(
|
85
|
+
path_with_branches,
|
86
|
+
endpoint,
|
87
|
+
befores: @befores + ensure_array(route_befores),
|
88
|
+
afters: @afters + ensure_array(route_afters)
|
89
|
+
)
|
67
90
|
|
68
|
-
if @branches.size >= 1
|
69
|
-
return push_to_branch(method.to_s.upcase, route_instance)
|
70
|
-
end
|
91
|
+
return push_to_branch(method.to_s.upcase, route_instance) if @branches.size >= 1
|
71
92
|
|
72
93
|
@routes[method.to_s.upcase][:__instances].push(route_instance)
|
73
94
|
end
|
@@ -84,10 +105,11 @@ class Rackr
|
|
84
105
|
@error = endpoint
|
85
106
|
end
|
86
107
|
|
87
|
-
def append_branch(name, branch_befores
|
108
|
+
def append_branch(name, branch_befores: [], branch_afters: [], as: nil)
|
88
109
|
Errors.check_branch_name(name)
|
89
110
|
Errors.check_as(as, @branches.join('/'))
|
90
111
|
Errors.check_callbacks(branch_befores, name)
|
112
|
+
Errors.check_callbacks(branch_afters, name)
|
91
113
|
|
92
114
|
@branches.push(name)
|
93
115
|
|
@@ -95,26 +117,31 @@ class Rackr
|
|
95
117
|
@befores.concat(branch_befores)
|
96
118
|
@branches_befores[name] = branch_befores
|
97
119
|
|
120
|
+
branch_afters = ensure_array(branch_afters)
|
121
|
+
@afters.concat(branch_afters)
|
122
|
+
@branches_afters[name] = branch_afters
|
123
|
+
|
98
124
|
@nameds_as.push(as)
|
99
125
|
@branches_named_as[name] = as
|
100
126
|
end
|
101
127
|
|
102
128
|
def clear_last_branch
|
103
129
|
@befores -= @branches_befores[@branches.last]
|
130
|
+
@afters -= @branches_afters[@branches.last]
|
104
131
|
@nameds_as -= [@branches_named_as[@branches.last]]
|
105
132
|
@branches = @branches.first(@branches.size - 1)
|
106
133
|
end
|
107
134
|
|
108
135
|
private
|
109
136
|
|
110
|
-
def call_endpoint(endpoint,
|
111
|
-
return endpoint.call(
|
137
|
+
def call_endpoint(endpoint, content)
|
138
|
+
return endpoint.call(content) if endpoint.respond_to?(:call)
|
112
139
|
|
113
|
-
if endpoint.include?(Rackr::Action)
|
114
|
-
return endpoint.new(route: @route, config: @config).call(
|
140
|
+
if endpoint.include?(Rackr::Action) || endpoint.include?(Rackr::Callback)
|
141
|
+
return endpoint.new(route: @route, config: @config).call(content)
|
115
142
|
end
|
116
143
|
|
117
|
-
endpoint.new.call(
|
144
|
+
endpoint.new.call(content)
|
118
145
|
end
|
119
146
|
|
120
147
|
def ensure_array(list)
|
data/lib/rackr.rb
CHANGED
@@ -5,10 +5,12 @@ require_relative 'rackr/action'
|
|
5
5
|
require_relative 'rackr/callback'
|
6
6
|
|
7
7
|
class Rackr
|
8
|
+
class NotFound < StandardError; end
|
9
|
+
|
8
10
|
include Action
|
9
11
|
|
10
|
-
def initialize(config = {})
|
11
|
-
@router = Router.new(config)
|
12
|
+
def initialize(config = {}, before: [], after: [])
|
13
|
+
@router = Router.new(config, before: before, after: after)
|
12
14
|
end
|
13
15
|
|
14
16
|
def call(&block)
|
@@ -29,8 +31,13 @@ class Rackr
|
|
29
31
|
@router.config[:db]
|
30
32
|
end
|
31
33
|
|
32
|
-
def r(name, before: [], as: nil, &block)
|
33
|
-
@router.append_branch(
|
34
|
+
def r(name, before: [], after: [], as: nil, &block)
|
35
|
+
@router.append_branch(
|
36
|
+
name,
|
37
|
+
branch_befores: before,
|
38
|
+
branch_afters: after,
|
39
|
+
as: as
|
40
|
+
)
|
34
41
|
instance_eval(&block)
|
35
42
|
|
36
43
|
@router.clear_last_branch
|
@@ -53,11 +60,25 @@ class Rackr
|
|
53
60
|
end
|
54
61
|
|
55
62
|
%w[GET POST DELETE PUT TRACE OPTIONS PATCH].each do |http_method|
|
56
|
-
define_method(http_method.downcase.to_sym) do |path = '', endpoint = -> {}, as: nil, before: nil, &block|
|
63
|
+
define_method(http_method.downcase.to_sym) do |path = '', endpoint = -> {}, as: nil, before: nil, after: nil, &block|
|
57
64
|
if block.respond_to?(:call)
|
58
|
-
@router.add(
|
65
|
+
@router.add(
|
66
|
+
http_method,
|
67
|
+
path,
|
68
|
+
block,
|
69
|
+
as: as,
|
70
|
+
route_befores: before,
|
71
|
+
route_afters: after
|
72
|
+
)
|
59
73
|
else
|
60
|
-
@router.add(
|
74
|
+
@router.add(
|
75
|
+
http_method,
|
76
|
+
path,
|
77
|
+
endpoint,
|
78
|
+
as: as,
|
79
|
+
route_befores: before,
|
80
|
+
route_afters: after
|
81
|
+
)
|
61
82
|
end
|
62
83
|
end
|
63
84
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rackr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.44
|
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-08-
|
11
|
+
date: 2023-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: erubi
|