Skip to content
Snippets Groups Projects
Commit da651ace authored by Arnaud Levy's avatar Arnaud Levy
Browse files

better peertube detection

parent 7997206e
No related branches found
No related tags found
No related merge requests found
......@@ -3,19 +3,14 @@ class Video::Provider
Vimeo,
Youtube,
Dailymotion,
Peertube
Peertube # Comes last because detection is less reliable
]
def self.find(video_url)
PROVIDERS.each do |provider|
return provider.new(video_url) if url_in_domains?(video_url, provider::DOMAINS)
PROVIDERS.each do |provider_class|
provider = provider_class.new(video_url)
return provider if provider.correct?
end
Default.new(video_url)
end
protected
def self.url_in_domains?(url, domains)
domains.any? { |domain| url.include? domain }
end
end
class Video::Provider::Default
DOMAINS = []
attr_reader :video_url
include ActionView::Helpers::TagHelper
......@@ -19,6 +21,10 @@ class Video::Provider::Default
content_tag(:iframe, nil, default_iframe_options.merge(iframe_options))
end
def correct?
url_in_domains?
end
def default_iframe_options
{
class: (platform == :default ? nil : platform),
......@@ -26,4 +32,12 @@ class Video::Provider::Default
src: iframe_url
}
end
protected
def url_in_domains?
self.class::DOMAINS.any? do |domain|
video_url.include?(domain)
end
end
end
class Video::Provider::Peertube < Video::Provider::Default
DOMAINS = ['peertube.fr', 'peertude.my.noesya.coop']
DOMAINS = ['peertube.fr']
# "https://peertube.fr/w/1i848Qvi7Q3ytW2uPY8AxG"
def identifier
video_url.split('/w/').last
end
......@@ -14,4 +13,14 @@ class Video::Provider::Peertube < Video::Provider::Default
def iframe_url
"#{host}/videos/embed/#{identifier}"
end
def correct?
url_in_domains? || url_looks_like_peertube?
end
protected
def url_looks_like_peertube?
"/w/".in?(video_url) || "/videos/watch/".in?(video_url)
end
end
require "test_helper"
class Video::ProviderTest < ActiveSupport::TestCase
test "provider empty" do
provider = Video::Provider.find('')
assert_equal Video::Provider::Default, provider.class
end
test "vimeo" do
provider = Video::Provider.find('https://vimeo.com/248482251')
assert_equal Video::Provider::Vimeo, provider.class
end
test "youtube" do
provider = Video::Provider.find('https://www.youtube.com/watch?v=sN8Cq5HEBug')
assert_equal Video::Provider::Youtube, provider.class
provider = Video::Provider.find('https://youtu.be/sN8Cq5HEBug')
assert_equal Video::Provider::Youtube, provider.class
end
test "dailymotion" do
provider = Video::Provider.find('https://www.dailymotion.com/video/x35l6b8')
assert_equal Video::Provider::Dailymotion, provider.class
provider = Video::Provider.find('https://dai.ly/x35l6b8')
assert_equal Video::Provider::Dailymotion, provider.class
end
test "peertube" do
provider = Video::Provider.find('https://peertube.fr/w/1i848Qvi7Q3ytW2uPY8AxG')
assert_equal Video::Provider::Peertube, provider.class
provider = Video::Provider.find('https://peertube.my.noesya.coop/w/qBMwAAULLA9oadFgbtdyq8')
assert_equal Video::Provider::Peertube, provider.class
end
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment