Skip to content

Commit

Permalink
Routes task uses LuckyRouter (#1898)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmcgarvey authored Jun 13, 2024
1 parent e069903 commit 83f3623
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/lucky/router_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions src/lucky/router.cr
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 7 additions & 2 deletions tasks/routes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 83f3623

Please sign in to comment.