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 20 |
# 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 return nil end |
.has_invalid_chars?(youtube_url) ⇒ Boolean
22 23 24 |
# File 'lib/easy_youtube.rb', line 22 def self.has_invalid_chars?(youtube_url) !INVALID_CHARS.match(youtube_url).nil? end |
.valid_id?(id) ⇒ Boolean
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/easy_youtube.rb', line 41 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
54 55 56 57 58 59 60 61 |
# File 'lib/easy_youtube.rb', line 54 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
26 27 28 29 |
# File 'lib/easy_youtube.rb', line 26 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
31 32 33 34 |
# File 'lib/easy_youtube.rb', line 31 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
36 37 38 39 |
# File 'lib/easy_youtube.rb', line 36 def self.youtube_shortened_url(youtube_url) video_id = extract_video_id(youtube_url) "http://youtu.be/#{ video_id }" end |