Skip to content

Commit

Permalink
Use LuckyRouter update for lucky routes
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmcgarvey committed Jun 12, 2024
1 parent e069903 commit 47b3a6c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
6 changes: 6 additions & 0 deletions shard.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies:
# Assuming we have a conflict with the version of the Redis shard
# This will override any specified version and use the `master` branch instead
lucky_router:
github: luckyframework/lucky_router
branch: main
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

Check failure on line 7 in src/lucky/router.cr

View workflow job for this annotation

GitHub Actions / specs (shard.yml, 1.10.0, false)

undefined method 'list_routes' for LuckyRouter::Matcher(Lucky::Action.class)

Check failure on line 7 in src/lucky/router.cr

View workflow job for this annotation

GitHub Actions / specs (shard.yml, latest, false)

undefined method 'list_routes' for LuckyRouter::Matcher(Lucky::Action.class)
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 47b3a6c

Please sign in to comment.