rackr 0.0.41 → 0.0.43

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c92b9939e63fb6f89dde70e441527279567e58ee6a9a2a490d195b093e083e0
4
- data.tar.gz: c9f0f1ab708b742d319de83e314b2f37dbe6ab0eafa85496cb3ff2ac8f333466
3
+ metadata.gz: 2e2cc704247949346f6cfc0fde345dc102320dafaf55b37f955aadb39b45f076
4
+ data.tar.gz: ca4d5ae5a2b41ca2f543962bb9464fd1fdaec34454a681829590d12817832c8f
5
5
  SHA512:
6
- metadata.gz: b637fb24fc243de2461370122a0228e6228537f62ce658fbaddeb12a9d30974795d33adf48029a380e4ed26fadcc8c05f4b60a6f59e855c32452af2bcf23ac3a
7
- data.tar.gz: c3e7397002bf65a165459ebcae8cc5d5976843b009f88d20e6596fa0787408ff18391de071e2d538323f92cd505478fdba37e4536e0cecb4abf48550859b70a9
6
+ metadata.gz: 106da24676d33cd7121ea8408cdb4a3231c44228f7f0d3e97a41114c7896da234d70411f29c3e71c3c79b54425fe8504747cc67deea37d96497364f90a1b410d
7
+ data.tar.gz: f857873f017b893c49d552115cab7535b08debde99d530cf50a1b48cf50ae81cef7b212560b54559c2564c2e11b43e01e2692120aa5097bf75dde07618bd883c
data/lib/rackr/action.rb CHANGED
@@ -116,7 +116,7 @@ class Rackr
116
116
  db: nil,
117
117
  response_instance: false
118
118
  )
119
- base_path = config.dig(:views, :path) || "views"
119
+ base_path = config.dig(:views, :path) || 'views'
120
120
 
121
121
  file_or_nil = lambda do |path|
122
122
  ::File.read(path)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Rackr
2
4
  module Callback
3
5
  def self.included(base)
@@ -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, "as: argument in routes and branches must be a `string` or a `symbol`, got: '#{as}' for '#{path}'")
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, "Callbacks must respond to a `call` method or be a class with a `call` instance method, got: '#{callback.inspect}' for '#{path}'")
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, "Endpoints must respond to a `call` method or be a class with a `call` instance method, got: '#{endpoint.inspect}' for '#{path}'")
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
@@ -3,9 +3,15 @@
3
3
  class Rackr
4
4
  class Router
5
5
  class Route
6
- attr_reader :endpoint, :splitted_path, :has_params, :has_befores, :befores
7
-
8
- def initialize(path, endpoint, befores = [])
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
- call_endpoint(route_instance.endpoint, before_result || rack_request)
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 = nil, route_befores = [])
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(path_with_branches, endpoint, @befores + ensure_array(route_befores))
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 = [], as = nil)
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, rack_request)
111
- return endpoint.call(rack_request) if endpoint.respond_to?(: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(rack_request)
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(rack_request)
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(name, before, as)
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(http_method, path, block, as, before)
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(http_method, path, endpoint, as, before)
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.41
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-09 00:00:00.000000000 Z
11
+ date: 2023-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubi