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
- .create_embeded_url(video_id, width = 420, height = 315) ⇒ Object
- .create_regular_url(video_id) ⇒ Object
- .create_shortened_url(video_id) ⇒ Object
- .extract_video_id(youtube_url) ⇒ Object
- .has_invalid_chars?(youtube_url) ⇒ Boolean
- .valid_id?(id) ⇒ Boolean
- .valid_youtube_url?(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
.create_embeded_url(video_id, width = 420, height = 315) ⇒ Object
30 31 32 |
# File 'lib/easy_youtube.rb', line 30 def self.(video_id, width = 420, height = 315) %(<iframe width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{video_id}" frameborder="0" allowfullscreen></iframe>) end |
.create_regular_url(video_id) ⇒ Object
38 39 40 |
# File 'lib/easy_youtube.rb', line 38 def self.create_regular_url(video_id) "http://www.youtube.com/watch?v=#{video_id}" end |
.create_shortened_url(video_id) ⇒ Object
46 47 48 |
# File 'lib/easy_youtube.rb', line 46 def self.create_shortened_url(video_id) "http://youtu.be/#{ video_id }" end |
.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
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/easy_youtube.rb', line 50 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_url?(youtube_url) ⇒ Boolean
63 64 65 66 67 68 69 |
# File 'lib/easy_youtube.rb', line 63 def self.valid_youtube_url?(youtube_url) if has_invalid_chars?(youtube_url) false else valid_id?(extract_video_id(youtube_url)) end end |
.youtube_embed_url(youtube_url, width = 420, height = 315) ⇒ Object
26 27 28 |
# File 'lib/easy_youtube.rb', line 26 def self.(youtube_url, width = 420, height = 315) (extract_video_id(youtube_url), width, height) end |
.youtube_regular_url(youtube_url) ⇒ Object
34 35 36 |
# File 'lib/easy_youtube.rb', line 34 def self.youtube_regular_url(youtube_url) create_regular_url(extract_video_id(youtube_url)) end |
.youtube_shortened_url(youtube_url) ⇒ Object
42 43 44 |
# File 'lib/easy_youtube.rb', line 42 def self.youtube_shortened_url(youtube_url) create_shortened_url(extract_video_id(youtube_url)) end |