Refactor source strategies to remove the `canonical_url` method. `canonical_url` returned the URL that should be used as the source of the post after upload. Now we simply use `Source::URL#page_url` to determine the source after upload. If the source is an image URL that is convertible to a page URL, then the image URL is used as the source. If the source is an image URL that is not convertible to a page URL, then the page URL is used as the source. This simplifies source strategies so that all they have to care about is implementing the `Source::URL#page_url` and `Sources::Strategies#page_url` methods, and the preferred source will be chosen for posts automatically.
36 lines
901 B
Ruby
36 lines
901 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Sources
|
|
module Strategies
|
|
def self.all
|
|
[
|
|
Strategies::Pixiv,
|
|
Strategies::Twitter,
|
|
Strategies::Tumblr,
|
|
Strategies::NicoSeiga,
|
|
Strategies::DeviantArt,
|
|
Strategies::Moebooru,
|
|
Strategies::Nijie,
|
|
Strategies::ArtStation,
|
|
Strategies::HentaiFoundry,
|
|
Strategies::Fanbox,
|
|
Strategies::Mastodon,
|
|
Strategies::PixivSketch,
|
|
Strategies::Weibo,
|
|
Strategies::Newgrounds,
|
|
Strategies::Skeb,
|
|
Strategies::Lofter,
|
|
Strategies::Foundation,
|
|
Strategies::Plurk,
|
|
Strategies::Tinami,
|
|
Strategies::Fantia,
|
|
]
|
|
end
|
|
|
|
def self.find(url, referer = nil, default: Strategies::Null)
|
|
strategy = all.lazy.map { |s| s.new(url, referer) }.detect(&:match?)
|
|
strategy || default&.new(url, referer)
|
|
end
|
|
end
|
|
end
|