Skip to content

Commit 47b3a6c

Browse files
Use LuckyRouter update for lucky routes
1 parent e069903 commit 47b3a6c

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

shard.override.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dependencies:
2+
# Assuming we have a conflict with the version of the Redis shard
3+
# This will override any specified version and use the `master` branch instead
4+
lucky_router:
5+
github: luckyframework/lucky_router
6+
branch: main

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)