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

Simplify route matchers #9

Merged
merged 1 commit into from
Dec 14, 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 lib/strong_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "strong_routes/allow"
require "strong_routes/config"
require "strong_routes/route_matcher"
require "strong_routes/matcher"
require "strong_routes/route_mapper"
require "strong_routes/version"

Expand Down
2 changes: 1 addition & 1 deletion lib/strong_routes/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize
def allowed_routes=(value)
raise TypeError, "allowed routes must be an Enumerable" unless value.is_a?(Enumerable)
@allowed_routes = value.to_set
@route_matchers = allowed_routes.map { |route| StrongRoutes::RouteMatcher.new(route) }
@route_matchers = allowed_routes.map { |route| StrongRoutes::Matcher.new(route) }
end

def enabled?
Expand Down
16 changes: 16 additions & 0 deletions lib/strong_routes/matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module StrongRoutes
class Matcher < Regexp
def initialize(route)
if route.is_a?(Regexp)
super
else
escaped_route = Regexp.escape(route)
# Replace dynamic segments in the route with wildcards (e.g. /:foo/users/:id becomes /.*/users/.*)
escaped_route.gsub!(/:\w+/, ".*")
super(/\A#{escaped_route}/i)
end
end
end
end
23 changes: 0 additions & 23 deletions lib/strong_routes/route_matcher.rb

This file was deleted.

5 changes: 5 additions & 0 deletions test/strong_routes/allow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
_(last_response).must_be :ok?
end

it "allows access to /users?stuff=12" do
get "/users/?stuff=12"
_(last_response).must_be :ok?
end

it "does not allow access to /user" do
get "/user"
_(last_response).wont_be :ok?
Expand Down
2 changes: 1 addition & 1 deletion test/strong_routes/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
describe "#allowed_routes=" do
it "maps allowed routes to matchers" do
subject.allowed_routes = ["/users"]
_(subject.route_matchers).must_equal [StrongRoutes::RouteMatcher.new("/users")]
_(subject.route_matchers).must_equal [StrongRoutes::Matcher.new("/users")]
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
require "test_helper"

describe StrongRoutes::RouteMatcher do
describe StrongRoutes::Matcher do
describe "initialize" do
context "when given a regex" do
let(:matcher) { /foo/ }

subject { StrongRoutes::RouteMatcher.new(matcher) }
subject { StrongRoutes::Matcher.new(matcher) }

it "uses the regex as the matcher" do
_(subject).must_equal matcher
end
end

context "when given a symbol" do
let(:matcher) { /\A\/foo/i }

subject { StrongRoutes::RouteMatcher.new(:foo) }

it "creates a new matcher" do
_(subject).must_equal matcher
end
end

context "when given a string" do
let(:matcher) { /\A\/foo/i }

subject { StrongRoutes::RouteMatcher.new("foo") }
subject { StrongRoutes::Matcher.new("/foo") }

it "creates a new matcher" do
_(subject).must_equal matcher
Expand All @@ -35,7 +25,7 @@
context "when given a string with dynamic segments" do
let(:matcher) { /\A\/.*\/foo\/.*\/bar/i }

subject { StrongRoutes::RouteMatcher.new(":id/foo/:foo_id/bar") }
subject { StrongRoutes::Matcher.new("/:id/foo/:foo_id/bar") }

it "creates a new matcher" do
_(subject).must_equal matcher
Expand Down
Loading