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:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user