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.
Files changed (2) hide show
  1. data/lib/easy_youtube.rb +65 -58
  2. metadata +1 -1
@@ -1,63 +1,70 @@
1
1
  require "net/http"
2
2
  class EasyYouTube
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
- }
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
- 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
- video_id = extract_video_id(youtube_url)
28
- %(<iframe width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{video_id}" frameborder="0" allowfullscreen></iframe>)
29
- end
30
-
31
- def self.youtube_regular_url(youtube_url)
32
- video_id = extract_video_id(youtube_url)
33
- "http://www.youtube.com/watch?v=#{ video_id }"
34
- end
35
-
36
- def self.youtube_shortened_url(youtube_url)
37
- video_id = extract_video_id(youtube_url)
38
- "http://youtu.be/#{ video_id }"
39
- end
40
-
41
- def self.valid_id?(id)
42
- if id
43
- response = Net::HTTP.get("gdata.youtube.com", "/feeds/api/videos/#{id}")
44
- if ["Invalid id", "Video not found"].include? response
45
- false
46
- else
47
- true
48
- end
49
- else
50
- false
51
- end
52
- end
53
-
54
- def self.valid_youtube_link?(youtube_url)
55
- if has_invalid_chars?(youtube_url)
56
- false
57
- else
58
- video_id = extract_video_id(youtube_url)
59
- valid_id?(id)
60
- end
61
- end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_youtube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: