hanami-router 2.1.0.rc2 → 2.1.0

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: c3207bbac39b5897bd7fa20d6a5e08d6949944be65e0591bccd97f3003e6478f
4
- data.tar.gz: b278f47862cc6be5e1dcb4f93517891a73e7ec1048e4ab2da42f3d41fda3ee9d
3
+ metadata.gz: bd15cdabe88259102767109851f7eb4253c52fbdd8a77fff7e3e53512953866c
4
+ data.tar.gz: ca5c14f2abe932cda82da4ebacfe569e8acdfbce915d33e89201dd3af52fd044
5
5
  SHA512:
6
- metadata.gz: dc876c976c261b4c59741c78d78ac9b5afad73074f934a677d4c6c6189bfa86f567990c2037f3bb200ec738825fe00bb648d6c82b7f1a92adc7559abc331ddb7
7
- data.tar.gz: afe72323c1f8cb7ef12a933d3ea4a8934d80ca9f4ea99b85e2c0049195eea546b20c5677689e2c7bd00936c2bf839308d9556d1c2c99ed981f0ef5e1fc05f5ad
6
+ metadata.gz: 1b8dba8cd1f1a178b84a8222b0f0d671a87c0e8e58332022c4480a850a2b3daf3e1d9a0915299d0ee8249d016610a088c8ee7049d0bb93fbefb25e74586d6dbc
7
+ data.tar.gz: 1eca7bf5c3e7928f9d5400a87fa9d9089f43312f4d89684c5137c082469c31aeea5cd8f910fea9c08cc58a8cc01d2a659feee0a0c62593ccca1ba1854d70d6c8
data/CHANGELOG.md CHANGED
@@ -2,6 +2,19 @@
2
2
 
3
3
  Rack compatible HTTP router for Ruby
4
4
 
5
+ ## v2.1.0 - 2024-02-27
6
+
7
+ ### Fixed
8
+
9
+ - [Pat Allan] Fix PATH_INFO and SCRIPT_NAME for Rack apps mounted at the root (keep the leading slash in PATH_INFO, and set SCRIPT_NAME to a blank string)
10
+ - [Pat Allan] Process glob routes and mounted apps together, so that the routes can be handled in the user-specified order (previously, a root-mounted app would handle routes even if matching globs were declared earlier)
11
+
12
+ ### Changed
13
+
14
+ - [Pat Allan] Pass keyword args through to middleware
15
+
16
+ ## v2.1.0.rc3 - 2024-02-16
17
+
5
18
  ## v2.1.0.rc2 - 2023-11-08
6
19
 
7
20
  ## v2.1.0.rc1 - 2023-11-02
@@ -21,8 +21,8 @@ module Hanami
21
21
  mapping.each do |path, stack|
22
22
  builder = Rack::Builder.new
23
23
 
24
- stack.each do |middleware, args, blk|
25
- builder.use(middleware, *args, &blk)
24
+ stack.each do |middleware, args, kwargs, blk|
25
+ builder.use(middleware, *args, **kwargs, &blk)
26
26
  end
27
27
 
28
28
  builder.run(router)
@@ -23,8 +23,6 @@ module Hanami
23
23
 
24
24
  # Parse a multipart body payload (form file uploading)
25
25
  #
26
- # @param body [String] a multipart body
27
- #
28
26
  # @return [Hash] the parsed multipart body
29
27
  #
30
28
  # @raise [Hanami::Middleware::BodyParser::BodyParsingError] when the body can't be parsed.
@@ -78,7 +78,7 @@ module Hanami
78
78
  # @since 0.5.0
79
79
  # @api private
80
80
  def initialize(env)
81
- super %(Cannot find routable endpoint for: #{env[::Rack::REQUEST_METHOD]} #{env[::Rack::PATH_INFO]})
81
+ super(%(Cannot find routable endpoint for: #{env[::Rack::REQUEST_METHOD]} #{env[::Rack::PATH_INFO]}))
82
82
  end
83
83
  end
84
84
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hanami
4
+ class Router
5
+ class GlobbedPath
6
+ def initialize(http_method, path, to)
7
+ @http_method = http_method
8
+ @path = path
9
+ @to = to
10
+ end
11
+
12
+ def endpoint_and_params(env)
13
+ return [] unless @http_method == env[::Rack::REQUEST_METHOD]
14
+
15
+ if (match = @path.match(env[::Rack::PATH_INFO]))
16
+ [@to, match.named_captures]
17
+ else
18
+ []
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hanami
4
+ class Router
5
+ class MountedPath
6
+ def initialize(prefix, app)
7
+ @prefix = prefix
8
+ @app = app
9
+ end
10
+
11
+ def endpoint_and_params(env)
12
+ return [] unless (match = @prefix.peek_match(env[::Rack::PATH_INFO]))
13
+
14
+ if @prefix.to_s == "/"
15
+ env[::Rack::SCRIPT_NAME] = EMPTY_STRING
16
+ else
17
+ env[::Rack::SCRIPT_NAME] = env[::Rack::SCRIPT_NAME].to_s + @prefix.to_s
18
+ env[::Rack::PATH_INFO] = env[::Rack::PATH_INFO].sub(@prefix.to_s, EMPTY_STRING)
19
+ env[::Rack::PATH_INFO] = DEFAULT_PREFIX if env[::Rack::PATH_INFO] == EMPTY_STRING
20
+ end
21
+
22
+ [@app, match.named_captures]
23
+ end
24
+ end
25
+ end
26
+ end
@@ -8,6 +8,6 @@ module Hanami
8
8
  #
9
9
  # @since 0.1.0
10
10
  # @api public
11
- VERSION = "2.1.0.rc2"
11
+ VERSION = "2.1.0"
12
12
  end
13
13
  end
data/lib/hanami/router.rb CHANGED
@@ -20,6 +20,8 @@ module Hanami
20
20
  require "hanami/router/block"
21
21
  require "hanami/router/route"
22
22
  require "hanami/router/url_helpers"
23
+ require "hanami/router/globbed_path"
24
+ require "hanami/router/mounted_path"
23
25
 
24
26
  # URL helpers for other Hanami integrations
25
27
  #
@@ -84,8 +86,7 @@ module Hanami
84
86
  @block_context = block_context
85
87
  @fixed = {}
86
88
  @variable = {}
87
- @globbed = {}
88
- @mounted = {}
89
+ @globs_and_mounts = []
89
90
  @blk = blk
90
91
  @inspector = inspector
91
92
  instance_eval(&blk) if blk
@@ -424,7 +425,7 @@ module Hanami
424
425
  path = prefixed_path(at)
425
426
  prefix = Segment.fabricate(path, **constraints)
426
427
 
427
- @mounted[prefix] = @resolver.call(path, app)
428
+ @globs_and_mounts << MountedPath.new(prefix, @resolver.call(path, app))
428
429
  if inspect?
429
430
  @inspector.add_route(Route.new(http_method: "*", path: at, to: app, constraints: constraints))
430
431
  end
@@ -619,31 +620,12 @@ module Hanami
619
620
  @variable[env[::Rack::REQUEST_METHOD]]&.find(env[::Rack::PATH_INFO])
620
621
  end
621
622
 
622
- # @since 2.0.0
623
- # @api private
624
- def globbed(env)
625
- @globbed[env[::Rack::REQUEST_METHOD]]&.each do |path, to|
626
- if (match = path.match(env[::Rack::PATH_INFO]))
627
- return [to, match.named_captures]
628
- end
629
- end
630
-
631
- nil
632
- end
633
-
634
- # @since 2.0.0
623
+ # @since 2.1.0
635
624
  # @api private
636
- def mounted(env)
637
- @mounted.each do |prefix, app|
638
- next unless (match = prefix.peek_match(env[::Rack::PATH_INFO]))
639
-
640
- # TODO: ensure compatibility with existing env[::Rack::SCRIPT_NAME]
641
- # TODO: cleanup this code
642
- env[::Rack::SCRIPT_NAME] = env[::Rack::SCRIPT_NAME].to_s + prefix.to_s
643
- env[::Rack::PATH_INFO] = env[::Rack::PATH_INFO].sub(prefix.to_s, EMPTY_STRING)
644
- env[::Rack::PATH_INFO] = DEFAULT_PREFIX if env[::Rack::PATH_INFO] == EMPTY_STRING
645
-
646
- return [app, match.named_captures]
625
+ def globbed_or_mounted(env)
626
+ @globs_and_mounts.each do |path|
627
+ result = path.endpoint_and_params(env)
628
+ return result unless result.empty?
647
629
  end
648
630
 
649
631
  nil
@@ -800,7 +782,7 @@ module Hanami
800
782
  endpoint = fixed(env)
801
783
  return [endpoint, {}] if endpoint
802
784
 
803
- variable(env) || mounted(env) || globbed(env)
785
+ variable(env) || globbed_or_mounted(env)
804
786
  end
805
787
 
806
788
  # @since 2.0.0
@@ -843,8 +825,7 @@ module Hanami
843
825
  # @since 2.0.0
844
826
  # @api private
845
827
  def add_globbed_route(http_method, path, to, constraints)
846
- @globbed[http_method] ||= []
847
- @globbed[http_method] << [Segment.fabricate(path, **constraints), to]
828
+ @globs_and_mounts << GlobbedPath.new(http_method, Segment.fabricate(path, **constraints), to)
848
829
  end
849
830
 
850
831
  # @since 2.0.0
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-router
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0.rc2
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-08 00:00:00.000000000 Z
11
+ date: 2024-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -169,7 +169,9 @@ files:
169
169
  - lib/hanami/router/errors.rb
170
170
  - lib/hanami/router/formatter/csv.rb
171
171
  - lib/hanami/router/formatter/human_friendly.rb
172
+ - lib/hanami/router/globbed_path.rb
172
173
  - lib/hanami/router/inspector.rb
174
+ - lib/hanami/router/mounted_path.rb
173
175
  - lib/hanami/router/node.rb
174
176
  - lib/hanami/router/params.rb
175
177
  - lib/hanami/router/prefix.rb
@@ -196,11 +198,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
196
198
  version: '3.0'
197
199
  required_rubygems_version: !ruby/object:Gem::Requirement
198
200
  requirements:
199
- - - ">"
201
+ - - ">="
200
202
  - !ruby/object:Gem::Version
201
- version: 1.3.1
203
+ version: '0'
202
204
  requirements: []
203
- rubygems_version: 3.4.21
205
+ rubygems_version: 3.5.6
204
206
  signing_key:
205
207
  specification_version: 4
206
208
  summary: Rack compatible HTTP router for Ruby and Hanami