video_block.rb
1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class VideoBlock < Block
attr_accessible :url, :width, :height
settings_items :url, :type => :string, :default => ""
settings_items :width, :type => :integer, :default => 400
settings_items :height, :type => :integer, :default => 315
YOUTUBE_ID_FORMAT = '\w-'
def is_youtube?
url.match(/.*(youtube.com.*v=[#{YOUTUBE_ID_FORMAT}]+|youtu.be\/[#{YOUTUBE_ID_FORMAT}]+).*/) ? true : false
end
def is_vimeo?
url.match(/^(http[s]?:\/\/)?(www.)?(vimeo.com|player.vimeo.com\/video)\/[[:digit:]]+/) ? true : false
end
def is_video_file?
url.match(/.*(mp4|ogg|ogv|webm)$/) ? true : false
end
def format_embed_video_url_for_youtube
"//www.youtube-nocookie.com/embed/#{extract_youtube_id}?rel=0&wmode=transparent" if is_youtube?
end
def format_embed_video_url_for_vimeo
"//player.vimeo.com/video/#{extract_vimeo_id}" if is_vimeo?
end
def self.description
_('Display a Video')
end
def help
_('This block presents a video from youtube, vimeo and some video formats (mp4, ogg, ogv and webm)')
end
def content(args={})
block = self
proc do
render :file => 'video_block', :locals => { :block => block }
end
end
private
def extract_youtube_id
return nil unless is_youtube?
youtube_match = url.match("v=([#{YOUTUBE_ID_FORMAT}]*)")
youtube_match ||= url.match("youtu.be\/([#{YOUTUBE_ID_FORMAT}]*)")
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