rackr 0.0.45 → 0.0.46

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: b44fd42881233909b8fc9239a9d235c53644bc12bc40c34cf94baf66d73d2f9d
4
- data.tar.gz: 7be5769d84a9cb8e7163da17c694481a1412ff9a26d9063fc4894fa647cb291d
3
+ metadata.gz: a1bf9fdde553e48bee0e503f8a570d877b026d35c5dc6747aaa4c96dec9f692e
4
+ data.tar.gz: 046d0e28e5f71f518a109402b93c2215e75355c85365d44c6b6e148ab92eae06
5
5
  SHA512:
6
- metadata.gz: 5d5e78dcd9c0650ccf13d53ba4c3975c128b0f3b1143d0765fd2788a390f9daed89824ab6a70a892a6d9388ec00a44ff577407e75d1b517d505f664037148dab
7
- data.tar.gz: 5371f8525a097bd4cc86b6ffcd0be75baf999a13488412eb66c13a8e7e599b65f4b633fb0df04ce2a863c1f787e9ce4b11eaa618b8e3d83e6256fca826b91e61
6
+ metadata.gz: 0141e4fb96aecd7c62e07a7161c60ac06520ac2259f92629de25858f9d4ab5ba1e3b14666394aa7aca772be1cef74a5a0d8b58afd7c2870f773d6f8e2c69f71f
7
+ data.tar.gz: bc9cccbdcab47f5cca73bff2e081b6f1e22fe08731331e0e75d33df11a9639df71abf1a20a62ee1c2cfb3e006de1a1a8db24560f9f14137eeecd6e0f50cb6805
data/lib/rackr/action.rb CHANGED
@@ -8,10 +8,10 @@ class Rackr
8
8
  module Action
9
9
  def self.included(base)
10
10
  base.class_eval do
11
- attr_reader :route, :config, :db if self != Rackr
11
+ attr_reader :routes, :config, :db if self != Rackr
12
12
 
13
- def initialize(route: nil, config: nil)
14
- @route = route
13
+ def initialize(routes: nil, config: nil)
14
+ @routes = routes
15
15
  @config = config
16
16
  @db = config[:db]
17
17
  end
@@ -22,7 +22,7 @@ class Rackr
22
22
  a_view_params,
23
23
  status: status,
24
24
  config: config,
25
- route: route,
25
+ routes: routes,
26
26
  db: db
27
27
  )
28
28
  end
@@ -35,7 +35,7 @@ class Rackr
35
35
  a_view_params,
36
36
  status: status,
37
37
  config: config,
38
- route: route,
38
+ routes: routes,
39
39
  db: db,
40
40
  response_instance: response_instance
41
41
  )
@@ -93,7 +93,7 @@ class Rackr
93
93
  view_params = {},
94
94
  status: 200,
95
95
  config: {},
96
- route: nil,
96
+ routes: nil,
97
97
  db: nil
98
98
  )
99
99
  view(
@@ -101,7 +101,7 @@ class Rackr
101
101
  view_params,
102
102
  status: status,
103
103
  config: config,
104
- route: route,
104
+ routes: routes,
105
105
  db: db,
106
106
  response_instance: true
107
107
  )
@@ -112,7 +112,7 @@ class Rackr
112
112
  view_params = {},
113
113
  status: 200,
114
114
  config: {},
115
- route: nil,
115
+ routes: nil,
116
116
  db: nil,
117
117
  response_instance: false
118
118
  )
@@ -138,7 +138,7 @@ class Rackr
138
138
  ].join,
139
139
  view_params,
140
140
  config: config,
141
- route: route,
141
+ routes: routes,
142
142
  db: db
143
143
  )
144
144
 
@@ -194,7 +194,7 @@ class Rackr
194
194
  end
195
195
 
196
196
  # rubocop:disable Lint/UnusedMethodArgument
197
- def erb(content, view_params = {}, config: nil, route: nil, db: nil)
197
+ def erb(content, view_params = {}, config: nil, routes: nil, db: nil)
198
198
  @view = OpenStruct.new(view_params)
199
199
 
200
200
  eval(Erubi::Engine.new(content).src)
@@ -3,8 +3,9 @@
3
3
  class Rackr
4
4
  class Router
5
5
  class BuildRequest
6
- def initialize(env)
6
+ def initialize(env, spplited_request_path)
7
7
  @env = env
8
+ @spplited_request_path = spplited_request_path
8
9
  end
9
10
 
10
11
  def call(route = nil)
@@ -19,14 +20,12 @@ class Rackr
19
20
  private
20
21
 
21
22
  def update_request_params(request, route)
22
- splitted_request_path = request.path.split('/')
23
-
24
23
  i = 0
25
24
 
26
25
  while i < route.splitted_path.size
27
26
  route_word = route.splitted_path[i]
28
27
  if route_word.start_with?(':')
29
- param = splitted_request_path[i]
28
+ param = @spplited_request_path[i]
30
29
  param = param.to_i if is_a_integer_string?(param)
31
30
 
32
31
  update_request_param(request, route_word, param)
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  class Rackr
4
2
  class Router
5
3
  class Route
@@ -21,10 +19,11 @@ class Rackr
21
19
  @has_befores = befores != []
22
20
  @afters = afters
23
21
  @has_afters = afters != []
22
+ @path_regex = /\A#{path.gsub(/(:\w+)/, '([^/]+)')}\z/
24
23
  end
25
24
 
26
25
  def match?(env)
27
- return match_with_params?(env) if @has_params
26
+ return env['PATH_INFO'].match?(@path_regex) if @has_params
28
27
 
29
28
  env['PATH_INFO'] == @path
30
29
  end
@@ -34,25 +33,6 @@ class Rackr
34
33
  def fetch_params
35
34
  @splitted_path.select { |value| value.start_with? ':' }
36
35
  end
37
-
38
- def match_with_params?(env)
39
- splitted_request_path = env['PATH_INFO'].split('/')
40
-
41
- return false if @splitted_path.size != splitted_request_path.size
42
-
43
- matched_path_pieces =
44
- @splitted_path
45
- .map
46
- .with_index do |segment, i|
47
- if segment.start_with?(':')
48
- true
49
- else
50
- splitted_request_path[i] == segment
51
- end
52
- end
53
-
54
- !matched_path_pieces.include?(false)
55
- end
56
36
  end
57
37
  end
58
38
  end
data/lib/rackr/router.rb CHANGED
@@ -7,14 +7,14 @@ require_relative 'router/build_request'
7
7
  class Rackr
8
8
  class Router
9
9
  attr_writer :not_found
10
- attr_reader :route, :config
10
+ attr_reader :routes, :config
11
11
 
12
12
  def initialize(config = {}, before: [], after: [])
13
- @routes = {}
13
+ @instance_routes = {}
14
14
  %w[GET POST DELETE PUT TRACE OPTIONS PATCH].each do |method|
15
- @routes[method] = { __instances: [] }
15
+ @instance_routes[method] = { __instances: [] }
16
16
  end
17
- @route =
17
+ @routes =
18
18
  Hash.new do |_hash, key|
19
19
  raise(Errors::UndefinedNamedRouteError, "Undefined named route: '#{key}'")
20
20
  end
@@ -28,10 +28,13 @@ class Rackr
28
28
  @branches_named_as = {}
29
29
  @error = proc { |_req, e| raise e }
30
30
  @not_found = proc { [404, {}, ['Not found']] }
31
+ @splitted_request_path = []
31
32
  end
32
33
 
33
34
  def call(env)
34
- request_builder = BuildRequest.new(env)
35
+ @splitted_request_path = env['PATH_INFO'].split('/')
36
+
37
+ request_builder = BuildRequest.new(env, @splitted_request_path)
35
38
  env['REQUEST_METHOD'] = 'GET' if env['REQUEST_METHOD'] == 'HEAD'
36
39
 
37
40
  route_instance = match_route(env)
@@ -90,7 +93,7 @@ class Rackr
90
93
 
91
94
  return push_to_branch(method.to_s.upcase, route_instance) if @branches.size >= 1
92
95
 
93
- @routes[method.to_s.upcase][:__instances].push(route_instance)
96
+ @instance_routes[method.to_s.upcase][:__instances].push(route_instance)
94
97
  end
95
98
 
96
99
  def add_not_found(endpoint)
@@ -111,6 +114,8 @@ class Rackr
111
114
  Errors.check_callbacks(branch_befores, name)
112
115
  Errors.check_callbacks(branch_afters, name)
113
116
 
117
+ name = ":#{name}" if name.is_a? Symbol
118
+
114
119
  @branches.push(name)
115
120
 
116
121
  branch_befores = ensure_array(branch_befores)
@@ -138,7 +143,7 @@ class Rackr
138
143
  return endpoint.call(content) if endpoint.respond_to?(:call)
139
144
 
140
145
  if endpoint.include?(Rackr::Action) || endpoint.include?(Rackr::Callback)
141
- return endpoint.new(route: @route, config: @config).call(content)
146
+ return endpoint.new(routes: @routes, config: @config).call(content)
142
147
  end
143
148
 
144
149
  endpoint.new.call(content)
@@ -155,12 +160,12 @@ class Rackr
155
160
  nameds_as = [@nameds_as.last].push(as).compact
156
161
  return if nameds_as.empty?
157
162
 
158
- @route[nameds_as.join('_').to_sym] = path_with_branches
163
+ @routes[nameds_as.join('_').to_sym] = path_with_branches
159
164
  end
160
165
 
161
166
  def push_to_branch(method, route_instance)
162
167
  branches_with_slash = @branches + %i[__instances]
163
- push_it(@routes[method], *branches_with_slash, route_instance)
168
+ push_it(@instance_routes[method], *branches_with_slash, route_instance)
164
169
  end
165
170
 
166
171
  def push_it(hash, first_key, *rest_keys, val)
@@ -186,18 +191,18 @@ class Rackr
186
191
  end
187
192
 
188
193
  def match_route(env, last_tail = nil, found_branches = [])
189
- routes =
194
+ instance_routes =
190
195
  if last_tail.nil?
191
- last_tail = env['PATH_INFO'].split('/').drop(1)
196
+ last_tail = @splitted_request_path.drop(1)
192
197
 
193
- @routes[env['REQUEST_METHOD']]
198
+ @instance_routes[env['REQUEST_METHOD']]
194
199
  else
195
- @routes[env['REQUEST_METHOD']].dig(*found_branches)
200
+ @instance_routes[env['REQUEST_METHOD']].dig(*found_branches)
196
201
  end
197
202
 
198
203
  segment, *tail = last_tail
199
204
 
200
- routes.each do |branch, _v|
205
+ instance_routes.each do |branch, _v|
201
206
  next if branch == :__instances
202
207
 
203
208
  if segment == branch || branch.start_with?(':')
@@ -207,7 +212,7 @@ class Rackr
207
212
  end
208
213
 
209
214
  if tail.empty? || found_branches == []
210
- return @routes[env['REQUEST_METHOD']].dig(
215
+ return @instance_routes[env['REQUEST_METHOD']].dig(
211
216
  *(found_branches << :__instances)
212
217
  )
213
218
  &.detect { |route_instance| route_instance.match?(env) }
data/lib/rackr.rb CHANGED
@@ -19,8 +19,8 @@ class Rackr
19
19
  @router
20
20
  end
21
21
 
22
- def route
23
- @router.route
22
+ def routes
23
+ @router.routes
24
24
  end
25
25
 
26
26
  def config
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rackr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.45
4
+ version: 0.0.46
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique F. Teixeira