Skip to content
Snippets Groups Projects
Unverified Commit b85a7e4f authored by Sébastien Gaya's avatar Sébastien Gaya
Browse files

Merge branch 'main' into license-block

parents 5b2164c6 02711667
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::Dailymotion < Video::Provider::Default
DOMAINS = ['dailymotion.com', 'dai.ly']
# "https://www.dailymotion.com/video/x35l6b8"
# "https://dai.ly/x35l6b8"
def identifier
video_url.include?('dai.ly') ? video_url.split('dai.ly/').last
: video_url.split('video/').last
......
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
class Video::Provider::Vimeo < Video::Provider::Default
DOMAINS = ['vimeo.com']
# "https://vimeo.com/248482251"
def identifier
video_url.chomp('/').split('/').last
end
......
class Video::Provider::Youtube < Video::Provider::Default
DOMAINS = ['youtube.com', 'youtu.be']
# "https://www.youtube.com/watch?v=sN8Cq5HEBug"
# "https://youtu.be/sN8Cq5HEBug"
def identifier
video_url.include?('youtu.be') ? video_url.split('youtu.be/').last
: video_url.split('v=').last
......
require "test_helper"
class Video::ProviderTest < ActiveSupport::TestCase
def test_provider_empty
provider = Video::Provider.find('')
assert_equal Video::Provider::Default, provider.class
end
def test_vimeo
provider = Video::Provider.find('https://vimeo.com/248482251')
assert_equal Video::Provider::Vimeo, provider.class
end
def test_youtube
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
def test_dailymotion
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
def test_peertube
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