Skip to content

Commit d91fd7f

Browse files
committed
Allow RoutedResult to be asked about the path that matched
1 parent ae6332d commit d91fd7f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

spec/amber_router/routed_result/routed_result_spec.cr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,16 @@ describe Amber::Router::RoutedResult do
4444
result["name"].should eq "john"
4545
end
4646
end
47+
48+
context "path" do
49+
it "allows retrieving the matched path" do
50+
router = build do
51+
add "/get/users/:id", :user
52+
end
53+
54+
result = router.find("/get/users/3")
55+
result.found?.should be_true
56+
result.path.should eq "/get/users/:id"
57+
end
58+
end
4759
end

src/amber/router/routed_result.cr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ module Amber::Router
1111
@terminal_segment.not_nil!
1212
end
1313

14+
def path
15+
if @terminal_segment
16+
terminal_segment.full_path
17+
else
18+
raise "Cannot provide route path when no route was found. Ask first with #found?"
19+
end
20+
end
21+
1422
def found?
1523
! @terminal_segment.nil?
1624
end

0 commit comments

Comments
 (0)