hanami-router 2.1.0.rc3 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e20f7326a54f71efcb3251ae1fd939bf9e7229d005e45a2ce65dc5d10551f7bd
4
- data.tar.gz: 1b3568b80c0742543a9706a86ae42600460b297887c9af9276949dbe1afd18c4
3
+ metadata.gz: bd15cdabe88259102767109851f7eb4253c52fbdd8a77fff7e3e53512953866c
4
+ data.tar.gz: ca5c14f2abe932cda82da4ebacfe569e8acdfbce915d33e89201dd3af52fd044
5
5
  SHA512:
6
- metadata.gz: 155276f1b0f2b72d43ab2356e5c3896e78d5fe874e4bc38310fea442ab2987a388197ae2b564a9cf7bad911bb8ae1ceb1235dddf16f804385486eb962840d0f9
7
- data.tar.gz: c80a5864b520752a7cfc4c9f4ebebe18f5e865598a618b4f0ef1154642d18f49a711b4748e73a6a067521cc13d9af98e6d2cddc8ddc9f8a0d3871054e401fc65
6
+ metadata.gz: 1b8dba8cd1f1a178b84a8222b0f0d671a87c0e8e58332022c4480a850a2b3daf3e1d9a0915299d0ee8249d016610a088c8ee7049d0bb93fbefb25e74586d6dbc
7
+ data.tar.gz: 1eca7bf5c3e7928f9d5400a87fa9d9089f43312f4d89684c5137c082469c31aeea5cd8f910fea9c08cc58a8cc01d2a659feee0a0c62593ccca1ba1854d70d6c8
data/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
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
+
5
16
  ## v2.1.0.rc3 - 2024-02-16
6
17
 
7
18
  ## v2.1.0.rc2 - 2023-11-08
@@ -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)
@@ -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.rc3"
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.rc3
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: 2024-02-16 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