Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use LuckyRouter update for lucky routes #1898

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading