Reputation: 1886
In a Rails app I need to identify certain URLs paths that are expressed in this form:
paths = ['/a/*/b/c', '/f/*']
I'm in a middleware and I have access to the path, what kind of conditional should I write to check if the current URL path has a match with an entry in the provided array?
path = Rack::Request.new(env).path
included = paths.any? { |s| path.include?(s) }
this only checks for inclusion, but now wildcards have arrived.
Upvotes: 1
Views: 52
Reputation: 369444
You can convert paths into regular expression patterns:
paths = ['/a/*/b/c', '/f/*']
path = Rack::Request.new(env).path
patterns = paths.map { |path| Regexp.new path.gsub(/\*/, '[^/]+') }
# Convert `*` to `[^/]+` to match any non-`/` characters.
included = patterns.any? { |s| path =~ s }
Upvotes: 2