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