hanami-router 2.0.0.alpha4 → 2.0.0.alpha5
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 +4 -0
- data/lib/hanami/router.rb +64 -2
- data/lib/hanami/router/inspector.rb +38 -0
- data/lib/hanami/router/redirect.rb +6 -1
- data/lib/hanami/router/route.rb +130 -0
- data/lib/hanami/router/version.rb +1 -1
- 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: d72d9efd8d4b3b0df52796170481b05878e531b2aee3ea09894e6eeccb5562e6
|
4
|
+
data.tar.gz: 2078b5956054db900fe4d2f097b62901499ceb1622a57baae326fa921ef62f48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5409a722ae69fc035fcbe54138ecfe504d97d9dcd62f0c3ce04a93f2fa5ca40aa833813b491a3a13d1db972e8e352e9d7e89948dfa3f522d99beafae4908e961
|
7
|
+
data.tar.gz: 55505b92589cb74795bd01a6062d32effce16a56d0405b98e726d0ba9d3b54ca301fb464cf0826fdac7e45f1de23a15913a211359c2f908117b1e972684b90d7
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# Hanami::Router
|
2
2
|
Rack compatible HTTP router for Ruby
|
3
3
|
|
4
|
+
## v2.0.0.alpha5 - 2021-05-04
|
5
|
+
### Added
|
6
|
+
- [Luca Guidi] Introduced `Hanami::Router#to_inspect` which returns a string blob with all the routes formatted for human readability
|
7
|
+
|
4
8
|
## v2.0.0.alpha4 - 2021-01-16
|
5
9
|
### Added
|
6
10
|
- [Luca Guidi] Official support for MRI 3.0
|
data/lib/hanami/router.rb
CHANGED
@@ -15,6 +15,7 @@ module Hanami
|
|
15
15
|
require "hanami/router/params"
|
16
16
|
require "hanami/router/trie"
|
17
17
|
require "hanami/router/block"
|
18
|
+
require "hanami/router/route"
|
18
19
|
require "hanami/router/url_helpers"
|
19
20
|
|
20
21
|
# URL helpers for other Hanami integrations
|
@@ -23,6 +24,12 @@ module Hanami
|
|
23
24
|
# @since 2.0.0
|
24
25
|
attr_reader :url_helpers
|
25
26
|
|
27
|
+
# Routes for inspection
|
28
|
+
#
|
29
|
+
# @api private
|
30
|
+
# @since 2.0.0
|
31
|
+
attr_reader :routes
|
32
|
+
|
26
33
|
# Returns the given block as it is.
|
27
34
|
#
|
28
35
|
# @param blk [Proc] a set of route definitions
|
@@ -61,11 +68,12 @@ module Hanami
|
|
61
68
|
# Hanami::Router.new do
|
62
69
|
# get "/", to: ->(*) { [200, {}, ["OK"]] }
|
63
70
|
# end
|
64
|
-
def initialize(base_url: DEFAULT_BASE_URL, prefix: DEFAULT_PREFIX, resolver: DEFAULT_RESOLVER, not_found: NOT_FOUND, block_context: nil, &blk) # rubocop:disable Layout/LineLength
|
71
|
+
def initialize(base_url: DEFAULT_BASE_URL, prefix: DEFAULT_PREFIX, resolver: DEFAULT_RESOLVER, not_found: NOT_FOUND, block_context: nil, inspector: nil, &blk) # rubocop:disable Layout/LineLength
|
65
72
|
# TODO: verify if Prefix can handle both name and path prefix
|
66
73
|
@path_prefix = Prefix.new(prefix)
|
67
74
|
@name_prefix = Prefix.new("")
|
68
75
|
@url_helpers = UrlHelpers.new(base_url)
|
76
|
+
@base_url = base_url
|
69
77
|
@resolver = resolver
|
70
78
|
@not_found = not_found
|
71
79
|
@block_context = block_context
|
@@ -73,6 +81,8 @@ module Hanami
|
|
73
81
|
@variable = {}
|
74
82
|
@globbed = {}
|
75
83
|
@mounted = {}
|
84
|
+
@blk = blk
|
85
|
+
@inspector = inspector
|
76
86
|
instance_eval(&blk) if blk
|
77
87
|
end
|
78
88
|
|
@@ -407,7 +417,11 @@ module Hanami
|
|
407
417
|
def mount(app, at:, **constraints)
|
408
418
|
path = prefixed_path(at)
|
409
419
|
prefix = Segment.fabricate(path, **constraints)
|
420
|
+
|
410
421
|
@mounted[prefix] = @resolver.call(path, app)
|
422
|
+
if inspect?
|
423
|
+
@inspector.add_route(Route.new(http_method: "*", path: at, to: app, constraints: constraints))
|
424
|
+
end
|
411
425
|
end
|
412
426
|
|
413
427
|
# Generate an relative URL for a specified named route.
|
@@ -588,6 +602,21 @@ module Hanami
|
|
588
602
|
)
|
589
603
|
end
|
590
604
|
|
605
|
+
# Returns formatted routes
|
606
|
+
#
|
607
|
+
# @return [String] formatted routes
|
608
|
+
#
|
609
|
+
# @since 2.0.0
|
610
|
+
# @api private
|
611
|
+
def to_inspect
|
612
|
+
require "hanami/router/inspector"
|
613
|
+
|
614
|
+
inspector = Inspector.new
|
615
|
+
with(inspector: inspector)
|
616
|
+
|
617
|
+
inspector.call
|
618
|
+
end
|
619
|
+
|
591
620
|
# @since 2.0.0
|
592
621
|
# @api private
|
593
622
|
def fixed(env)
|
@@ -728,6 +757,12 @@ module Hanami
|
|
728
757
|
end
|
729
758
|
|
730
759
|
add_named_route(path, as, constraints) if as
|
760
|
+
|
761
|
+
if inspect?
|
762
|
+
@inspector.add_route(
|
763
|
+
Route.new(http_method: http_method, path: path, to: to, as: as, constraints: constraints, blk: blk)
|
764
|
+
)
|
765
|
+
end
|
731
766
|
end
|
732
767
|
|
733
768
|
# @since 2.0.0
|
@@ -778,6 +813,12 @@ module Hanami
|
|
778
813
|
/\*/.match?(path)
|
779
814
|
end
|
780
815
|
|
816
|
+
# @since 2.0.0
|
817
|
+
# @api private
|
818
|
+
def inspect?
|
819
|
+
!@inspector.nil?
|
820
|
+
end
|
821
|
+
|
781
822
|
# @since 2.0.0
|
782
823
|
# @api private
|
783
824
|
def prefixed_path(path)
|
@@ -790,6 +831,27 @@ module Hanami
|
|
790
831
|
@name_prefix.relative_join(name, "_").to_sym
|
791
832
|
end
|
792
833
|
|
834
|
+
# Returns a new instance of Hanami::Router with the modified options.
|
835
|
+
#
|
836
|
+
# @return [Hanami::Route] a new instance of Hanami::Router
|
837
|
+
#
|
838
|
+
# @see Hanami::Router#initialize
|
839
|
+
#
|
840
|
+
# @since 2.0.0
|
841
|
+
# @api private
|
842
|
+
def with(**new_options, &blk)
|
843
|
+
options = {
|
844
|
+
base_url: @base_url,
|
845
|
+
prefix: @path_prefix.to_s,
|
846
|
+
resolver: @resolver,
|
847
|
+
not_found: @not_found,
|
848
|
+
block_context: @block_context,
|
849
|
+
inspector: @inspector
|
850
|
+
}
|
851
|
+
|
852
|
+
self.class.new(**options.merge(new_options), &(blk || @blk))
|
853
|
+
end
|
854
|
+
|
793
855
|
# @since 2.0.0
|
794
856
|
# @api private
|
795
857
|
def _redirect(to, code)
|
@@ -798,7 +860,7 @@ module Hanami
|
|
798
860
|
end
|
799
861
|
|
800
862
|
destination = prefixed_path(to)
|
801
|
-
Redirect.new(destination, ->(*) { [code, {"Location" => destination}, [body]] })
|
863
|
+
Redirect.new(destination, code, ->(*) { [code, {"Location" => destination}, [body]] })
|
802
864
|
end
|
803
865
|
|
804
866
|
# @since 2.0.0
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hanami
|
4
|
+
class Router
|
5
|
+
# Routes inspector
|
6
|
+
#
|
7
|
+
# @api private
|
8
|
+
# @since 2.0.0
|
9
|
+
class Inspector
|
10
|
+
# @api private
|
11
|
+
# @since 2.0.0
|
12
|
+
def initialize(routes: [])
|
13
|
+
@routes = routes
|
14
|
+
end
|
15
|
+
|
16
|
+
# @param route [Hash] serialized route
|
17
|
+
#
|
18
|
+
# @api private
|
19
|
+
# @since 2.0.0
|
20
|
+
def add_route(route)
|
21
|
+
@routes.push(route)
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [String] The inspected routes
|
25
|
+
#
|
26
|
+
# @api private
|
27
|
+
# @since 2.0.0
|
28
|
+
def call(*)
|
29
|
+
@routes.map(&:to_inspect).join(NEW_LINE)
|
30
|
+
end
|
31
|
+
|
32
|
+
# @api private
|
33
|
+
# @since 2.0.0
|
34
|
+
NEW_LINE = $/
|
35
|
+
private_constant :NEW_LINE
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -13,8 +13,13 @@ module Hanami
|
|
13
13
|
|
14
14
|
# @since 2.0.0
|
15
15
|
# @api private
|
16
|
-
|
16
|
+
attr_reader :code
|
17
|
+
|
18
|
+
# @since 2.0.0
|
19
|
+
# @api private
|
20
|
+
def initialize(destination, code, endpoint)
|
17
21
|
@destination = destination
|
22
|
+
@code = code
|
18
23
|
@endpoint = endpoint
|
19
24
|
end
|
20
25
|
|
@@ -0,0 +1,130 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hanami/router/redirect"
|
4
|
+
require "hanami/router/block"
|
5
|
+
|
6
|
+
module Hanami
|
7
|
+
class Router
|
8
|
+
class Route
|
9
|
+
# @api private
|
10
|
+
# @since 2.0.0
|
11
|
+
attr_reader :http_method
|
12
|
+
|
13
|
+
# @api private
|
14
|
+
# @since 2.0.0
|
15
|
+
attr_reader :path
|
16
|
+
|
17
|
+
# @api private
|
18
|
+
# @since 2.0.0
|
19
|
+
attr_reader :to
|
20
|
+
|
21
|
+
# @api private
|
22
|
+
# @since 2.0.0
|
23
|
+
attr_reader :as
|
24
|
+
|
25
|
+
# @api private
|
26
|
+
# @since 2.0.0
|
27
|
+
attr_reader :constraints
|
28
|
+
|
29
|
+
# @api private
|
30
|
+
# @since 2.0.0
|
31
|
+
def initialize(http_method:, path:, to:, as: nil, constraints: {}, blk: nil) # rubocop:disable Metrics/ParameterLists
|
32
|
+
@http_method = http_method
|
33
|
+
@path = path
|
34
|
+
@to = to
|
35
|
+
@as = as
|
36
|
+
@constraints = constraints
|
37
|
+
@blk = blk
|
38
|
+
freeze
|
39
|
+
end
|
40
|
+
|
41
|
+
# @api private
|
42
|
+
# @since 2.0.0
|
43
|
+
def to_inspect
|
44
|
+
return EMPTY_ROUTE if head?
|
45
|
+
|
46
|
+
result = http_method.to_s.ljust(SMALL_STRING_JUSTIFY_AMOUNT)
|
47
|
+
result += path.ljust(LARGE_STRING_JUSTIFY_AMOUNT)
|
48
|
+
result += inspect_to(to).ljust(LARGE_STRING_JUSTIFY_AMOUNT)
|
49
|
+
result += "as #{as.inspect}".ljust(MEDIUM_STRING_JUSTIFY_AMOUNT) if as
|
50
|
+
|
51
|
+
if constraints?
|
52
|
+
result += "(#{inspect_constraints(constraints)})".ljust(EXTRA_LARGE_STRING_JUSTIFY_AMOUNT)
|
53
|
+
end
|
54
|
+
|
55
|
+
result
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# @api private
|
61
|
+
# @since 2.0.0
|
62
|
+
EMPTY_ROUTE = ""
|
63
|
+
private_constant :EMPTY_ROUTE
|
64
|
+
|
65
|
+
# @api private
|
66
|
+
# @since 2.0.0
|
67
|
+
ROUTE_CONSTRAINT_SEPARATOR = ", "
|
68
|
+
private_constant :ROUTE_CONSTRAINT_SEPARATOR
|
69
|
+
|
70
|
+
# @api private
|
71
|
+
# @since 2.0.0
|
72
|
+
SMALL_STRING_JUSTIFY_AMOUNT = 8
|
73
|
+
private_constant :SMALL_STRING_JUSTIFY_AMOUNT
|
74
|
+
|
75
|
+
# @api private
|
76
|
+
# @since 2.0.0
|
77
|
+
MEDIUM_STRING_JUSTIFY_AMOUNT = 20
|
78
|
+
private_constant :MEDIUM_STRING_JUSTIFY_AMOUNT
|
79
|
+
|
80
|
+
# @api private
|
81
|
+
# @since 2.0.0
|
82
|
+
LARGE_STRING_JUSTIFY_AMOUNT = 30
|
83
|
+
private_constant :LARGE_STRING_JUSTIFY_AMOUNT
|
84
|
+
|
85
|
+
# @api private
|
86
|
+
# @since 2.0.0
|
87
|
+
EXTRA_LARGE_STRING_JUSTIFY_AMOUNT = 40
|
88
|
+
private_constant :EXTRA_LARGE_STRING_JUSTIFY_AMOUNT
|
89
|
+
|
90
|
+
# @api private
|
91
|
+
# @since 2.0.0
|
92
|
+
def head?
|
93
|
+
http_method == "HEAD"
|
94
|
+
end
|
95
|
+
|
96
|
+
# @api private
|
97
|
+
# @since 2.0.0
|
98
|
+
def constraints?
|
99
|
+
constraints.any?
|
100
|
+
end
|
101
|
+
|
102
|
+
# @api private
|
103
|
+
# @since 2.0.0
|
104
|
+
def inspect_to(to)
|
105
|
+
case to
|
106
|
+
when String
|
107
|
+
to
|
108
|
+
when Proc
|
109
|
+
"(proc)"
|
110
|
+
when Class
|
111
|
+
to.name || "(class)"
|
112
|
+
when Block
|
113
|
+
"(block)"
|
114
|
+
when Redirect
|
115
|
+
"#{to.destination} (HTTP #{to.code})"
|
116
|
+
else
|
117
|
+
inspect_to(to.class)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# @api private
|
122
|
+
# @since 2.0.0
|
123
|
+
def inspect_constraints(constraints)
|
124
|
+
constraints.map do |key, value|
|
125
|
+
"#{key}: #{value.inspect}"
|
126
|
+
end.join(ROUTE_CONSTRAINT_SEPARATOR)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
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.0.0.
|
4
|
+
version: 2.0.0.alpha5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luca Guidi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -162,11 +162,13 @@ files:
|
|
162
162
|
- lib/hanami/router.rb
|
163
163
|
- lib/hanami/router/block.rb
|
164
164
|
- lib/hanami/router/error.rb
|
165
|
+
- lib/hanami/router/inspector.rb
|
165
166
|
- lib/hanami/router/node.rb
|
166
167
|
- lib/hanami/router/params.rb
|
167
168
|
- lib/hanami/router/prefix.rb
|
168
169
|
- lib/hanami/router/recognized_route.rb
|
169
170
|
- lib/hanami/router/redirect.rb
|
171
|
+
- lib/hanami/router/route.rb
|
170
172
|
- lib/hanami/router/segment.rb
|
171
173
|
- lib/hanami/router/trie.rb
|
172
174
|
- lib/hanami/router/url_helpers.rb
|