From 6e6dd66aec4d3ea19137855525d03469bc7a8621 Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos <81665687568@serpro-1541727.(none)> Date: Tue, 1 Oct 2013 20:04:47 -0300 Subject: [PATCH] display embed url's for youtube and vimeo --- plugins/video/lib/.video_block.rb.swp | Bin 12288 -> 0 bytes plugins/video/lib/video_block.rb | 50 +++++++++++++++++++++++++++++++------------------- plugins/video/test/unit/video_block_test.rb | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------- plugins/video/views/video_block.rhtml | 12 ++++++------ 4 files changed, 107 insertions(+), 57 deletions(-) delete mode 100644 plugins/video/lib/.video_block.rb.swp diff --git a/plugins/video/lib/.video_block.rb.swp b/plugins/video/lib/.video_block.rb.swp deleted file mode 100644 index f0515d9..0000000 Binary files a/plugins/video/lib/.video_block.rb.swp and /dev/null differ diff --git a/plugins/video/lib/video_block.rb b/plugins/video/lib/video_block.rb index d4ec929..0e0335c 100644 --- a/plugins/video/lib/video_block.rb +++ b/plugins/video/lib/video_block.rb @@ -3,43 +3,40 @@ class VideoBlock < Block settings_items :url, :type => :string, :default => "" settings_items :width, :type => :integer, :default => 400 settings_items :height, :type => :integer, :default => 315 - - def is_valid_source? - - if url.match(/.*youtu(.be|be.com).*v=[[:alnum:]].*/) - true - elsif url.match(/^(http[s]?:\/\/)?(www.)?(vimeo.com|player.vimeo.com\/video)\/[[:digit:]].*/) - true - else - false - end + + def is_youtube? + url.match(/.*(youtube.com.*v=[[:alnum:]]*|youtu.be\/[[:alnum:]]*).*/) ? true : false end - def is_file? - false + def is_vimeo? + url.match(/^(http[s]?:\/\/)?(www.)?(vimeo.com|player.vimeo.com\/video)\/[[:digit:]]*/) ? true : false + end - extensions = [".mp4", ".ogg", ".ogv", ".wmv"] - if extensions.include? url[-4, 4] - true - end + def is_video_file? + url.match(/.*(mp4|ogg|ogv|webm)$/) ? true : false end + #FIXME Make this test def format_embed_video_url_for_youtube - self.url.gsub("watch?v=", "embed/") + "//www.youtube-nocookie.com/embed/#{extract_youtube_id}?rel=0&wmode=transparent" end + #FIXME Make this test def format_embed_video_url_for_vimeo - self.url.gsub("vimeo.com/", "player.vimeo.com/video/") + "//player.vimeo.com/video/#{extract_vimeo_id}" end + #FIXME Make this test def self.description _('Add Video') end + #FIXME Make this test def help _('This block presents a video block.') end + #FIXME Make this test def content(args={}) block = self @@ -47,10 +44,25 @@ class VideoBlock < Block render :file => 'video_block', :locals => { :block => block } end end - + #FIXME Make this test def cacheable? false end + + private + + #FIXME Make this test + def extract_youtube_id + return nil unless is_youtube? + youtube_match = url.match('v=([[:alnum:]]*)') + youtube_match ||= url.match('youtu.be\/([[:alnum:]]*)') + youtube_match[1] unless youtube_match.nil? + end + def extract_vimeo_id + return nil unless is_vimeo? + vimeo_match = url.match('([[:digit:]]*)$') + vimeo_match[1] unless vimeo_match.nil? + end end diff --git a/plugins/video/test/unit/video_block_test.rb b/plugins/video/test/unit/video_block_test.rb index 5525476..87104e6 100644 --- a/plugins/video/test/unit/video_block_test.rb +++ b/plugins/video/test/unit/video_block_test.rb @@ -3,101 +3,139 @@ class VideoBlockTest < ActiveSupport::TestCase ### Tests for YouTube - should "is_valid_source return true when the url contains http://youtube.com" do + should "is_youtube return true when the url contains http://youtube.com" do block = VideoBlock.new block.url = "http://youtube.com/?v=XXXXX" - assert block.is_valid_source? + assert block.is_youtube? end - should "is_valid_source return true when the url contains https://youtube.com" do + should "is_youtube return true when the url contains https://youtube.com" do block = VideoBlock.new block.url = "https://youtube.com/?v=XXXXX" - assert block.is_valid_source? + assert block.is_youtube? end - should "is_valid_source return true when the url contains https://www.youtube.com" do + should "is_youtube return true when the url contains https://www.youtube.com" do block = VideoBlock.new block.url = "https://www.youtube.com/?v=XXXXX" - assert block.is_valid_source? + assert block.is_youtube? end - should "is_valid_source return true when the url contains www.youtube.com" do + should "is_youtube return true when the url contains www.youtube.com" do block = VideoBlock.new block.url = "www.youtube.com/?v=XXXXX" - assert block.is_valid_source? + assert block.is_youtube? end - should "is_valid_source return true when the url contains youtube.com" do + should "is_youtube return true when the url contains youtube.com" do block = VideoBlock.new block.url = "youtube.com/?v=XXXXX" - assert block.is_valid_source? + assert block.is_youtube? end - should "is_valid_source return false when the url not contains youtube video ID" do + should "is_youtube return false when the url not contains youtube video ID" do block = VideoBlock.new block.url = "youtube.com/" - assert !block.is_valid_source? + assert !block.is_youtube? end - should "is_valid_source return false when the url contains empty youtube video ID" do + should "is_youtube return false when the url contains empty youtube video ID" do block = VideoBlock.new block.url = "youtube.com/?v=" - assert !block.is_valid_source? + assert !block.is_youtube? end - should "is_valid_source return false when the url contains an invalid youtube link" do + should "is_youtube return false when the url contains an invalid youtube link" do block = VideoBlock.new block.url = "http://www.yt.com/?v=XXXXX" - assert !block.is_valid_source? + assert !block.is_youtube? end #### Tests for Vimeo Videos - should "is_valid_source return true when the url contains http://vimeo.com" do + should "is_vimeo return true when the url contains http://vimeo.com" do block = VideoBlock.new block.url = "http://vimeo.com/98979" - assert block.is_valid_source? + assert block.is_vimeo? end - should "is_valid_source return true when the url contains https://vimeo.com" do + should "is_vimeo return true when the url contains https://vimeo.com" do block = VideoBlock.new block.url = "https://vimeo.com/989798" - assert block.is_valid_source? + assert block.is_vimeo? end - should "is_valid_source return true when the url contains https://www.vimeo.com" do + should "is_vimeo return true when the url contains https://www.vimeo.com" do block = VideoBlock.new block.url = "https://www.vimeo.com/98987" - assert block.is_valid_source? + assert block.is_vimeo? end - should "is_valid_source return true when the url contains www.vimeo.com" do + should "is_vimeo return true when the url contains www.vimeo.com" do block = VideoBlock.new block.url = "www.vimeo.com/989798" - assert block.is_valid_source? + assert block.is_vimeo? end - should "is_valid_source return true when the url contains vimeo.com" do + should "is_vimeo return true when the url contains vimeo.com" do block = VideoBlock.new block.url = "vimeo.com/09898" - assert block.is_valid_source? + assert block.is_vimeo? end - should "is_valid_source return false when the url not contains vimeo video ID" do + should "is_vimeo return false when the url not contains vimeo video ID" do block = VideoBlock.new block.url = "vimeo.com/home" - assert !block.is_valid_source? + assert !block.is_vimeo? end - should "is_valid_source return false when the url contains empty vimeo video ID" do + should "is_vimeo return false when the url contains empty vimeo video ID" do block = VideoBlock.new block.url = "vimeo.com/" - assert !block.is_valid_source? + assert !block.is_vimeo? end - should "is_valid_source return false when the url contains an invalid vimeo link" do + should "is_vimeo return false when the url contains an invalid vimeo link" do block = VideoBlock.new block.url = "http://www.vmsd.com/98979" - assert !block.is_valid_source? + assert !block.is_vimeo? end + + # Other video formats + should "is_video return true if url ends with mp4" do + block = VideoBlock.new + block.url = "http://www.vmsd.com/98979.mp4" + assert block.is_video_file? + end + + should "is_video return true if url ends with ogg" do + block = VideoBlock.new + block.url = "http://www.vmsd.com/98979.ogg" + assert block.is_video_file? + end + + should "is_video return true if url ends with ogv" do + block = VideoBlock.new + block.url = "http://www.vmsd.com/98979.ogv" + assert block.is_video_file? + end + + should "is_video return true if url ends with webm" do + block = VideoBlock.new + block.url = "http://www.vmsd.com/98979.webm" + assert block.is_video_file? + end + + should "is_video return false if url ends without mp4, ogg, ogv, webm" do + block = VideoBlock.new + block.url = "http://www.vmsd.com/98979.mp4r" + assert !block.is_video_file? + block.url = "http://www.vmsd.com/98979.oggr" + assert !block.is_video_file? + block.url = "http://www.vmsd.com/98979.ogvr" + assert !block.is_video_file? + block.url = "http://www.vmsd.com/98979.webmr" + assert !block.is_video_file? + end + end diff --git a/plugins/video/views/video_block.rhtml b/plugins/video/views/video_block.rhtml index 7777174..e799b0f 100644 --- a/plugins/video/views/video_block.rhtml +++ b/plugins/video/views/video_block.rhtml @@ -1,15 +1,15 @@

- <%=block.title%> + <%=block.title%>

<% if block.is_youtube? %> - <%= render :partial => 'iframe_video_block', :locals => { :url => block.format_embed_video_url_for_youtube, :width => block.width, :height => block.height }%> + <%= render :partial => 'iframe_video_block', :locals => { :url => block.format_embed_video_url_for_youtube, :width => block.width, :height => block.height }%> <% elsif block.is_vimeo? %> - <%= render :partial => 'iframe_video_block', :locals => { :url => block.format_embed_video_url_for_vimeo, :width => block.width, :height => block.height }%> - <% elsif block.is_file? %> - <%= render :partial => 'html5_video_block', :locals => { :url => block.url, :width => block.width, :height => block.height }%> + <%= render :partial => 'iframe_video_block', :locals => { :url => block.format_embed_video_url_for_vimeo, :width => block.width, :height => block.height }%> + <% elsif block.is_video_file? %> + <%= render :partial => 'html5_video_block', :locals => { :url => block.url, :width => block.width, :height => block.height }%> <% else %> - <%= " Cadastre uma nova URL (Vimeo, Youtube)" %> + <%= " Cadastre uma nova URL (Vimeo, Youtube)" %> <% end %>
-- libgit2 0.21.2