youtube_addy 0.0.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/youtube_addy.rb +25 -2
- metadata +1 -1
data/lib/youtube_addy.rb
CHANGED
@@ -1,5 +1,28 @@
|
|
1
1
|
class YouTubeAddy
|
2
|
-
|
3
|
-
|
2
|
+
URL_Formats = {
|
3
|
+
regular: /^(https?:\/\/)?(www\.)?youtube.com\/watch\?v=([^&]+)/,
|
4
|
+
shortened: /^(https?:\/\/)?(www\.)?youtu.be\/([^&]+)/,
|
5
|
+
invalid_chars: /[^a-zA-Z0-9\:\/\?\=\&\$\-\_\.\+\!\*\'\(\)\,]/
|
6
|
+
}
|
7
|
+
|
8
|
+
def self.has_invalid_chars?(youtube_url)
|
9
|
+
!URL_Formats[:invalid_chars].match(youtube_url).nil?
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.extract_video_id(youtube_url)
|
13
|
+
return nil if has_invalid_chars?(youtube_url)
|
14
|
+
|
15
|
+
if match = URL_Formats[:regular].match(youtube_url)
|
16
|
+
return match[3]
|
17
|
+
elsif match = URL_Formats[:shortened].match(youtube_url)
|
18
|
+
return match[3]
|
19
|
+
end
|
20
|
+
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.youtube_embed_url(youtube_url, width = 420, height = 315)
|
25
|
+
vid_id = extract_video_id(youtube_url)
|
26
|
+
%(<iframe width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{vid_id}" frameborder="0" allowfullscreen></iframe>)
|
4
27
|
end
|
5
28
|
end
|