From 83f3623e8fb85690903f0265537e171b37c286b2 Mon Sep 17 00:00:00 2001 From: Matthew McGarvey Date: Thu, 13 Jun 2024 13:56:03 -0500 Subject: [PATCH] Routes task uses LuckyRouter (#1898) --- shard.yml | 2 +- spec/lucky/router_spec.cr | 12 ++++++++++++ src/lucky/router.cr | 7 +++++-- tasks/routes.cr | 9 +++++++-- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/shard.yml b/shard.yml index 38b77da55..49665700c 100644 --- a/shard.yml +++ b/shard.yml @@ -38,7 +38,7 @@ dependencies: version: ~> 0.4.0 lucky_router: github: luckyframework/lucky_router - version: ~> 0.5.2 + branch: main shell-table: github: luckyframework/shell-table.cr version: ~> 0.9.3 diff --git a/spec/lucky/router_spec.cr b/spec/lucky/router_spec.cr index a4f021a7b..b0b51291c 100644 --- a/spec/lucky/router_spec.cr +++ b/spec/lucky/router_spec.cr @@ -23,4 +23,16 @@ describe Lucky::Router do Lucky.router.find_action(:get, "/complex_path/1/2").should_not be_nil Lucky.router.find_action(:get, "/complex_path/1").should_not be_nil end + + describe "#list_routes" do + it "returns list of routes" do + Lucky.router.add :get, "/users", Lucky::Action + Lucky.router.add :put, "/clients/:client_id", Lucky::Action + + routes = Lucky.router.list_routes + + routes.should contain({"/users", "get", Lucky::Action}) + routes.should contain({"/clients/:client_id", "put", Lucky::Action}) + end + end end diff --git a/src/lucky/router.cr b/src/lucky/router.cr index 1e98453ce..2e0e10295 100644 --- a/src/lucky/router.cr +++ b/src/lucky/router.cr @@ -1,10 +1,13 @@ # :nodoc: class Lucky::Router - getter routes = [] of Tuple(Symbol, String, Lucky::Action.class) @matcher = LuckyRouter::Matcher(Lucky::Action.class).new + # Array of path, method, and payload + def list_routes : Array(Tuple(String, String, Lucky::Action.class)) + @matcher.list_routes + end + def add(method : Symbol, path : String, action : Lucky::Action.class) : Nil - @routes << {method, path, action} @matcher.add(method.to_s, path, action) end diff --git a/tasks/routes.cr b/tasks/routes.cr index 8e14bfd2d..80ca7478a 100644 --- a/tasks/routes.cr +++ b/tasks/routes.cr @@ -22,9 +22,14 @@ class Routes < LuckyTask::Task def call routes = [] of Array(String) - Lucky.router.routes.each do |method, path, action| + Lucky.router.list_routes.each do |path, method, action| + # skip HEAD routes + # LuckyRouter creates these routes from any GET routes submitted + # This could be an issue if users define their own HEAD routes + next if method.upcase == "HEAD" + row = [] of String - row << method.to_s.upcase + row << method.upcase row << path.colorize.green.to_s row << action.name routes << row