rack-http_router 0.0.33 → 0.0.34

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: c0c64f3cfa73ac4138ffda97860742253fe931eb53de45281627a58def572c54
4
- data.tar.gz: 369489457adc954e84ffb7fd8c30eb9cfdea5ec3e44e64eda8a310df04779928
3
+ metadata.gz: 540161f25e3b163ac4fa031715aeb5fa88d08d18fa4e2064f531929a1cc5a71a
4
+ data.tar.gz: 4902e738c689c9e83d4fc8147da221187e86318a699545c6f81052d6d267d93a
5
5
  SHA512:
6
- metadata.gz: 796e12240f741f5b5f771a0b97b433a79f1c2d28a0d72d6db27470d78f22ddcbcb4a20207665912af65ab6351ffd1e7b26c2901366c7553a2b8842e0bb82d1d9
7
- data.tar.gz: 858a3256bbe6f8e2910a5934b7fe71f2d83276e30a0735e769f83f7d1d72aded1d45d3587ca0c6c1e7c9a07c857b92bd6d32ce8d8422cf3a7a05463606969ed5
6
+ metadata.gz: a28d4e0c6173d667f69c11d48472e9c2766398aae894757fc2d8a87b3f3026d4c502c2a2a9d7582659fef5e7f8e31c1a0fe10af3f341b291757e8b2624cf87af
7
+ data.tar.gz: fe5f4cc387c3296b98ba1ec1e832cd8c8bed3ef44a64d25e772dedc5ec3095122f109b0233d1e65477692881e8a0fbfa75f937198812c57a81eee151999f9df0
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erubi'
4
- require 'json'
4
+ require 'oj'
5
5
  require 'rack'
6
6
 
7
7
  module Rack
@@ -122,9 +122,9 @@ module Rack
122
122
  response_instance: false
123
123
  )
124
124
  erb = if paths.is_a?(Array)
125
- paths.map { |path| erb("views/#{path}", config, route, db, view_params) }.join
125
+ paths.map { |path| erb("#{config.dig(:views, :path) || "views"}/#{path}", config, route, db, view_params) }.join
126
126
  else
127
- erb("views/#{paths}", config, route, db, view_params)
127
+ erb("#{config.dig(:views, :path) || "views"}/#{paths}", config, route, db, view_params)
128
128
  end
129
129
 
130
130
  if response_instance
@@ -139,12 +139,12 @@ module Rack
139
139
  end
140
140
 
141
141
  def json(content = {}, status: 200)
142
- [status, { 'Content-Type' => 'application/json' }, [content.to_json]]
142
+ [status, { 'Content-Type' => 'application/json' }, [Oj.dump(content, mode: :compat)]]
143
143
  end
144
144
 
145
145
  def json_response(content = {}, status: 200)
146
146
  Rack::Response.new(
147
- content.to_json,
147
+ Oj.dump(content, mode: :compat),
148
148
  status,
149
149
  { 'Content-Type' => 'application/json' }
150
150
  )
@@ -22,18 +22,20 @@ module Rack
22
22
  def update_request_params(request, route)
23
23
  splitted_request_path = request.path.split('/')
24
24
 
25
- route
26
- .splitted_path
27
- .each
28
- .with_index do |route_word, route_word_position|
29
- if route_word.start_with?(':')
30
- param = splitted_request_path[route_word_position]
31
- param = param.to_i if is_a_integer_string?(param)
32
-
33
- update_request_param(request, route_word, param)
34
- end
25
+ i = 0
26
+
27
+ while i < route.splitted_path.size
28
+ route_word = route.splitted_path[i]
29
+ if route_word.start_with?(':')
30
+ param = splitted_request_path[i]
31
+ param = param.to_i if is_a_integer_string?(param)
32
+
33
+ update_request_param(request, route_word, param)
35
34
  end
36
35
 
36
+ i += 1
37
+ end
38
+
37
39
  request
38
40
  end
39
41
 
@@ -4,14 +4,16 @@ module Rack
4
4
  class HttpRouter
5
5
  class Router
6
6
  class Route
7
- attr_reader :endpoint, :splitted_path, :has_params
7
+ attr_reader :endpoint, :splitted_path, :has_params, :has_befores, :befores
8
8
 
9
- def initialize(path, endpoint)
9
+ def initialize(path, endpoint, befores = [])
10
10
  @path = path
11
11
  @splitted_path = @path.split('/')
12
12
  @endpoint = endpoint
13
13
  @params = fetch_params
14
14
  @has_params = @params != []
15
+ @befores = befores
16
+ @has_befores = befores != []
15
17
  end
16
18
 
17
19
  def match?(env)
@@ -20,6 +20,8 @@ module Rack
20
20
  end
21
21
  @config = config
22
22
  @scopes = []
23
+ @befores = []
24
+ @scopes_befores = {}
23
25
  @error = proc { |_req, e| raise e }
24
26
  @not_found = proc { [404, {}, ['Not found']] }
25
27
  end
@@ -32,29 +34,40 @@ module Rack
32
34
 
33
35
  return render_not_found(request_builder.call) if route_instance.nil?
34
36
 
35
- if route_instance.endpoint.respond_to?(:call)
36
- return route_instance.endpoint.call(
37
- request_builder.call(route_instance)
38
- )
39
- end
37
+ rack_request = request_builder.call(route_instance)
40
38
 
41
- if route_instance.endpoint.include?(Rack::HttpRouter::Action)
42
- return route_instance.endpoint.new(route: @route, config: @config)
43
- .call(request_builder.call(route_instance))
39
+ befores = route_instance.befores
40
+ before_result = nil
41
+ i = 0
42
+ while i < befores.size
43
+ before_result = call_endpoint(befores[i], rack_request)
44
+ return before_result unless before_result.is_a?(Rack::Request)
45
+
46
+ i += 1
44
47
  end
45
48
 
46
- route_instance.endpoint.new.call(request_builder.call(route_instance))
49
+ call_endpoint(route_instance.endpoint, before_result || rack_request)
47
50
  rescue Exception => e
48
51
  @error.call(request_builder.call, e)
49
52
  end
50
53
 
54
+ def call_endpoint(endpoint, rack_request)
55
+ return endpoint.call(rack_request) if endpoint.respond_to?(:call)
56
+
57
+ if endpoint.include?(Rack::HttpRouter::Action)
58
+ return endpoint.new(route: @route, config: @config).call(rack_request)
59
+ end
60
+
61
+ endpoint.new.call(rack_request)
62
+ end
63
+
51
64
  def add(method, path, endpoint, as = nil)
52
65
  method = :get if method == :head
53
66
 
54
67
  path_with_scopes = "/#{@scopes.join('/')}#{put_path_slash(path)}"
55
68
  @route[as] = path_with_scopes if as
56
69
 
57
- route_instance = Route.new(path_with_scopes, endpoint)
70
+ route_instance = Route.new(path_with_scopes, endpoint, @befores)
58
71
 
59
72
  if @scopes.size >= 1
60
73
  return push_to_scope(method.to_s.upcase, route_instance)
@@ -71,11 +84,15 @@ module Rack
71
84
  @error = endpoint
72
85
  end
73
86
 
74
- def append_scope(name)
87
+ def append_scope(name, befores = [])
75
88
  @scopes.push(name)
89
+ befores = [befores] unless befores.is_a?(Array)
90
+ @befores.concat(befores)
91
+ @scopes_befores[name] = befores
76
92
  end
77
93
 
78
94
  def clear_last_scope
95
+ @befores -= @scopes_befores[@scopes.last]
79
96
  @scopes = @scopes.first(@scopes.size - 1)
80
97
  end
81
98
 
@@ -133,7 +150,7 @@ module Rack
133
150
  return @routes[env['REQUEST_METHOD']].dig(
134
151
  *(found_scopes << :__instances)
135
152
  )
136
- .detect { |route_instance| route_instance.match?(env) }
153
+ &.detect { |route_instance| route_instance.match?(env) }
137
154
  end
138
155
 
139
156
  match_route(env, tail, found_scopes)
@@ -29,8 +29,8 @@ module Rack
29
29
  @router.config[:db]
30
30
  end
31
31
 
32
- def scope(name, &block)
33
- @router.append_scope(name)
32
+ def scope(name, before: [], &block)
33
+ @router.append_scope(name, before)
34
34
  instance_eval(&block)
35
35
 
36
36
  @router.clear_last_scope
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.33
4
+ version: 0.0.34
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-28 00:00:00.000000000 Z
11
+ date: 2023-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubi
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.15'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.15'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rack
29
43
  requirement: !ruby/object:Gem::Requirement