hanami-router 3.0.0 → 3.1.0.rc1
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 +39 -5
- data/README.md +2 -2
- data/lib/hanami/middleware/app.rb +0 -7
- data/lib/hanami/middleware/trie.rb +7 -4
- data/lib/hanami/router/expander/mustermann_expander.rb +49 -0
- data/lib/hanami/router/expander/simple_expander.rb +93 -0
- data/lib/hanami/router/expander.rb +29 -0
- data/lib/hanami/router/formatter/csv.rb +18 -19
- data/lib/hanami/router/formatter/human_friendly.rb +12 -5
- data/lib/hanami/router/formatter.rb +22 -0
- data/lib/hanami/router/leaf.rb +7 -7
- data/lib/hanami/router/node.rb +8 -1
- data/lib/hanami/router/prefix.rb +6 -2
- data/lib/hanami/router/route.rb +3 -2
- data/lib/hanami/router/trie.rb +3 -8
- data/lib/hanami/router/url_helpers.rb +5 -20
- data/lib/hanami/router/version.rb +1 -1
- data/lib/hanami/router.rb +79 -47
- metadata +5 -2
- data/lib/hanami/router/inspector.rb +0 -46
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 909a2b0c1ec3e48e258a930ed6eedfb6505bb34483c3e330f08c3286ad931f6d
|
|
4
|
+
data.tar.gz: 4fb07d920180cbf185422e72b3530af7ae262e5a920bd0ce909ebf65519cface
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 32194b6b155c5ed9fe26a8bf59b8824e64f48fa180752b5add9d7a9cfc309859fad297edde680e4e06dd2991e6f6cac97ac9f78d5adb2ebaae0ba904049ed17f
|
|
7
|
+
data.tar.gz: c3da3adb5453c908d39347d63b9a8fc3b862843a7ea1cd84769071329baf026a25df421a5d5688fa7eea6540cfbba93085ceab281658bfa6424063ea02f4f204
|
data/CHANGELOG.md
CHANGED
|
@@ -19,17 +19,51 @@ and this project adheres to [Break Versioning](https://www.taoensso.com/break-ve
|
|
|
19
19
|
|
|
20
20
|
### Security
|
|
21
21
|
|
|
22
|
-
[Unreleased]: http://github.com/hanami/hanami-router/compare/v3.0.
|
|
22
|
+
[Unreleased]: http://github.com/hanami/hanami-router/compare/v3.1.0.rc1...HEAD
|
|
23
|
+
|
|
24
|
+
## [3.1.0.rc1] - 2026-11-27
|
|
25
|
+
|
|
26
|
+
### Added
|
|
27
|
+
|
|
28
|
+
- Add `Hanami::Router#routes`, returning a frozen array of the router's routes, in the order they were defined. The router now records its routes at all times, so they are available at runtime (for example, to list them on a "not found" error page). (@afomera and @timriley in #306)
|
|
29
|
+
- Make `Hanami::Router::Formatter::HumanFriendly` and `Hanami::Router::Formatter::CSV` public API. A formatter is any object that responds to `#call(routes, **options)` and returns a String. Use one to render routes: `Hanami::Router::Formatter::HumanFriendly.new.call(router.routes)`. (@timriley in #306)
|
|
30
|
+
- Support for HTTP QUERY method. (@katafrakt in #307)
|
|
31
|
+
|
|
32
|
+
### Changed
|
|
33
|
+
|
|
34
|
+
- Generate most paths without Mustermann, making `#path` and `#url` around 10 times faster, and routers with many named routes quicker to build. (@parndt in #311)
|
|
35
|
+
- Raise `FrozenError` when adding a route to, or mounting an app on, a frozen router. Previously, the route was added to the routing tables and served requests, even though the router was frozen. (@timriley in #306)
|
|
36
|
+
|
|
37
|
+
### Removed
|
|
38
|
+
|
|
39
|
+
- **BREAKING:** Remove `Hanami::Router::Inspector`, the `inspector:` argument to `Hanami::Router.new`, and `Hanami::Router#inspector`. Use `#routes` with a formatter instead: `Hanami::Router::Formatter::HumanFriendly.new.call(router.routes)`. If you use hanami and hanami-cli, upgrade those to v3.1, which use the formatters directly. (@timriley in #306)
|
|
40
|
+
|
|
41
|
+
### Fixed
|
|
42
|
+
|
|
43
|
+
- Allow route names to give only a prefix, with an `as:` of `[prefix, nil]`. Previously, this gave a name with a trailing underscore. For example, `scope "admin" do get "/", to: dashboard, as: [:dashboard, nil] end` now gives `dashboard_admin`, rather than `dashboard_admin_`. (@timriley in #314)
|
|
44
|
+
- Record the full prefixed path for apps mounted within a scope or under a router prefix. Previously, `mount app, at: "/api"` within `scope "v1"` was listed with the path `/api` instead of `/v1/api`. (@timriley in #306)
|
|
45
|
+
- `Trie#find` keeps every path it looks up. (@aaronmallen in #312)
|
|
46
|
+
|
|
47
|
+
[3.1.0.rc1]: http://github.com/hanami/hanami-router/compare/v3.0.1...v3.1.0.rc1
|
|
48
|
+
|
|
49
|
+
## [3.0.1] - 2026-11-24
|
|
50
|
+
|
|
51
|
+
### Security
|
|
52
|
+
|
|
53
|
+
- Apply a scope's middleware to all of its paths when it contains a nested scope or slice with its own middleware. Previously, paths in the outer scope that weren't in the nested scope skipped the outer scope's middleware, such as authentication. (@parndt in GHSA-73j7-6jvw-2v7g)
|
|
54
|
+
- Stop concurrent requests to the same route from receiving each other's path params. Matching a route stored the captured params on the route itself, so a thread switch between matching and reading them could give one request another's params. (@parndt in GHSA-wgjv-fq27-94m7)
|
|
55
|
+
|
|
56
|
+
[3.0.1]: http://github.com/hanami/hanami-router/compare/v3.0.0...v3.0.1
|
|
23
57
|
|
|
24
58
|
## [3.0.0] - 2026-06-30
|
|
25
59
|
|
|
26
60
|
### Added
|
|
27
61
|
|
|
28
|
-
- Add `#
|
|
62
|
+
- Add `#redirect_permanent` and `#redirect_temporary` helpers for 301 and 302 responses. (@cllns in #302)
|
|
29
63
|
|
|
30
64
|
### Changed
|
|
31
65
|
|
|
32
|
-
- **BREAKING:** `#redirect` now requires an explicit `code:` argument. Use `#
|
|
66
|
+
- **BREAKING:** `#redirect` now requires an explicit `code:` argument. Use `#redirect_permanent` or `#redirect_temporary` for the common cases, and pass `code:` to `#redirect` for less common codes (e.g. `303`, `307`, `308`). (@cllns in #302)
|
|
33
67
|
- Upgrade mustermann to 3.1 and remove mustermann-contrib dependency. (@rkh in #300)
|
|
34
68
|
- Require Ruby 3.3 or newer.
|
|
35
69
|
|
|
@@ -44,11 +78,11 @@ and this project adheres to [Break Versioning](https://www.taoensso.com/break-ve
|
|
|
44
78
|
|
|
45
79
|
### Added
|
|
46
80
|
|
|
47
|
-
- Add `#
|
|
81
|
+
- Add `#redirect_permanent` and `#redirect_temporary` helpers for 301 and 302 responses. (@cllns in #302)
|
|
48
82
|
|
|
49
83
|
### Changed
|
|
50
84
|
|
|
51
|
-
- **BREAKING:** `#redirect` now requires an explicit `code:` argument. Use `#
|
|
85
|
+
- **BREAKING:** `#redirect` now requires an explicit `code:` argument. Use `#redirect_permanent` or `#redirect_temporary` for the common cases, and pass `code:` to `#redirect` for less common codes (e.g. `303`, `307`, `308`). (@cllns in #302)
|
|
52
86
|
- Upgrade mustermann to 3.1 and remove mustermann-contrib dependency. (@rkh in #300)
|
|
53
87
|
- Require Ruby 3.3 or newer.
|
|
54
88
|
|
data/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[actions]: https://github.com/hanami/hanami-router/actions
|
|
4
4
|
[chat]: https://discord.gg/naQApPAsZB
|
|
5
|
-
[forum]: https://discourse.
|
|
5
|
+
[forum]: https://discourse.hanakai.org
|
|
6
6
|
[rubygem]: https://rubygems.org/gems/hanami-router
|
|
7
7
|
|
|
8
8
|
# Hanami Router [][rubygem] [][actions]
|
|
@@ -344,7 +344,7 @@ route.routable? # => false
|
|
|
344
344
|
1. Fork this repo to your account and clone it locally (`git clone git@github.com:your-pseudo/your-cloned-repo.git`)
|
|
345
345
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
346
346
|
3. Install the dependencies (`bundle install`)
|
|
347
|
-
4. Run tests, they should all pass (
|
|
347
|
+
4. Run tests, they should all pass (`rake spec`)
|
|
348
348
|
5. Make your changes & check that the tests still pass. Add some test cases if needed.
|
|
349
349
|
6. Commit your changes (`git commit -am 'Add some feature'`)
|
|
350
350
|
7. Push to the branch (`git push origin my-new-feature`)
|
|
@@ -31,7 +31,6 @@ module Hanami
|
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
@trie.freeze
|
|
34
|
-
@inspector = router.inspector.freeze
|
|
35
34
|
end
|
|
36
35
|
|
|
37
36
|
# @since 2.0.0
|
|
@@ -39,12 +38,6 @@ module Hanami
|
|
|
39
38
|
def call(env)
|
|
40
39
|
@trie.find(env[::Rack::PATH_INFO]).call(env)
|
|
41
40
|
end
|
|
42
|
-
|
|
43
|
-
# @since 2.0.0
|
|
44
|
-
# @api private
|
|
45
|
-
def to_inspect
|
|
46
|
-
@inspector&.call.to_s
|
|
47
|
-
end
|
|
48
41
|
end
|
|
49
42
|
end
|
|
50
43
|
end
|
|
@@ -38,16 +38,19 @@ module Hanami
|
|
|
38
38
|
# @since 2.0.0
|
|
39
39
|
def find(path)
|
|
40
40
|
node = @root
|
|
41
|
+
app = @root.app
|
|
41
42
|
|
|
43
|
+
# Track the deepest scope with an app on the way down. When the descent stops early (a
|
|
44
|
+
# segment with no matching child under a node that has children), the request is still
|
|
45
|
+
# within that scope, so its middleware must apply.
|
|
42
46
|
for_each_segment(path) do |segment|
|
|
47
|
+
node = node.get(segment)
|
|
43
48
|
break unless node
|
|
44
49
|
|
|
45
|
-
|
|
50
|
+
app = node.app if node.app?
|
|
46
51
|
end
|
|
47
52
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
@root.app || @app
|
|
53
|
+
app || @app
|
|
51
54
|
end
|
|
52
55
|
|
|
53
56
|
# @api private
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rack/utils"
|
|
4
|
+
require_relative "../segment"
|
|
5
|
+
|
|
6
|
+
module Hanami
|
|
7
|
+
class Router
|
|
8
|
+
module Expander
|
|
9
|
+
# Generates a named route's paths with Mustermann
|
|
10
|
+
#
|
|
11
|
+
# @since 3.1.0
|
|
12
|
+
# @api private
|
|
13
|
+
class MustermannExpander
|
|
14
|
+
# @since 3.1.0
|
|
15
|
+
# @api private
|
|
16
|
+
def initialize(path)
|
|
17
|
+
@segment = Segment.fabricate(path)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Variables the path doesn't have are added to the query string, as are arrays.
|
|
21
|
+
#
|
|
22
|
+
# @raise [Mustermann::ExpandError] when the variables don't fill the path
|
|
23
|
+
#
|
|
24
|
+
# @since 3.1.0
|
|
25
|
+
# @api private
|
|
26
|
+
def expand(variables)
|
|
27
|
+
scalar_vars = variables.reject { |_, value| value.is_a?(Array) }
|
|
28
|
+
array_vars = array_query_vars(variables)
|
|
29
|
+
|
|
30
|
+
expanded_path = @segment.expand(:append, scalar_vars)
|
|
31
|
+
return expanded_path if array_vars.empty?
|
|
32
|
+
|
|
33
|
+
join_char = expanded_path.include?("?") ? "&" : "?"
|
|
34
|
+
"#{expanded_path}#{join_char}#{Rack::Utils.build_query(array_vars)}"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
# @since 3.1.0
|
|
40
|
+
# @api private
|
|
41
|
+
def array_query_vars(variables)
|
|
42
|
+
variables
|
|
43
|
+
.select { |_, value| value.is_a?(Array) }
|
|
44
|
+
.to_h { |key, value| ["#{key}[]", value] }
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "mustermann_expander"
|
|
4
|
+
|
|
5
|
+
module Hanami
|
|
6
|
+
class Router
|
|
7
|
+
module Expander
|
|
8
|
+
# Generates a named route's paths without Mustermann, for a path that's only unreserved
|
|
9
|
+
# characters, slashes and variables, and for values that need no escaping
|
|
10
|
+
#
|
|
11
|
+
# Anything else is left to a Mustermann expander, built the first time it's needed.
|
|
12
|
+
#
|
|
13
|
+
# @since 3.1.0
|
|
14
|
+
# @api private
|
|
15
|
+
class SimpleExpander
|
|
16
|
+
# @since 3.1.0
|
|
17
|
+
# @api private
|
|
18
|
+
SIMPLE_PATH = %r{\A(?:[a-zA-Z0-9\-._~/]|:\w+)*\z}
|
|
19
|
+
private_constant :SIMPLE_PATH
|
|
20
|
+
|
|
21
|
+
# @since 3.1.0
|
|
22
|
+
# @api private
|
|
23
|
+
VARIABLE = /(:\w+)/
|
|
24
|
+
private_constant :VARIABLE
|
|
25
|
+
|
|
26
|
+
# @since 3.1.0
|
|
27
|
+
# @api private
|
|
28
|
+
PLAIN_VALUE = /\A[a-zA-Z0-9_]+\z/
|
|
29
|
+
private_constant :PLAIN_VALUE
|
|
30
|
+
|
|
31
|
+
# @return [SimpleExpander, nil] nil when the path isn't simple
|
|
32
|
+
#
|
|
33
|
+
# @since 3.1.0
|
|
34
|
+
# @api private
|
|
35
|
+
def self.fabricate(path)
|
|
36
|
+
return unless SIMPLE_PATH.match?(path)
|
|
37
|
+
|
|
38
|
+
parts = path.split(VARIABLE).reject(&:empty?).map do |part|
|
|
39
|
+
part.start_with?(":") ? part[1..].to_sym : part.freeze
|
|
40
|
+
end
|
|
41
|
+
names = parts.grep(Symbol)
|
|
42
|
+
return unless names.uniq.size == names.size
|
|
43
|
+
|
|
44
|
+
new(path, parts, names)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# @since 3.1.0
|
|
48
|
+
# @api private
|
|
49
|
+
def initialize(path, parts, names)
|
|
50
|
+
@path = path
|
|
51
|
+
@parts = parts.freeze
|
|
52
|
+
@names = names.freeze
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# @raise [Mustermann::ExpandError] when the variables don't fill the path
|
|
56
|
+
#
|
|
57
|
+
# @since 3.1.0
|
|
58
|
+
# @api private
|
|
59
|
+
def expand(variables)
|
|
60
|
+
return mustermann.expand(variables) unless simple?(variables)
|
|
61
|
+
|
|
62
|
+
@parts.map { |part| part.is_a?(Symbol) ? variables[part].to_s : part }.join
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
# Exactly the path's variables, each with a value that needs no escaping
|
|
68
|
+
#
|
|
69
|
+
# @since 3.1.0
|
|
70
|
+
# @api private
|
|
71
|
+
def simple?(variables)
|
|
72
|
+
variables.size == @names.size && @names.all? { |name| plain?(variables[name]) }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# @since 3.1.0
|
|
76
|
+
# @api private
|
|
77
|
+
def plain?(value)
|
|
78
|
+
case value
|
|
79
|
+
when Integer then true
|
|
80
|
+
when String then PLAIN_VALUE.match?(value)
|
|
81
|
+
else false
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# @since 3.1.0
|
|
86
|
+
# @api private
|
|
87
|
+
def mustermann
|
|
88
|
+
@mustermann ||= MustermannExpander.new(@path)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "expander/mustermann_expander"
|
|
4
|
+
require_relative "expander/simple_expander"
|
|
5
|
+
|
|
6
|
+
module Hanami
|
|
7
|
+
class Router
|
|
8
|
+
# Builds the expander that generates a named route's paths
|
|
9
|
+
#
|
|
10
|
+
# Constraints aren't checked when expanding, so they play no part here.
|
|
11
|
+
#
|
|
12
|
+
# @since 3.1.0
|
|
13
|
+
# @api private
|
|
14
|
+
module Expander
|
|
15
|
+
# Returns a simple expander for routes it can expand without Mustermann, and a Mustermann
|
|
16
|
+
# one for the rest.
|
|
17
|
+
#
|
|
18
|
+
# @param path [String] the route's path
|
|
19
|
+
#
|
|
20
|
+
# @return [SimpleExpander, MustermannExpander]
|
|
21
|
+
#
|
|
22
|
+
# @since 3.1.0
|
|
23
|
+
# @api private
|
|
24
|
+
def self.fabricate(path)
|
|
25
|
+
SimpleExpander.fabricate(path) || MustermannExpander.new(path)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -4,25 +4,19 @@ require "csv"
|
|
|
4
4
|
|
|
5
5
|
module Hanami
|
|
6
6
|
class Router
|
|
7
|
-
# Renders a CSV representation of the routes
|
|
8
|
-
#
|
|
9
|
-
# You can forward [CSV generation
|
|
10
|
-
# options](https://ruby-doc.org/stdlib-3.1.0/libdoc/csv/rdoc/CSV.html#class-CSV-label-Options+for+Generating]
|
|
11
|
-
# when calling it:
|
|
12
|
-
#
|
|
13
|
-
# ```
|
|
14
|
-
# require "hanami/router/inspector"
|
|
15
|
-
# require "hanami/router/formatter/csv"
|
|
16
|
-
#
|
|
17
|
-
# Hanami::Router::Inspector.new(
|
|
18
|
-
# routes: Router.routes,
|
|
19
|
-
# formatter: Hanami::Router::Formatter::CSV.new
|
|
20
|
-
# ).call(write_headers: false)
|
|
21
|
-
# ```
|
|
22
|
-
#
|
|
23
|
-
# @api private
|
|
24
|
-
# @since 2.0.0
|
|
25
7
|
module Formatter
|
|
8
|
+
# Renders a CSV representation of the routes, skipping HEAD routes.
|
|
9
|
+
#
|
|
10
|
+
# You can forward [CSV generation
|
|
11
|
+
# options](https://ruby-doc.org/stdlib-3.1.0/libdoc/csv/rdoc/CSV.html#class-CSV-label-Options+for+Generating]
|
|
12
|
+
# when calling it:
|
|
13
|
+
#
|
|
14
|
+
# ```
|
|
15
|
+
# Hanami::Router::Formatter::CSV.new.call(router.routes, write_headers: false)
|
|
16
|
+
# ```
|
|
17
|
+
#
|
|
18
|
+
# @api public
|
|
19
|
+
# @since 2.0.0
|
|
26
20
|
class CSV
|
|
27
21
|
# @api private
|
|
28
22
|
# @since 2.0.0
|
|
@@ -34,7 +28,12 @@ module Hanami
|
|
|
34
28
|
# @since 2.0.0
|
|
35
29
|
HEADERS = %w[METHOD PATH TO AS CONSTRAINTS].freeze
|
|
36
30
|
|
|
37
|
-
# @
|
|
31
|
+
# @param routes [Array<Hanami::Router::Route>]
|
|
32
|
+
# @param csv_opts [Hash] options for `CSV.generate`
|
|
33
|
+
#
|
|
34
|
+
# @return [String]
|
|
35
|
+
#
|
|
36
|
+
# @api public
|
|
38
37
|
# @since 2.0.0
|
|
39
38
|
def call(routes, **csv_opts)
|
|
40
39
|
::CSV.generate(**DEFAULT_OPTIONS, **csv_opts) do |csv|
|
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
module Hanami
|
|
4
4
|
class Router
|
|
5
|
-
# Renders a human friendly representation of the routes
|
|
6
|
-
#
|
|
7
|
-
# @api private
|
|
8
|
-
# @since 2.0.0
|
|
9
5
|
module Formatter
|
|
6
|
+
# Renders a human friendly representation of the routes, skipping HEAD routes.
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
# Hanami::Router::Formatter::HumanFriendly.new.call(router.routes)
|
|
10
|
+
#
|
|
11
|
+
# @api public
|
|
12
|
+
# @since 2.0.0
|
|
10
13
|
class HumanFriendly
|
|
11
14
|
# @api private
|
|
12
15
|
# @since 2.0.0
|
|
@@ -33,7 +36,11 @@ module Hanami
|
|
|
33
36
|
EXTRA_LARGE_STRING_JUSTIFY_AMOUNT = 40
|
|
34
37
|
private_constant :EXTRA_LARGE_STRING_JUSTIFY_AMOUNT
|
|
35
38
|
|
|
36
|
-
# @
|
|
39
|
+
# @param routes [Array<Hanami::Router::Route>]
|
|
40
|
+
#
|
|
41
|
+
# @return [String]
|
|
42
|
+
#
|
|
43
|
+
# @api public
|
|
37
44
|
# @since 2.0.0
|
|
38
45
|
def call(routes)
|
|
39
46
|
routes.filter_map(&method(:format_route_unless_head)).join(NEW_LINE)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Hanami
|
|
4
|
+
class Router
|
|
5
|
+
# Formatters render a router's {Router#routes}.
|
|
6
|
+
#
|
|
7
|
+
# A formatter is any object that responds to `#call(routes, **options)` and returns a
|
|
8
|
+
# String. It can use the {Route} methods, such as {Route#inspect_to}, to render each route.
|
|
9
|
+
#
|
|
10
|
+
# The built-in formatters load when first used.
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
# Hanami::Router::Formatter::HumanFriendly.new.call(router.routes)
|
|
14
|
+
#
|
|
15
|
+
# @api public
|
|
16
|
+
# @since 2.0.0
|
|
17
|
+
module Formatter
|
|
18
|
+
autoload :HumanFriendly, "hanami/router/formatter/human_friendly"
|
|
19
|
+
autoload :CSV, "hanami/router/formatter/csv"
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
data/lib/hanami/router/leaf.rb
CHANGED
|
@@ -9,24 +9,24 @@ module Hanami
|
|
|
9
9
|
#
|
|
10
10
|
# @api private
|
|
11
11
|
# @since 2.2.0
|
|
12
|
-
attr_reader :to, :
|
|
12
|
+
attr_reader :to, :matcher
|
|
13
13
|
|
|
14
14
|
# @api private
|
|
15
15
|
# @since 2.2.0
|
|
16
16
|
def initialize(route, to, constraints)
|
|
17
17
|
@to = to
|
|
18
|
-
@params = nil
|
|
19
18
|
@matcher = Mustermann.new(route, type: :rails, version: "5.0", capture: constraints)
|
|
20
19
|
end
|
|
21
20
|
|
|
21
|
+
# Returns the captured params for the given path, or nil if the path doesn't match.
|
|
22
|
+
#
|
|
23
|
+
# Leaves are shared across concurrent requests, so the captures are returned rather than
|
|
24
|
+
# stored on the leaf.
|
|
25
|
+
#
|
|
22
26
|
# @api private
|
|
23
27
|
# @since 2.2.0
|
|
24
28
|
def match(path)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
@params = match.named_captures if match
|
|
28
|
-
|
|
29
|
-
match
|
|
29
|
+
matcher.match(path)&.named_captures
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
end
|
data/lib/hanami/router/node.rb
CHANGED
|
@@ -45,10 +45,17 @@ module Hanami
|
|
|
45
45
|
@leaves << Leaf.new(route, to, constraints)
|
|
46
46
|
end
|
|
47
47
|
|
|
48
|
+
# Returns the endpoint and captured params of the first leaf matching the given path, or nil.
|
|
49
|
+
#
|
|
48
50
|
# @api private
|
|
49
51
|
# @since 2.2.0
|
|
50
52
|
def match(path)
|
|
51
|
-
@leaves&.
|
|
53
|
+
@leaves&.each do |leaf|
|
|
54
|
+
params = leaf.match(path)
|
|
55
|
+
return [leaf.to, params] if params
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
nil
|
|
52
59
|
end
|
|
53
60
|
|
|
54
61
|
private
|
data/lib/hanami/router/prefix.rb
CHANGED
|
@@ -23,9 +23,13 @@ module Hanami
|
|
|
23
23
|
|
|
24
24
|
# @since 2.0.0
|
|
25
25
|
# @api private
|
|
26
|
+
#
|
|
27
|
+
# Returns only the prefix, with no trailing separator, when the path is empty.
|
|
26
28
|
def relative_join(path, separator = DEFAULT_SEPARATOR)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
path = path.to_s
|
|
30
|
+
joined = path.empty? ? @prefix : _join(path)
|
|
31
|
+
|
|
32
|
+
joined.gsub(DEFAULT_SEPARATOR_REGEXP, separator)[1..].to_s
|
|
29
33
|
end
|
|
30
34
|
|
|
31
35
|
# @since 2.0.0
|
data/lib/hanami/router/route.rb
CHANGED
|
@@ -63,12 +63,13 @@ module Hanami
|
|
|
63
63
|
|
|
64
64
|
# @api private
|
|
65
65
|
# @since 2.0.0
|
|
66
|
-
def initialize(http_method:, path:, to:, as: nil, constraints:
|
|
66
|
+
def initialize(http_method:, path:, to:, as: nil, constraints: Router::EMPTY_HASH, blk: nil)
|
|
67
67
|
@http_method = http_method
|
|
68
68
|
@path = path
|
|
69
69
|
@to = to
|
|
70
70
|
@as = as
|
|
71
|
-
|
|
71
|
+
# Most routes have no constraints. Share one empty hash rather than keep one per route.
|
|
72
|
+
@constraints = constraints.empty? ? Router::EMPTY_HASH : constraints
|
|
72
73
|
@blk = blk
|
|
73
74
|
freeze
|
|
74
75
|
end
|
data/lib/hanami/router/trie.rb
CHANGED
|
@@ -17,7 +17,6 @@ module Hanami
|
|
|
17
17
|
# @since 2.0.0
|
|
18
18
|
def initialize
|
|
19
19
|
@root = Node.new
|
|
20
|
-
@segments_map = {}
|
|
21
20
|
end
|
|
22
21
|
|
|
23
22
|
# @api private
|
|
@@ -41,7 +40,7 @@ module Hanami
|
|
|
41
40
|
|
|
42
41
|
return unless segments.all? { |segment| node = node.get(segment) }
|
|
43
42
|
|
|
44
|
-
node.match(path)
|
|
43
|
+
node.match(path)
|
|
45
44
|
end
|
|
46
45
|
|
|
47
46
|
private
|
|
@@ -54,12 +53,8 @@ module Hanami
|
|
|
54
53
|
# @api private
|
|
55
54
|
# @since 2.2.0
|
|
56
55
|
def segments_from(path)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
else
|
|
60
|
-
_, *segments = path.split(SEGMENT_SEPARATOR)
|
|
61
|
-
@segments_map[path] = segments
|
|
62
|
-
end
|
|
56
|
+
_, *segments = path.split(SEGMENT_SEPARATOR)
|
|
57
|
+
segments
|
|
63
58
|
end
|
|
64
59
|
end
|
|
65
60
|
end
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "hanami/router/errors"
|
|
4
4
|
require "mustermann/error"
|
|
5
|
+
require_relative "expander"
|
|
5
6
|
require_relative "prefix"
|
|
6
7
|
|
|
7
8
|
module Hanami
|
|
@@ -19,23 +20,15 @@ module Hanami
|
|
|
19
20
|
end
|
|
20
21
|
|
|
21
22
|
# @api private
|
|
22
|
-
def add(name,
|
|
23
|
-
@named[name] =
|
|
23
|
+
def add(name, path)
|
|
24
|
+
@named[name] = Expander.fabricate(path)
|
|
24
25
|
end
|
|
25
26
|
|
|
26
27
|
# @api private
|
|
27
28
|
def path(name, variables = {})
|
|
28
|
-
|
|
29
|
-
array_vars = array_query_vars(variables)
|
|
30
|
-
|
|
31
|
-
expanded_path = @named
|
|
29
|
+
@named
|
|
32
30
|
.fetch(name.to_sym) { raise MissingRouteError.new(name) }
|
|
33
|
-
.expand(
|
|
34
|
-
|
|
35
|
-
return expanded_path if array_vars.empty?
|
|
36
|
-
|
|
37
|
-
join_char = expanded_path.include?("?") ? "&" : "?"
|
|
38
|
-
"#{expanded_path}#{join_char}#{Rack::Utils.build_query(array_vars)}"
|
|
31
|
+
.expand(variables)
|
|
39
32
|
rescue Mustermann::ExpandError => exception
|
|
40
33
|
raise InvalidRouteExpansionError.new(name, exception.message)
|
|
41
34
|
end
|
|
@@ -44,14 +37,6 @@ module Hanami
|
|
|
44
37
|
def url(name, variables = {})
|
|
45
38
|
@base_url + @prefix.join(path(name, variables)).to_s
|
|
46
39
|
end
|
|
47
|
-
|
|
48
|
-
private
|
|
49
|
-
|
|
50
|
-
def array_query_vars(variables = {})
|
|
51
|
-
variables
|
|
52
|
-
.select { |_, value| value.is_a?(Array) }
|
|
53
|
-
.to_h { |key, value| ["#{key}[]", value] }
|
|
54
|
-
end
|
|
55
40
|
end
|
|
56
41
|
end
|
|
57
42
|
end
|
data/lib/hanami/router.rb
CHANGED
|
@@ -23,6 +23,7 @@ module Hanami
|
|
|
23
23
|
require "hanami/router/globbed_path"
|
|
24
24
|
require "hanami/router/mounted_path"
|
|
25
25
|
require "hanami/router/rack_utils"
|
|
26
|
+
require "hanami/router/formatter"
|
|
26
27
|
|
|
27
28
|
# URL helpers for other Hanami integrations
|
|
28
29
|
#
|
|
@@ -30,12 +31,31 @@ module Hanami
|
|
|
30
31
|
# @since 2.0.0
|
|
31
32
|
attr_reader :url_helpers
|
|
32
33
|
|
|
33
|
-
#
|
|
34
|
+
# Returns the routes defined on this router, in the order they were defined.
|
|
34
35
|
#
|
|
35
|
-
#
|
|
36
|
+
# Each GET route is followed by the HEAD route the router generates for it. Routes defined
|
|
37
|
+
# within a {#scope}, or under a router prefix, have their full prefixed path.
|
|
36
38
|
#
|
|
37
|
-
#
|
|
38
|
-
|
|
39
|
+
# The returned array is frozen. Once the router is frozen, every call returns the same array.
|
|
40
|
+
#
|
|
41
|
+
# To render the routes, pass them to a formatter, such as {Formatter::HumanFriendly} or
|
|
42
|
+
# {Formatter::CSV}.
|
|
43
|
+
#
|
|
44
|
+
# @return [Array<Hanami::Router::Route>]
|
|
45
|
+
#
|
|
46
|
+
# @example
|
|
47
|
+
# router = Hanami::Router.new do
|
|
48
|
+
# get "/books/:id", to: "books.show", as: :book
|
|
49
|
+
# end
|
|
50
|
+
#
|
|
51
|
+
# router.routes.map { |route| "#{route.http_method} #{route.path}" }
|
|
52
|
+
# # => ["GET /books/:id", "HEAD /books/:id"]
|
|
53
|
+
#
|
|
54
|
+
# @since 3.1.0
|
|
55
|
+
# @api public
|
|
56
|
+
def routes
|
|
57
|
+
@routes.frozen? ? @routes : @routes.dup.freeze
|
|
58
|
+
end
|
|
39
59
|
|
|
40
60
|
# Returns the given block as it is.
|
|
41
61
|
#
|
|
@@ -75,7 +95,7 @@ module Hanami
|
|
|
75
95
|
# Hanami::Router.new do
|
|
76
96
|
# get "/", to: ->(*) { [200, {}, ["OK"]] }
|
|
77
97
|
# end
|
|
78
|
-
def initialize(base_url: DEFAULT_BASE_URL, prefix: DEFAULT_PREFIX, resolver: DEFAULT_RESOLVER, not_allowed: NOT_ALLOWED, not_found: NOT_FOUND, block_context: nil,
|
|
98
|
+
def initialize(base_url: DEFAULT_BASE_URL, prefix: DEFAULT_PREFIX, resolver: DEFAULT_RESOLVER, not_allowed: NOT_ALLOWED, not_found: NOT_FOUND, block_context: nil, &blk) # rubocop:disable Layout/LineLength
|
|
79
99
|
# TODO: verify if Prefix can handle both name and path prefix
|
|
80
100
|
@path_prefix = Prefix.new(prefix)
|
|
81
101
|
@name_prefix = Prefix.new("")
|
|
@@ -88,11 +108,22 @@ module Hanami
|
|
|
88
108
|
@fixed = {}
|
|
89
109
|
@variable = {}
|
|
90
110
|
@globs_and_mounts = []
|
|
111
|
+
@routes = []
|
|
91
112
|
@blk = blk
|
|
92
|
-
@inspector = inspector
|
|
93
113
|
instance_eval(&blk) if blk
|
|
94
114
|
end
|
|
95
115
|
|
|
116
|
+
# Freezes the router, along with its {#routes}.
|
|
117
|
+
#
|
|
118
|
+
# @return [self]
|
|
119
|
+
#
|
|
120
|
+
# @since 2.0.0
|
|
121
|
+
# @api public
|
|
122
|
+
def freeze
|
|
123
|
+
@routes.freeze
|
|
124
|
+
super
|
|
125
|
+
end
|
|
126
|
+
|
|
96
127
|
# Resolve the given Rack env to a registered endpoint and invokes it.
|
|
97
128
|
#
|
|
98
129
|
# @param env [Hash] a Rack env
|
|
@@ -224,6 +255,22 @@ module Hanami
|
|
|
224
255
|
add_route(::Rack::HEAD, path, to, as, constraints, &blk)
|
|
225
256
|
end
|
|
226
257
|
|
|
258
|
+
if defined?(::Rack::QUERY)
|
|
259
|
+
# Defines a route that accepts QUERY requests for the given path.
|
|
260
|
+
#
|
|
261
|
+
# @param path [String] the relative URL to be matched
|
|
262
|
+
# @param to [#call] the Rack endpoint
|
|
263
|
+
# @param as [Symbol, Array<Symbol>] a unique name for the route, or a ["prefix", "name"] array,
|
|
264
|
+
# to add a prefix to the name when nested within scopes.
|
|
265
|
+
# @param constraints [Hash] a set of constraints for path variables
|
|
266
|
+
# @param blk [Proc] the anonymous proc to be used as endpoint for the route
|
|
267
|
+
#
|
|
268
|
+
# @since 3.1.0
|
|
269
|
+
def query(path, to: nil, as: nil, **constraints, &blk)
|
|
270
|
+
add_route(::Rack::QUERY, path, to, as, constraints, &blk)
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
|
|
227
274
|
# Defines a route that accepts POST requests for the given path.
|
|
228
275
|
#
|
|
229
276
|
# @param path [String] the relative URL to be matched
|
|
@@ -499,13 +546,13 @@ module Hanami
|
|
|
499
546
|
# mount MyRackApp.new, at: "/foo"
|
|
500
547
|
# end
|
|
501
548
|
def mount(app, at:, **constraints)
|
|
549
|
+
raise_if_frozen
|
|
550
|
+
|
|
502
551
|
path = prefixed_path(at)
|
|
503
552
|
prefix = Segment.fabricate(path, **constraints)
|
|
504
553
|
|
|
505
554
|
@globs_and_mounts << MountedPath.new(prefix, @resolver.call(path, app))
|
|
506
|
-
|
|
507
|
-
@inspector.add_route(Route.new(http_method: "*", path: at, to: app, constraints: constraints))
|
|
508
|
-
end
|
|
555
|
+
@routes << Route.new(http_method: "*", path:, to: app, constraints:)
|
|
509
556
|
end
|
|
510
557
|
|
|
511
558
|
# Generate an relative URL for a specified named route.
|
|
@@ -894,6 +941,8 @@ module Hanami
|
|
|
894
941
|
# @since 2.0.0
|
|
895
942
|
# @api private
|
|
896
943
|
def add_route(http_method, path, to, as, constraints, &blk)
|
|
944
|
+
raise_if_frozen
|
|
945
|
+
|
|
897
946
|
path = prefixed_path(path)
|
|
898
947
|
endpoint = resolve_endpoint(path, to, blk)
|
|
899
948
|
|
|
@@ -907,14 +956,10 @@ module Hanami
|
|
|
907
956
|
|
|
908
957
|
if as
|
|
909
958
|
as = prefixed_name(as)
|
|
910
|
-
add_named_route(path, as
|
|
959
|
+
add_named_route(path, as)
|
|
911
960
|
end
|
|
912
961
|
|
|
913
|
-
|
|
914
|
-
@inspector.add_route(
|
|
915
|
-
Route.new(http_method:, path:, to: to || endpoint, as:, constraints:, blk:)
|
|
916
|
-
)
|
|
917
|
-
end
|
|
962
|
+
@routes << Route.new(http_method:, path:, to: to || endpoint, as:, constraints:, blk:)
|
|
918
963
|
end
|
|
919
964
|
|
|
920
965
|
# @since 2.0.0
|
|
@@ -948,8 +993,8 @@ module Hanami
|
|
|
948
993
|
|
|
949
994
|
# @since 2.0.0
|
|
950
995
|
# @api private
|
|
951
|
-
def add_named_route(path, name
|
|
952
|
-
@url_helpers.add(name,
|
|
996
|
+
def add_named_route(path, name)
|
|
997
|
+
@url_helpers.add(name, path)
|
|
953
998
|
end
|
|
954
999
|
|
|
955
1000
|
# @since 2.0.0
|
|
@@ -964,26 +1009,35 @@ module Hanami
|
|
|
964
1009
|
ROUTE_GLOBBED_MATCHER.match?(path)
|
|
965
1010
|
end
|
|
966
1011
|
|
|
967
|
-
#
|
|
1012
|
+
# Raises before a route is added to a frozen router.
|
|
1013
|
+
#
|
|
1014
|
+
# Freezing the router does not freeze its routing tables, so without this check a route would
|
|
1015
|
+
# be added to them, and so start serving requests, before {#routes} raised on being changed.
|
|
1016
|
+
#
|
|
1017
|
+
# @raise [FrozenError] if the router is frozen
|
|
1018
|
+
#
|
|
1019
|
+
# @since 3.1.0
|
|
968
1020
|
# @api private
|
|
969
|
-
def
|
|
970
|
-
|
|
1021
|
+
def raise_if_frozen
|
|
1022
|
+
raise FrozenError.new("can't modify frozen #{self.class}", receiver: self) if frozen?
|
|
971
1023
|
end
|
|
972
1024
|
|
|
973
1025
|
# @since 2.0.0
|
|
974
1026
|
# @api private
|
|
975
1027
|
def prefixed_path(path)
|
|
976
|
-
|
|
1028
|
+
# Intern the path so every use of it shares a single string. This includes the routing tables,
|
|
1029
|
+
# such as the hash keys in {#add_fixed_route}, and each route we record for {#routes}. This
|
|
1030
|
+
# means a route defined for several HTTP methods (including the HEAD added for every GET) then
|
|
1031
|
+
# keeps just one copy of its path.
|
|
1032
|
+
-@path_prefix.join(path).to_s
|
|
977
1033
|
end
|
|
978
1034
|
|
|
979
1035
|
# @api private
|
|
980
1036
|
def prefixed_name(name)
|
|
981
1037
|
prefix, suffix = name_parts(name)
|
|
1038
|
+
parts = [prefix, @name_prefix.relative_join(suffix, PREFIXED_NAME_SEPARATOR)]
|
|
982
1039
|
|
|
983
|
-
|
|
984
|
-
name = prefix + PREFIXED_NAME_SEPARATOR + name if prefix
|
|
985
|
-
|
|
986
|
-
normalized_name(name)
|
|
1040
|
+
normalized_name(parts.compact.reject(&:empty?).join(PREFIXED_NAME_SEPARATOR))
|
|
987
1041
|
end
|
|
988
1042
|
|
|
989
1043
|
# Returns a [prefix, suffix] array for a given route name.
|
|
@@ -1004,28 +1058,6 @@ module Hanami
|
|
|
1004
1058
|
name.gsub(UNDERSCORED_NAME_REGEXP, "_").to_sym
|
|
1005
1059
|
end
|
|
1006
1060
|
|
|
1007
|
-
# Returns a new instance of Hanami::Router with the modified options.
|
|
1008
|
-
#
|
|
1009
|
-
# @return [Hanami::Route] a new instance of Hanami::Router
|
|
1010
|
-
#
|
|
1011
|
-
# @see Hanami::Router#initialize
|
|
1012
|
-
#
|
|
1013
|
-
# @since 2.0.0
|
|
1014
|
-
# @api private
|
|
1015
|
-
def with(**new_options, &blk)
|
|
1016
|
-
options = {
|
|
1017
|
-
base_url: @base_url,
|
|
1018
|
-
prefix: @path_prefix.to_s,
|
|
1019
|
-
resolver: @resolver,
|
|
1020
|
-
not_allowed: @not_allowed,
|
|
1021
|
-
not_found: @not_found,
|
|
1022
|
-
block_context: @block_context,
|
|
1023
|
-
inspector: @inspector
|
|
1024
|
-
}
|
|
1025
|
-
|
|
1026
|
-
self.class.new(**options.merge(new_options), &blk || @blk)
|
|
1027
|
-
end
|
|
1028
|
-
|
|
1029
1061
|
# @since 2.0.0
|
|
1030
1062
|
# @api private
|
|
1031
1063
|
def _redirect(to, code)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hanami-router
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.0.
|
|
4
|
+
version: 3.1.0.rc1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Hanakai team
|
|
@@ -79,10 +79,13 @@ files:
|
|
|
79
79
|
- lib/hanami/router/block.rb
|
|
80
80
|
- lib/hanami/router/constants.rb
|
|
81
81
|
- lib/hanami/router/errors.rb
|
|
82
|
+
- lib/hanami/router/expander.rb
|
|
83
|
+
- lib/hanami/router/expander/mustermann_expander.rb
|
|
84
|
+
- lib/hanami/router/expander/simple_expander.rb
|
|
85
|
+
- lib/hanami/router/formatter.rb
|
|
82
86
|
- lib/hanami/router/formatter/csv.rb
|
|
83
87
|
- lib/hanami/router/formatter/human_friendly.rb
|
|
84
88
|
- lib/hanami/router/globbed_path.rb
|
|
85
|
-
- lib/hanami/router/inspector.rb
|
|
86
89
|
- lib/hanami/router/leaf.rb
|
|
87
90
|
- lib/hanami/router/mounted_path.rb
|
|
88
91
|
- lib/hanami/router/node.rb
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "hanami/router/formatter/human_friendly"
|
|
4
|
-
|
|
5
|
-
module Hanami
|
|
6
|
-
class Router
|
|
7
|
-
# Builds a representation of an array of routes according to a given formatter.
|
|
8
|
-
#
|
|
9
|
-
# @see Router.new
|
|
10
|
-
#
|
|
11
|
-
# @since 2.0.0
|
|
12
|
-
# @api private
|
|
13
|
-
class Inspector
|
|
14
|
-
# @param routes [Array<Hanami::Route>]
|
|
15
|
-
# @param formatter [#call] routes formatter, taking routes as an argument and returning its
|
|
16
|
-
# own representation (typically a string). Defaults to {Formatter::HumanFriendly}.
|
|
17
|
-
#
|
|
18
|
-
# @since 2.0.0
|
|
19
|
-
# @api public
|
|
20
|
-
def initialize(routes: [], formatter: Formatter::HumanFriendly.new)
|
|
21
|
-
@routes = routes
|
|
22
|
-
@formatter = formatter
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
# Adds a route to be inspected.
|
|
26
|
-
#
|
|
27
|
-
# @param route [Route]
|
|
28
|
-
#
|
|
29
|
-
# @since 2.0.0
|
|
30
|
-
# @api public
|
|
31
|
-
def add_route(route)
|
|
32
|
-
@routes.push(route)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
# Calls the formatter for all added routes.
|
|
36
|
-
#
|
|
37
|
-
# @return [Any] Formatted routes
|
|
38
|
-
#
|
|
39
|
-
# @since 2.0.0
|
|
40
|
-
# @api public
|
|
41
|
-
def call(...)
|
|
42
|
-
@formatter.call(@routes, ...)
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
end
|