Commit 6e6dd66aec4d3ea19137855525d03469bc7a8621

Authored by Leandro Nunes dos Santos
1 parent 5b502e59

display embed url's for youtube and vimeo

plugins/video/lib/.video_block.rb.swp
No preview for this file type
plugins/video/lib/video_block.rb
@@ -3,43 +3,40 @@ class VideoBlock < Block @@ -3,43 +3,40 @@ class VideoBlock < Block
3 settings_items :url, :type => :string, :default => "" 3 settings_items :url, :type => :string, :default => ""
4 settings_items :width, :type => :integer, :default => 400 4 settings_items :width, :type => :integer, :default => 400
5 settings_items :height, :type => :integer, :default => 315 5 settings_items :height, :type => :integer, :default => 315
6 -  
7 - def is_valid_source?  
8 -  
9 - if url.match(/.*youtu(.be|be.com).*v=[[:alnum:]].*/)  
10 - true  
11 - elsif url.match(/^(http[s]?:\/\/)?(www.)?(vimeo.com|player.vimeo.com\/video)\/[[:digit:]].*/)  
12 - true  
13 - else  
14 - false  
15 - end 6 +
  7 + def is_youtube?
  8 + url.match(/.*(youtube.com.*v=[[:alnum:]]*|youtu.be\/[[:alnum:]]*).*/) ? true : false
16 end 9 end
17 10
18 - def is_file?  
19 - false 11 + def is_vimeo?
  12 + url.match(/^(http[s]?:\/\/)?(www.)?(vimeo.com|player.vimeo.com\/video)\/[[:digit:]]*/) ? true : false
  13 + end
20 14
21 - extensions = [".mp4", ".ogg", ".ogv", ".wmv"]  
22 - if extensions.include? url[-4, 4]  
23 - true  
24 - end 15 + def is_video_file?
  16 + url.match(/.*(mp4|ogg|ogv|webm)$/) ? true : false
25 end 17 end
26 18
  19 + #FIXME Make this test
27 def format_embed_video_url_for_youtube 20 def format_embed_video_url_for_youtube
28 - self.url.gsub("watch?v=", "embed/") 21 + "//www.youtube-nocookie.com/embed/#{extract_youtube_id}?rel=0&wmode=transparent"
29 end 22 end
30 23
  24 + #FIXME Make this test
31 def format_embed_video_url_for_vimeo 25 def format_embed_video_url_for_vimeo
32 - self.url.gsub("vimeo.com/", "player.vimeo.com/video/") 26 + "//player.vimeo.com/video/#{extract_vimeo_id}"
33 end 27 end
34 28
  29 + #FIXME Make this test
35 def self.description 30 def self.description
36 _('Add Video') 31 _('Add Video')
37 end 32 end
38 33
  34 + #FIXME Make this test
39 def help 35 def help
40 _('This block presents a video block.') 36 _('This block presents a video block.')
41 end 37 end
42 38
  39 + #FIXME Make this test
43 def content(args={}) 40 def content(args={})
44 block = self 41 block = self
45 42
@@ -47,10 +44,25 @@ class VideoBlock < Block @@ -47,10 +44,25 @@ class VideoBlock < Block
47 render :file => 'video_block', :locals => { :block => block } 44 render :file => 'video_block', :locals => { :block => block }
48 end 45 end
49 end 46 end
50 -  
51 47
  48 + #FIXME Make this test
52 def cacheable? 49 def cacheable?
53 false 50 false
54 end 51 end
  52 +
  53 + private
  54 +
  55 + #FIXME Make this test
  56 + def extract_youtube_id
  57 + return nil unless is_youtube?
  58 + youtube_match = url.match('v=([[:alnum:]]*)')
  59 + youtube_match ||= url.match('youtu.be\/([[:alnum:]]*)')
  60 + youtube_match[1] unless youtube_match.nil?
  61 + end
55 62
  63 + def extract_vimeo_id
  64 + return nil unless is_vimeo?
  65 + vimeo_match = url.match('([[:digit:]]*)$')
  66 + vimeo_match[1] unless vimeo_match.nil?
  67 + end
56 end 68 end
plugins/video/test/unit/video_block_test.rb
@@ -3,101 +3,139 @@ class VideoBlockTest < ActiveSupport::TestCase @@ -3,101 +3,139 @@ class VideoBlockTest < ActiveSupport::TestCase
3 3
4 ### Tests for YouTube 4 ### Tests for YouTube
5 5
6 - should "is_valid_source return true when the url contains http://youtube.com" do 6 + should "is_youtube return true when the url contains http://youtube.com" do
7 block = VideoBlock.new 7 block = VideoBlock.new
8 block.url = "http://youtube.com/?v=XXXXX" 8 block.url = "http://youtube.com/?v=XXXXX"
9 - assert block.is_valid_source? 9 + assert block.is_youtube?
10 end 10 end
11 11
12 - should "is_valid_source return true when the url contains https://youtube.com" do 12 + should "is_youtube return true when the url contains https://youtube.com" do
13 block = VideoBlock.new 13 block = VideoBlock.new
14 block.url = "https://youtube.com/?v=XXXXX" 14 block.url = "https://youtube.com/?v=XXXXX"
15 - assert block.is_valid_source? 15 + assert block.is_youtube?
16 end 16 end
17 17
18 - should "is_valid_source return true when the url contains https://www.youtube.com" do 18 + should "is_youtube return true when the url contains https://www.youtube.com" do
19 block = VideoBlock.new 19 block = VideoBlock.new
20 block.url = "https://www.youtube.com/?v=XXXXX" 20 block.url = "https://www.youtube.com/?v=XXXXX"
21 - assert block.is_valid_source? 21 + assert block.is_youtube?
22 end 22 end
23 23
24 - should "is_valid_source return true when the url contains www.youtube.com" do 24 + should "is_youtube return true when the url contains www.youtube.com" do
25 block = VideoBlock.new 25 block = VideoBlock.new
26 block.url = "www.youtube.com/?v=XXXXX" 26 block.url = "www.youtube.com/?v=XXXXX"
27 - assert block.is_valid_source? 27 + assert block.is_youtube?
28 end 28 end
29 29
30 - should "is_valid_source return true when the url contains youtube.com" do 30 + should "is_youtube return true when the url contains youtube.com" do
31 block = VideoBlock.new 31 block = VideoBlock.new
32 block.url = "youtube.com/?v=XXXXX" 32 block.url = "youtube.com/?v=XXXXX"
33 - assert block.is_valid_source? 33 + assert block.is_youtube?
34 end 34 end
35 35
36 - should "is_valid_source return false when the url not contains youtube video ID" do 36 + should "is_youtube return false when the url not contains youtube video ID" do
37 block = VideoBlock.new 37 block = VideoBlock.new
38 block.url = "youtube.com/" 38 block.url = "youtube.com/"
39 - assert !block.is_valid_source? 39 + assert !block.is_youtube?
40 end 40 end
41 41
42 - should "is_valid_source return false when the url contains empty youtube video ID" do 42 + should "is_youtube return false when the url contains empty youtube video ID" do
43 block = VideoBlock.new 43 block = VideoBlock.new
44 block.url = "youtube.com/?v=" 44 block.url = "youtube.com/?v="
45 - assert !block.is_valid_source? 45 + assert !block.is_youtube?
46 end 46 end
47 47
48 - should "is_valid_source return false when the url contains an invalid youtube link" do 48 + should "is_youtube return false when the url contains an invalid youtube link" do
49 block = VideoBlock.new 49 block = VideoBlock.new
50 block.url = "http://www.yt.com/?v=XXXXX" 50 block.url = "http://www.yt.com/?v=XXXXX"
51 - assert !block.is_valid_source? 51 + assert !block.is_youtube?
52 end 52 end
53 53
54 #### Tests for Vimeo Videos 54 #### Tests for Vimeo Videos
55 55
56 - should "is_valid_source return true when the url contains http://vimeo.com" do 56 + should "is_vimeo return true when the url contains http://vimeo.com" do
57 block = VideoBlock.new 57 block = VideoBlock.new
58 block.url = "http://vimeo.com/98979" 58 block.url = "http://vimeo.com/98979"
59 - assert block.is_valid_source? 59 + assert block.is_vimeo?
60 end 60 end
61 61
62 - should "is_valid_source return true when the url contains https://vimeo.com" do 62 + should "is_vimeo return true when the url contains https://vimeo.com" do
63 block = VideoBlock.new 63 block = VideoBlock.new
64 block.url = "https://vimeo.com/989798" 64 block.url = "https://vimeo.com/989798"
65 - assert block.is_valid_source? 65 + assert block.is_vimeo?
66 end 66 end
67 67
68 - should "is_valid_source return true when the url contains https://www.vimeo.com" do 68 + should "is_vimeo return true when the url contains https://www.vimeo.com" do
69 block = VideoBlock.new 69 block = VideoBlock.new
70 block.url = "https://www.vimeo.com/98987" 70 block.url = "https://www.vimeo.com/98987"
71 - assert block.is_valid_source? 71 + assert block.is_vimeo?
72 end 72 end
73 73
74 - should "is_valid_source return true when the url contains www.vimeo.com" do 74 + should "is_vimeo return true when the url contains www.vimeo.com" do
75 block = VideoBlock.new 75 block = VideoBlock.new
76 block.url = "www.vimeo.com/989798" 76 block.url = "www.vimeo.com/989798"
77 - assert block.is_valid_source? 77 + assert block.is_vimeo?
78 end 78 end
79 79
80 - should "is_valid_source return true when the url contains vimeo.com" do 80 + should "is_vimeo return true when the url contains vimeo.com" do
81 block = VideoBlock.new 81 block = VideoBlock.new
82 block.url = "vimeo.com/09898" 82 block.url = "vimeo.com/09898"
83 - assert block.is_valid_source? 83 + assert block.is_vimeo?
84 end 84 end
85 85
86 - should "is_valid_source return false when the url not contains vimeo video ID" do 86 + should "is_vimeo return false when the url not contains vimeo video ID" do
87 block = VideoBlock.new 87 block = VideoBlock.new
88 block.url = "vimeo.com/home" 88 block.url = "vimeo.com/home"
89 - assert !block.is_valid_source? 89 + assert !block.is_vimeo?
90 end 90 end
91 91
92 - should "is_valid_source return false when the url contains empty vimeo video ID" do 92 + should "is_vimeo return false when the url contains empty vimeo video ID" do
93 block = VideoBlock.new 93 block = VideoBlock.new
94 block.url = "vimeo.com/" 94 block.url = "vimeo.com/"
95 - assert !block.is_valid_source? 95 + assert !block.is_vimeo?
96 end 96 end
97 97
98 - should "is_valid_source return false when the url contains an invalid vimeo link" do 98 + should "is_vimeo return false when the url contains an invalid vimeo link" do
99 block = VideoBlock.new 99 block = VideoBlock.new
100 block.url = "http://www.vmsd.com/98979" 100 block.url = "http://www.vmsd.com/98979"
101 - assert !block.is_valid_source? 101 + assert !block.is_vimeo?
102 end 102 end
  103 +
  104 + # Other video formats
  105 + should "is_video return true if url ends with mp4" do
  106 + block = VideoBlock.new
  107 + block.url = "http://www.vmsd.com/98979.mp4"
  108 + assert block.is_video_file?
  109 + end
  110 +
  111 + should "is_video return true if url ends with ogg" do
  112 + block = VideoBlock.new
  113 + block.url = "http://www.vmsd.com/98979.ogg"
  114 + assert block.is_video_file?
  115 + end
  116 +
  117 + should "is_video return true if url ends with ogv" do
  118 + block = VideoBlock.new
  119 + block.url = "http://www.vmsd.com/98979.ogv"
  120 + assert block.is_video_file?
  121 + end
  122 +
  123 + should "is_video return true if url ends with webm" do
  124 + block = VideoBlock.new
  125 + block.url = "http://www.vmsd.com/98979.webm"
  126 + assert block.is_video_file?
  127 + end
  128 +
  129 + should "is_video return false if url ends without mp4, ogg, ogv, webm" do
  130 + block = VideoBlock.new
  131 + block.url = "http://www.vmsd.com/98979.mp4r"
  132 + assert !block.is_video_file?
  133 + block.url = "http://www.vmsd.com/98979.oggr"
  134 + assert !block.is_video_file?
  135 + block.url = "http://www.vmsd.com/98979.ogvr"
  136 + assert !block.is_video_file?
  137 + block.url = "http://www.vmsd.com/98979.webmr"
  138 + assert !block.is_video_file?
  139 + end
  140 +
103 end 141 end
plugins/video/views/video_block.rhtml
1 <h3 class="block-title"> 1 <h3 class="block-title">
2 - <span><%=block.title%></span> 2 + <span><%=block.title%></span>
3 </h3> 3 </h3>
4 <div class="video-block-data"> 4 <div class="video-block-data">
5 <% if block.is_youtube? %> 5 <% if block.is_youtube? %>
6 - <%= render :partial => 'iframe_video_block', :locals => { :url => block.format_embed_video_url_for_youtube, :width => block.width, :height => block.height }%> 6 + <%= render :partial => 'iframe_video_block', :locals => { :url => block.format_embed_video_url_for_youtube, :width => block.width, :height => block.height }%>
7 <% elsif block.is_vimeo? %> 7 <% elsif block.is_vimeo? %>
8 - <%= render :partial => 'iframe_video_block', :locals => { :url => block.format_embed_video_url_for_vimeo, :width => block.width, :height => block.height }%>  
9 - <% elsif block.is_file? %>  
10 - <%= render :partial => 'html5_video_block', :locals => { :url => block.url, :width => block.width, :height => block.height }%> 8 + <%= render :partial => 'iframe_video_block', :locals => { :url => block.format_embed_video_url_for_vimeo, :width => block.width, :height => block.height }%>
  9 + <% elsif block.is_video_file? %>
  10 + <%= render :partial => 'html5_video_block', :locals => { :url => block.url, :width => block.width, :height => block.height }%>
11 <% else %> 11 <% else %>
12 - <%= "<span class='alert-block'> Cadastre uma nova URL (Vimeo, Youtube)</span>" %> 12 + <%= "<span class='alert-block'> Cadastre uma nova URL (Vimeo, Youtube)</span>" %>
13 <% end %> 13 <% end %>
14 14
15 </div> 15 </div>