Class: EasyYouTube
- Inherits:
-
Object
- Object
- EasyYouTube
- Defined in:
- lib/easy_youtube.rb
Constant Summary collapse
- URL_FORMATS =
{ regular: /^(https?:\/\/)?(www\.)?youtube.com\/watch\?(.*\&)?v=(?<id>[^&]+)/, shortened: /^(https?:\/\/)?(www\.)?youtu.be\/(?<id>[^&]+)/, embed: /^(https?:\/\/)?(www\.)?youtube.com\/embed\/(?<id>[^&]+)/, embed_as3: /^(https?:\/\/)?(www\.)?youtube.com\/v\/(?<id>[^?]+)/, chromeless_as3: /^(https?:\/\/)?(www\.)?youtube.com\/apiplayer\?video_id=(?<id>[^&]+)/ }
- INVALID_CHARS =
/[^a-zA-Z0-9\:\/\?\=\&\$\-\_\.\+\!\*\'\(\)\,]/
Class Method Summary collapse
- .extract_video_id(youtube_url) ⇒ Object
- .has_invalid_chars?(youtube_url) ⇒ Boolean
- .valid_id?(id) ⇒ Boolean
- .valid_youtube_link?(youtube_url) ⇒ Boolean
- .youtube_embed_url(youtube_url, width = 420, height = 315) ⇒ Object
- .youtube_regular_url(youtube_url) ⇒ Object
- .youtube_shortened_url(youtube_url) ⇒ Object
Class Method Details
.extract_video_id(youtube_url) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/easy_youtube.rb', line 13 def self.extract_video_id(youtube_url) return nil if has_invalid_chars?(youtube_url) URL_FORMATS.values.each do |format_regex| match = format_regex.match(youtube_url) return match[:id] if match end end |
.has_invalid_chars?(youtube_url) ⇒ Boolean
21 22 23 |
# File 'lib/easy_youtube.rb', line 21 def self.has_invalid_chars?(youtube_url) !INVALID_CHARS.match(youtube_url).nil? end |
.valid_id?(id) ⇒ Boolean
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/easy_youtube.rb', line 40 def self.valid_id?(id) if id response = Net::HTTP.get("gdata.youtube.com", "/feeds/api/videos/#{id}") if ["Invalid id", "Video not found"].include? response false else true end else false end end |
.valid_youtube_link?(youtube_url) ⇒ Boolean
53 54 55 56 57 58 59 60 |
# File 'lib/easy_youtube.rb', line 53 def self.valid_youtube_link?(youtube_url) if has_invalid_chars?(youtube_url) false else video_id = extract_video_id(youtube_url) valid_id?(id) end end |
.youtube_embed_url(youtube_url, width = 420, height = 315) ⇒ Object
25 26 27 28 |
# File 'lib/easy_youtube.rb', line 25 def self.(youtube_url, width = 420, height = 315) video_id = extract_video_id(youtube_url) %(<iframe width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{video_id}" frameborder="0" allowfullscreen></iframe>) end |
.youtube_regular_url(youtube_url) ⇒ Object
30 31 32 33 |
# File 'lib/easy_youtube.rb', line 30 def self.youtube_regular_url(youtube_url) video_id = extract_video_id(youtube_url) "http://www.youtube.com/watch?v=#{ video_id }" end |
.youtube_shortened_url(youtube_url) ⇒ Object
35 36 37 38 |
# File 'lib/easy_youtube.rb', line 35 def self.youtube_shortened_url(youtube_url) video_id = extract_video_id(youtube_url) "http://youtu.be/#{ video_id }" end |