easy_youtube 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/easy_youtube.rb +65 -58
- metadata +1 -1
data/lib/easy_youtube.rb
CHANGED
@@ -1,63 +1,70 @@
|
|
1
1
|
require "net/http"
|
2
2
|
class EasyYouTube
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
URL_FORMATS = {
|
4
|
+
:regular => /^(https?:\/\/)?(www\.)?youtube.com\/watch\?(.*\&)?v=(?<id>[^&]+)/,
|
5
|
+
:shortened => /^(https?:\/\/)?(www\.)?youtu.be\/(?<id>[^&]+)/,
|
6
|
+
:embed => /^(https?:\/\/)?(www\.)?youtube.com\/embed\/(?<id>[^&]+)/,
|
7
|
+
:embed_as3 => /^(https?:\/\/)?(www\.)?youtube.com\/v\/(?<id>[^?]+)/,
|
8
|
+
:chromeless_as3 => /^(https?:\/\/)?(www\.)?youtube.com\/apiplayer\?video_id=(?<id>[^&]+)/
|
9
|
+
}
|
10
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
|
-
|
11
|
+
INVALID_CHARS = /[^a-zA-Z0-9\:\/\?\=\&\$\-\_\.\+\!\*\'\(\)\,]/
|
12
|
+
|
13
|
+
def self.extract_video_id(youtube_url)
|
14
|
+
return nil if has_invalid_chars?(youtube_url)
|
15
|
+
URL_FORMATS.values.each do |format_regex|
|
16
|
+
match = format_regex.match(youtube_url)
|
17
|
+
return match[:id] if match
|
18
|
+
end
|
19
|
+
return nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.has_invalid_chars?(youtube_url)
|
23
|
+
!INVALID_CHARS.match(youtube_url).nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.youtube_embed_url(youtube_url, width = 420, height = 315)
|
27
|
+
create_embeded_url(extract_video_id(youtube_url), width, height)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.create_embeded_url(video_id, width = 420, height = 315)
|
31
|
+
%(<iframe width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{video_id}" frameborder="0" allowfullscreen></iframe>)
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.youtube_regular_url(youtube_url)
|
35
|
+
create_regular_url(extract_video_id(youtube_url))
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.create_regular_url(video_id)
|
39
|
+
"http://www.youtube.com/watch?v=#{video_id}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.youtube_shortened_url(youtube_url)
|
43
|
+
create_shortened_url(extract_video_id(youtube_url))
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.create_shortened_url(video_id)
|
47
|
+
"http://youtu.be/#{ video_id }"
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.valid_id?(id)
|
51
|
+
if id
|
52
|
+
response = Net::HTTP.get("gdata.youtube.com", "/feeds/api/videos/#{id}")
|
53
|
+
if ["Invalid id", "Video not found"].include? response
|
54
|
+
false
|
55
|
+
else
|
56
|
+
true
|
57
|
+
end
|
58
|
+
else
|
59
|
+
false
|
60
|
+
end
|
61
|
+
end
|
62
62
|
|
63
|
+
def self.valid_youtube_url?(youtube_url)
|
64
|
+
if has_invalid_chars?(youtube_url)
|
65
|
+
false
|
66
|
+
else
|
67
|
+
valid_id?(extract_video_id(youtube_url))
|
68
|
+
end
|
69
|
+
end
|
63
70
|
end
|