diff --git a/app/models/communication/block/template/video.rb b/app/models/communication/block/template/video.rb index de9b378752ee780e5c7aad4f68e52b2d927b1bfc..0e0612d61b86f51883bdc0ff1bf4433d9d8ac71b 100644 --- a/app/models/communication/block/template/video.rb +++ b/app/models/communication/block/template/video.rb @@ -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 diff --git a/app/services/video/provider.rb b/app/services/video/provider.rb new file mode 100644 index 0000000000000000000000000000000000000000..5b6f3fd68892b3651781a45f47d6e700b448b53a --- /dev/null +++ b/app/services/video/provider.rb @@ -0,0 +1,20 @@ +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 diff --git a/app/services/video/provider/dailymotion.rb b/app/services/video/provider/dailymotion.rb new file mode 100644 index 0000000000000000000000000000000000000000..3a66f5fa5797fd35e2b617b3e45fac6237d77bec --- /dev/null +++ b/app/services/video/provider/dailymotion.rb @@ -0,0 +1,15 @@ +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 diff --git a/app/services/video/provider/default.rb b/app/services/video/provider/default.rb new file mode 100644 index 0000000000000000000000000000000000000000..336e8e7aaa27216a09f189c9fc7afee93b43e7cb --- /dev/null +++ b/app/services/video/provider/default.rb @@ -0,0 +1,29 @@ +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 diff --git a/app/services/video/provider/vimeo.rb b/app/services/video/provider/vimeo.rb new file mode 100644 index 0000000000000000000000000000000000000000..3d10b85440226a7be369e69398803dba4cd20d8b --- /dev/null +++ b/app/services/video/provider/vimeo.rb @@ -0,0 +1,13 @@ +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 diff --git a/app/services/video/provider/youtube.rb b/app/services/video/provider/youtube.rb new file mode 100644 index 0000000000000000000000000000000000000000..e9fa299e10619d5ff763a0275c65f1a87718d92e --- /dev/null +++ b/app/services/video/provider/youtube.rb @@ -0,0 +1,15 @@ +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 diff --git a/app/views/admin/communication/blocks/templates/video/_static.html.erb b/app/views/admin/communication/blocks/templates/video/_static.html.erb index ca779a451ec5b3d8212f816dc1df3ec7933a4ea8..ad5aae3546b5a64753c64decc27f1b8154a841f5 100644 --- a/app/views/admin/communication/blocks/templates/video/_static.html.erb +++ b/app/views/admin/communication/blocks/templates/video/_static.html.erb @@ -1,3 +1,7 @@ <%= 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 %>