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 3 settings_items :url, :type => :string, :default => ""
4 4 settings_items :width, :type => :integer, :default => 400
5 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 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 17 end
26 18  
  19 + #FIXME Make this test
27 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 22 end
30 23  
  24 + #FIXME Make this test
31 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 27 end
34 28  
  29 + #FIXME Make this test
35 30 def self.description
36 31 _('Add Video')
37 32 end
38 33  
  34 + #FIXME Make this test
39 35 def help
40 36 _('This block presents a video block.')
41 37 end
42 38  
  39 + #FIXME Make this test
43 40 def content(args={})
44 41 block = self
45 42  
... ... @@ -47,10 +44,25 @@ class VideoBlock < Block
47 44 render :file => 'video_block', :locals => { :block => block }
48 45 end
49 46 end
50   -
51 47  
  48 + #FIXME Make this test
52 49 def cacheable?
53 50 false
54 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 68 end
... ...
plugins/video/test/unit/video_block_test.rb
... ... @@ -3,101 +3,139 @@ class VideoBlockTest < ActiveSupport::TestCase
3 3  
4 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 7 block = VideoBlock.new
8 8 block.url = "http://youtube.com/?v=XXXXX"
9   - assert block.is_valid_source?
  9 + assert block.is_youtube?
10 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 13 block = VideoBlock.new
14 14 block.url = "https://youtube.com/?v=XXXXX"
15   - assert block.is_valid_source?
  15 + assert block.is_youtube?
16 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 19 block = VideoBlock.new
20 20 block.url = "https://www.youtube.com/?v=XXXXX"
21   - assert block.is_valid_source?
  21 + assert block.is_youtube?
22 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 25 block = VideoBlock.new
26 26 block.url = "www.youtube.com/?v=XXXXX"
27   - assert block.is_valid_source?
  27 + assert block.is_youtube?
28 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 31 block = VideoBlock.new
32 32 block.url = "youtube.com/?v=XXXXX"
33   - assert block.is_valid_source?
  33 + assert block.is_youtube?
34 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 37 block = VideoBlock.new
38 38 block.url = "youtube.com/"
39   - assert !block.is_valid_source?
  39 + assert !block.is_youtube?
40 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 43 block = VideoBlock.new
44 44 block.url = "youtube.com/?v="
45   - assert !block.is_valid_source?
  45 + assert !block.is_youtube?
46 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 49 block = VideoBlock.new
50 50 block.url = "http://www.yt.com/?v=XXXXX"
51   - assert !block.is_valid_source?
  51 + assert !block.is_youtube?
52 52 end
53 53  
54 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 57 block = VideoBlock.new
58 58 block.url = "http://vimeo.com/98979"
59   - assert block.is_valid_source?
  59 + assert block.is_vimeo?
60 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 63 block = VideoBlock.new
64 64 block.url = "https://vimeo.com/989798"
65   - assert block.is_valid_source?
  65 + assert block.is_vimeo?
66 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 69 block = VideoBlock.new
70 70 block.url = "https://www.vimeo.com/98987"
71   - assert block.is_valid_source?
  71 + assert block.is_vimeo?
72 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 75 block = VideoBlock.new
76 76 block.url = "www.vimeo.com/989798"
77   - assert block.is_valid_source?
  77 + assert block.is_vimeo?
78 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 81 block = VideoBlock.new
82 82 block.url = "vimeo.com/09898"
83   - assert block.is_valid_source?
  83 + assert block.is_vimeo?
84 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 87 block = VideoBlock.new
88 88 block.url = "vimeo.com/home"
89   - assert !block.is_valid_source?
  89 + assert !block.is_vimeo?
90 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 93 block = VideoBlock.new
94 94 block.url = "vimeo.com/"
95   - assert !block.is_valid_source?
  95 + assert !block.is_vimeo?
96 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 99 block = VideoBlock.new
100 100 block.url = "http://www.vmsd.com/98979"
101   - assert !block.is_valid_source?
  101 + assert !block.is_vimeo?
102 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 141 end
... ...
plugins/video/views/video_block.rhtml
1 1 <h3 class="block-title">
2   - <span><%=block.title%></span>
  2 + <span><%=block.title%></span>
3 3 </h3>
4 4 <div class="video-block-data">
5 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 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 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 13 <% end %>
14 14  
15 15 </div>
... ...