Skip to content

Commit 83f3623

Browse files
Routes task uses LuckyRouter (#1898)
1 parent e069903 commit 83f3623

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

shard.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ dependencies:
3838
version: ~> 0.4.0
3939
lucky_router:
4040
github: luckyframework/lucky_router
41-
version: ~> 0.5.2
41+
branch: main
4242
shell-table:
4343
github: luckyframework/shell-table.cr
4444
version: ~> 0.9.3

spec/lucky/router_spec.cr

+12
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,16 @@ describe Lucky::Router do
2323
Lucky.router.find_action(:get, "/complex_path/1/2").should_not be_nil
2424
Lucky.router.find_action(:get, "/complex_path/1").should_not be_nil
2525
end
26+
27+
describe "#list_routes" do
28+
it "returns list of routes" do
29+
Lucky.router.add :get, "/users", Lucky::Action
30+
Lucky.router.add :put, "/clients/:client_id", Lucky::Action
31+
32+
routes = Lucky.router.list_routes
33+
34+
routes.should contain({"/users", "get", Lucky::Action})
35+
routes.should contain({"/clients/:client_id", "put", Lucky::Action})
36+
end
37+
end
2638
end

src/lucky/router.cr

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# :nodoc:
22
class Lucky::Router
3-
getter routes = [] of Tuple(Symbol, String, Lucky::Action.class)
43
@matcher = LuckyRouter::Matcher(Lucky::Action.class).new
54

5+
# Array of path, method, and payload
6+
def list_routes : Array(Tuple(String, String, Lucky::Action.class))
7+
@matcher.list_routes
8+
end
9+
610
def add(method : Symbol, path : String, action : Lucky::Action.class) : Nil
7-
@routes << {method, path, action}
811
@matcher.add(method.to_s, path, action)
912
end
1013

tasks/routes.cr

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ class Routes < LuckyTask::Task
2222
def call
2323
routes = [] of Array(String)
2424

25-
Lucky.router.routes.each do |method, path, action|
25+
Lucky.router.list_routes.each do |path, method, action|
26+
# skip HEAD routes
27+
# LuckyRouter creates these routes from any GET routes submitted
28+
# This could be an issue if users define their own HEAD routes
29+
next if method.upcase == "HEAD"
30+
2631
row = [] of String
27-
row << method.to_s.upcase
32+
row << method.upcase
2833
row << path.colorize.green.to_s
2934
row << action.name
3035
routes << row

0 commit comments

Comments
 (0)