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