Add Danbooru::URL class.

Introduce a Danbooru::URL class for dealing with URLs. This is a wrapper
around Addressable::URI that adds some additional helper methods. Most
significantly, the `parse` method only allows valid http/https URLs, and
it returns nil instead of raising an exception when the URL is invalid.
This commit is contained in:
evazion
2022-02-21 18:37:18 -06:00
parent 60a26af6e3
commit 7d49ab6130
4 changed files with 53 additions and 11 deletions

View File

@@ -46,8 +46,8 @@ module Sources
@referer_url = referer_url&.to_s
@urls = [@url, @referer_url].select(&:present?)
@parsed_url = Addressable::URI.heuristic_parse(url) rescue nil
@parsed_referer = Addressable::URI.heuristic_parse(referer_url) rescue nil
@parsed_url = Danbooru::URL.parse(url)
@parsed_referer = Danbooru::URL.parse(referer_url)
@parsed_urls = [parsed_url, parsed_referer].select(&:present?)
end
@@ -65,7 +65,7 @@ module Sources
end
def site_name
host = Addressable::URI.heuristic_parse(url)&.host
host = parsed_url&.host
# XXX should go in dedicated strategies.
case host
@@ -134,8 +134,6 @@ module Sources
else
host
end
rescue Addressable::URI::InvalidURIError
nil
end
# Whatever <tt>url</tt> is, this method should return the direct links