AnApprentice
AnApprentice

Reputation: 110950

how to regex a list of url paths?

I have a list of url paths:

WHITELIST_PATHS = [ '/assets', '/images', '/javascripts']

How can regex be used to do something like:

allow_access = WHITELIST_PATHS.include? '/assets/application.css'

Idea being that the tested path just needs to start with a whitelist path. Ideas? Thanks

Upvotes: 0

Views: 278

Answers (2)

rubish
rubish

Reputation: 10907

WHITELIST_PATHS = [ '/assets', '/images', '/javascripts']
# probably should be 
# WHITELIST_PATHS = [ '/assets/', '/images/', '/javascripts/']
WHITELIST_REGEXP = /^(#{WHITELIST_PATHS.join("|")})/

allow_access = !!('/assets/application.css' =~ WHITELIST_REGEXP)

Upvotes: 1

jtbandes
jtbandes

Reputation: 118671

allow_access = WHITELIST_PATHS.any? {|p| '/assets/application.css'.start_with? p }

Upvotes: 3

Related Questions