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

iframe video

parent 62a2190d
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,10 @@ class Communication::Block::Template::Video < Communication::Block::Template::Ba
has_component :video_title, :string
has_component :transcription, :text
def video_iframe
Video::Provider.find(url).iframe_tag(title: video_title)
end
protected
def check_accessibility
......
class Video::Provider
PROVIDERS = [
Vimeo,
Youtube,
Dailymotion
]
def self.find(video_url)
PROVIDERS.each do |provider|
return provider.new(video_url) if url_in_domains?(video_url, provider::DOMAINS)
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
end
# https://developer.dailymotion.com/player#player-parameters
def iframe_url
"https://www.dailymotion.com/embed/video/#{identifier}"
end
end
class Video::Provider::Default
attr_reader :video_url
include ActionView::Helpers::TagHelper
def initialize(video_url)
@video_url = video_url
end
def platform
self.class.name.demodulize.downcase.to_sym
end
def iframe_url
video_url
end
def iframe_tag(**iframe_options)
content_tag(:iframe, nil, default_iframe_options.merge(iframe_options))
end
def default_iframe_options
{
class: (platform == :default ? nil : platform),
loading: 'lazy',
src: iframe_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
# https://help.vimeo.com/hc/en-us/articles/360001494447-Using-Player-Parameters
def iframe_url
"https://player.vimeo.com/video/#{identifier}"
end
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
end
# https://developers.google.com/youtube/player_parameters
def iframe_url
"https://www.youtube.com/embed/#{identifier}"
end
end
<%= block_component_static :url %>
<%= block_component_static :video_title %>
<% if block.template.url.present? %>
video_iframe: >-
<%= block.template.video_iframe %>
<% end %>
<%= block_component_static :transcription %>
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