2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-02-26 10:03:20 -06:00
|
|
|
module WildcardUrlChecker
|
|
|
|
def self.check_url(url, url_to_check)
|
2020-01-02 10:03:13 -06:00
|
|
|
return false if !valid_url?(url_to_check)
|
2019-12-12 20:12:12 -06:00
|
|
|
|
2019-02-26 10:03:20 -06:00
|
|
|
escaped_url = Regexp.escape(url).sub("\\*", '\S*')
|
2019-12-12 20:12:12 -06:00
|
|
|
url_regex = Regexp.new("\\A#{escaped_url}\\z", "i")
|
2019-02-26 10:03:20 -06:00
|
|
|
|
2020-01-02 10:03:13 -06:00
|
|
|
url_to_check.match?(url_regex)
|
2019-02-26 10:03:20 -06:00
|
|
|
end
|
|
|
|
|
2019-12-12 20:12:12 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def self.valid_url?(url)
|
|
|
|
uri = URI.parse(url)
|
2020-01-02 10:03:13 -06:00
|
|
|
uri&.scheme.present? && uri&.host.present?
|
2019-12-12 20:12:12 -06:00
|
|
|
rescue URI::InvalidURIError
|
|
|
|
false
|
|
|
|
end
|
2019-02-26 10:03:20 -06:00
|
|
|
end
|