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

@@ -314,10 +314,7 @@ class Post < ApplicationRecord
def source_domain
return "" unless source =~ %r{\Ahttps?://}i
url = Addressable::URI.parse(normalized_source)
url.domain
rescue StandardError
""
Danbooru::URL.parse(normalized_source)&.domain.to_s
end
end

View File

@@ -70,8 +70,7 @@ class Upload < ApplicationRecord
class_methods do
# percent-encode unicode characters in the URL
def normalize_source(url)
return nil if url.nil?
Addressable::URI.normalized_encode(url)
Danbooru::URL.parse(url)&.to_s.presence || url
end
end
end