rack-http_router 0.0.32 → 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: b8cff48c0ee5cfa89a5de1274be3303220345822527d05a090be207148a8b9b5
4
- data.tar.gz: 60aa48e2d132770c406419b713582b0c7f88a5d833fbc48763a29ed8d58989b0
3
+ metadata.gz: 540161f25e3b163ac4fa031715aeb5fa88d08d18fa4e2064f531929a1cc5a71a
4
+ data.tar.gz: 4902e738c689c9e83d4fc8147da221187e86318a699545c6f81052d6d267d93a
5
5
  SHA512:
6
- metadata.gz: 26edd2abbaff17cab89906192014d14e151aca4edd6f6cd7f81d8e8e2ed674d129e728ccdd87ff8e7557ba88ae1323084dca434b8fd06643f501a476cf9a46fc
7
- data.tar.gz: 6ce7a9c0fd92aaa12e9a7065c28594b20ac9af89634e34cef962b421ae4c702863c917ce1154c55f0854e4e0108ec2bdc52ef40ee6d00d66f923a4406fc60aca
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
@@ -21,7 +21,10 @@ module Rack
21
21
  Rack::HttpRouter::Action.view_response(
22
22
  a_path,
23
23
  a_view_params,
24
- status: status, route: route
24
+ status: status,
25
+ config: config,
26
+ route: route,
27
+ db: db
25
28
  )
26
29
  end
27
30
 
@@ -31,7 +34,11 @@ module Rack
31
34
  Rack::HttpRouter::Action.view(
32
35
  a_path,
33
36
  a_view_params,
34
- status: status, response_instance: response_instance, route: route
37
+ status: status,
38
+ config: config,
39
+ route: route,
40
+ db: db,
41
+ response_instance: response_instance
35
42
  )
36
43
  end
37
44
  end
@@ -86,12 +93,22 @@ module Rack
86
93
  Rack::Response.new(content, status, { 'Content-Type' => 'text/html' })
87
94
  end
88
95
 
89
- def view_response(paths, view_params = {}, status: 200, route: nil)
96
+ def view_response(
97
+ paths,
98
+ view_params = {},
99
+ status: 200,
100
+ config: {},
101
+ route: nil,
102
+ db: nil
103
+ )
90
104
  view(
91
105
  paths,
92
106
  view_params,
93
- status: status, response_instance: true,
94
- route: route
107
+ status: status,
108
+ config: config,
109
+ route: route,
110
+ db: db,
111
+ response_instance: true
95
112
  )
96
113
  end
97
114
 
@@ -99,13 +116,15 @@ module Rack
99
116
  paths,
100
117
  view_params = {},
101
118
  status: 200,
102
- response_instance: false,
103
- route: nil
119
+ config: {},
120
+ route: nil,
121
+ db: nil,
122
+ response_instance: false
104
123
  )
105
124
  erb = if paths.is_a?(Array)
106
- paths.map { |path| erb("views/#{path}", route, view_params) }.join
125
+ paths.map { |path| erb("#{config.dig(:views, :path) || "views"}/#{path}", config, route, db, view_params) }.join
107
126
  else
108
- erb("views/#{paths}", route, view_params)
127
+ erb("#{config.dig(:views, :path) || "views"}/#{paths}", config, route, db, view_params)
109
128
  end
110
129
 
111
130
  if response_instance
@@ -120,12 +139,12 @@ module Rack
120
139
  end
121
140
 
122
141
  def json(content = {}, status: 200)
123
- [status, { 'Content-Type' => 'application/json' }, [content.to_json]]
142
+ [status, { 'Content-Type' => 'application/json' }, [Oj.dump(content, mode: :compat)]]
124
143
  end
125
144
 
126
145
  def json_response(content = {}, status: 200)
127
146
  Rack::Response.new(
128
- content.to_json,
147
+ Oj.dump(content, mode: :compat),
129
148
  status,
130
149
  { 'Content-Type' => 'application/json' }
131
150
  )
@@ -143,11 +162,13 @@ module Rack
143
162
  )
144
163
  end
145
164
 
146
- def erb(path, _route, view_params = {})
165
+ # rubocop:disable Lint/UnusedMethodArgument
166
+ def erb(path, config, route, db, view_params = {})
147
167
  @view = OpenStruct.new(view_params)
148
168
 
149
169
  eval(Erubi::Engine.new(::File.read("#{path}.html.erb")).src)
150
170
  end
171
+ # rubocop:enable Lint/UnusedMethodArgument
151
172
 
152
173
  def redirect_response(url)
153
174
  Rack::Response.new(
@@ -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.32
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-25 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