rackr 0.0.4 → 0.0.43
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 +56 -0
- data/lib/rackr/router/route.rb +11 -3
- data/lib/rackr/router.rb +39 -14
- data/lib/rackr.rb +26 -7
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e2cc704247949346f6cfc0fde345dc102320dafaf55b37f955aadb39b45f076
|
4
|
+
data.tar.gz: ca4d5ae5a2b41ca2f543962bb9464fd1fdaec34454a681829590d12817832c8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 106da24676d33cd7121ea8408cdb4a3231c44228f7f0d3e97a41114c7896da234d70411f29c3e71c3c79b54425fe8504747cc67deea37d96497364f90a1b410d
|
7
|
+
data.tar.gz: f857873f017b893c49d552115cab7535b08debde99d530cf50a1b48cf50ae81cef7b212560b54559c2564c2e11b43e01e2692120aa5097bf75dde07618bd883c
|
data/lib/rackr/action.rb
CHANGED
data/lib/rackr/callback.rb
CHANGED
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Rackr
|
4
|
+
class Router
|
5
|
+
module Errors
|
6
|
+
class Error < StandardError; end
|
7
|
+
class InvalidNamedRouteError < Error; end
|
8
|
+
class UndefinedNamedRouteError < Error; end
|
9
|
+
class InvalidEndpointError < Error; end
|
10
|
+
class InvalidCallbackError < Error; end
|
11
|
+
class InvalidPathError < Error; end
|
12
|
+
class InvalidBranchNameError < Error; end
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def check_branch_name(path)
|
16
|
+
return if path.is_a?(String) || path.is_a?(Symbol)
|
17
|
+
|
18
|
+
raise(InvalidBranchNameError, "Route branch name must be a `string` or a `symbol`, got: '#{path}'")
|
19
|
+
end
|
20
|
+
|
21
|
+
def check_path(path)
|
22
|
+
return if path.is_a?(String) || path.is_a?(Symbol) || path.nil?
|
23
|
+
|
24
|
+
raise(InvalidPathError, "Path must be a `string`, `symbol` or `nil`, got: '#{path}'")
|
25
|
+
end
|
26
|
+
|
27
|
+
def check_as(as, path)
|
28
|
+
return if as.is_a?(String) || as.is_a?(Symbol) || as.nil?
|
29
|
+
|
30
|
+
raise(InvalidNamedRouteError,
|
31
|
+
"as: argument in routes and branches must be a `string` or a `symbol`, got: '#{as}' for '#{path}'")
|
32
|
+
end
|
33
|
+
|
34
|
+
def check_callbacks(callbacks, path)
|
35
|
+
check = lambda { |callback|
|
36
|
+
unless callback.nil? || callback.respond_to?(:call) || (callback.respond_to?(:new) && callback.instance_methods.include?(:call))
|
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}'")
|
39
|
+
end
|
40
|
+
}
|
41
|
+
|
42
|
+
callbacks.is_a?(Array) ? callbacks.compact.each(&check) : check.call(callbacks)
|
43
|
+
end
|
44
|
+
|
45
|
+
def check_endpoint(endpoint, path)
|
46
|
+
if endpoint.respond_to?(:call) || (endpoint.respond_to?(:new) && endpoint.instance_methods.include?(:call))
|
47
|
+
return
|
48
|
+
end
|
49
|
+
|
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}'")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
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,31 @@ 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
|
50
65
|
rescue Exception => e
|
51
66
|
@error.call(request_builder.call, e)
|
52
67
|
end
|
53
68
|
|
54
|
-
def add(method, path, endpoint, as
|
69
|
+
def add(method, path, endpoint, as: nil, route_befores: [], route_afters: [])
|
55
70
|
Errors.check_path(path)
|
56
71
|
Errors.check_endpoint(endpoint, path)
|
57
72
|
Errors.check_as(as, path)
|
58
73
|
Errors.check_callbacks(route_befores, path)
|
74
|
+
Errors.check_callbacks(route_afters, path)
|
59
75
|
|
60
76
|
method = :get if method == :head
|
61
77
|
|
@@ -63,11 +79,14 @@ class Rackr
|
|
63
79
|
add_named_route(path_with_branches, as)
|
64
80
|
|
65
81
|
route_instance =
|
66
|
-
Route.new(
|
82
|
+
Route.new(
|
83
|
+
path_with_branches,
|
84
|
+
endpoint,
|
85
|
+
befores: @befores + ensure_array(route_befores),
|
86
|
+
afters: @afters + ensure_array(route_afters)
|
87
|
+
)
|
67
88
|
|
68
|
-
if @branches.size >= 1
|
69
|
-
return push_to_branch(method.to_s.upcase, route_instance)
|
70
|
-
end
|
89
|
+
return push_to_branch(method.to_s.upcase, route_instance) if @branches.size >= 1
|
71
90
|
|
72
91
|
@routes[method.to_s.upcase][:__instances].push(route_instance)
|
73
92
|
end
|
@@ -84,10 +103,11 @@ class Rackr
|
|
84
103
|
@error = endpoint
|
85
104
|
end
|
86
105
|
|
87
|
-
def append_branch(name, branch_befores
|
106
|
+
def append_branch(name, branch_befores: [], branch_afters: [], as: nil)
|
88
107
|
Errors.check_branch_name(name)
|
89
108
|
Errors.check_as(as, @branches.join('/'))
|
90
109
|
Errors.check_callbacks(branch_befores, name)
|
110
|
+
Errors.check_callbacks(branch_afters, name)
|
91
111
|
|
92
112
|
@branches.push(name)
|
93
113
|
|
@@ -95,26 +115,31 @@ class Rackr
|
|
95
115
|
@befores.concat(branch_befores)
|
96
116
|
@branches_befores[name] = branch_befores
|
97
117
|
|
118
|
+
branch_afters = ensure_array(branch_afters)
|
119
|
+
@afters.concat(branch_afters)
|
120
|
+
@branches_afters[name] = branch_afters
|
121
|
+
|
98
122
|
@nameds_as.push(as)
|
99
123
|
@branches_named_as[name] = as
|
100
124
|
end
|
101
125
|
|
102
126
|
def clear_last_branch
|
103
127
|
@befores -= @branches_befores[@branches.last]
|
128
|
+
@afters -= @branches_afters[@branches.last]
|
104
129
|
@nameds_as -= [@branches_named_as[@branches.last]]
|
105
130
|
@branches = @branches.first(@branches.size - 1)
|
106
131
|
end
|
107
132
|
|
108
133
|
private
|
109
134
|
|
110
|
-
def call_endpoint(endpoint,
|
111
|
-
return endpoint.call(
|
135
|
+
def call_endpoint(endpoint, content)
|
136
|
+
return endpoint.call(content) if endpoint.respond_to?(:call)
|
112
137
|
|
113
|
-
if endpoint.include?(Rackr::Action)
|
114
|
-
return endpoint.new(route: @route, config: @config).call(
|
138
|
+
if endpoint.include?(Rackr::Action) || endpoint.include?(Rackr::Callback)
|
139
|
+
return endpoint.new(route: @route, config: @config).call(content)
|
115
140
|
end
|
116
141
|
|
117
|
-
endpoint.new.call(
|
142
|
+
endpoint.new.call(content)
|
118
143
|
end
|
119
144
|
|
120
145
|
def ensure_array(list)
|
data/lib/rackr.rb
CHANGED
@@ -7,8 +7,8 @@ require_relative 'rackr/callback'
|
|
7
7
|
class Rackr
|
8
8
|
include Action
|
9
9
|
|
10
|
-
def initialize(config = {})
|
11
|
-
@router = Router.new(config)
|
10
|
+
def initialize(config = {}, before: [], after: [])
|
11
|
+
@router = Router.new(config, before: before, after: after)
|
12
12
|
end
|
13
13
|
|
14
14
|
def call(&block)
|
@@ -29,8 +29,13 @@ class Rackr
|
|
29
29
|
@router.config[:db]
|
30
30
|
end
|
31
31
|
|
32
|
-
def r(name, before: [], as: nil, &block)
|
33
|
-
@router.append_branch(
|
32
|
+
def r(name, before: [], after: [], as: nil, &block)
|
33
|
+
@router.append_branch(
|
34
|
+
name,
|
35
|
+
branch_befores: before,
|
36
|
+
branch_afters: after,
|
37
|
+
as: as
|
38
|
+
)
|
34
39
|
instance_eval(&block)
|
35
40
|
|
36
41
|
@router.clear_last_branch
|
@@ -53,11 +58,25 @@ class Rackr
|
|
53
58
|
end
|
54
59
|
|
55
60
|
%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|
|
61
|
+
define_method(http_method.downcase.to_sym) do |path = '', endpoint = -> {}, as: nil, before: nil, after: nil, &block|
|
57
62
|
if block.respond_to?(:call)
|
58
|
-
@router.add(
|
63
|
+
@router.add(
|
64
|
+
http_method,
|
65
|
+
path,
|
66
|
+
block,
|
67
|
+
as: as,
|
68
|
+
route_befores: before,
|
69
|
+
route_afters: after
|
70
|
+
)
|
59
71
|
else
|
60
|
-
@router.add(
|
72
|
+
@router.add(
|
73
|
+
http_method,
|
74
|
+
path,
|
75
|
+
endpoint,
|
76
|
+
as: as,
|
77
|
+
route_befores: before,
|
78
|
+
route_afters: after
|
79
|
+
)
|
61
80
|
end
|
62
81
|
end
|
63
82
|
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.43
|
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-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: erubi
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- lib/rackr/callback.rb
|
70
70
|
- lib/rackr/router.rb
|
71
71
|
- lib/rackr/router/build_request.rb
|
72
|
+
- lib/rackr/router/errors.rb
|
72
73
|
- lib/rackr/router/route.rb
|
73
74
|
homepage: https://github.com/henrique-ft/rackr
|
74
75
|
licenses:
|