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 +4 -4
- data/CHANGELOG.md +11 -0
- data/lib/hanami/middleware/app.rb +2 -2
- data/lib/hanami/router/errors.rb +1 -1
- data/lib/hanami/router/globbed_path.rb +23 -0
- data/lib/hanami/router/mounted_path.rb +26 -0
- data/lib/hanami/router/version.rb +1 -1
- data/lib/hanami/router.rb +11 -30
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd15cdabe88259102767109851f7eb4253c52fbdd8a77fff7e3e53512953866c
|
4
|
+
data.tar.gz: ca5c14f2abe932cda82da4ebacfe569e8acdfbce915d33e89201dd3af52fd044
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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)
|
data/lib/hanami/router/errors.rb
CHANGED
@@ -78,7 +78,7 @@ module Hanami
|
|
78
78
|
# @since 0.5.0
|
79
79
|
# @api private
|
80
80
|
def initialize(env)
|
81
|
-
super
|
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
|
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
|
-
@
|
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
|
-
@
|
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.
|
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
|
637
|
-
@
|
638
|
-
|
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) ||
|
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
|
-
@
|
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
|
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-
|
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
|