Class: YouTubeAddy

Inherits:
Object
  • Object
show all
Defined in:
lib/youtube_addy.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

Class Method Details

.extract_video_id(youtube_url) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/youtube_addy.rb', line 16

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

Returns:

  • (Boolean)


12
13
14
# File 'lib/youtube_addy.rb', line 12

def self.has_invalid_chars?(youtube_url)
  !INVALID_CHARS.match(youtube_url).nil?
end

.youtube_embed_url(youtube_url, width = 420, height = 315) ⇒ Object



25
26
27
28
# File 'lib/youtube_addy.rb', line 25

def self.youtube_embed_url(youtube_url, width = 420, height = 315)
  vid_id = extract_video_id(youtube_url)
  %(<iframe width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{vid_id}" frameborder="0" allowfullscreen></iframe>)
end

.youtube_regular_url(youtube_url) ⇒ Object



30
31
32
33
# File 'lib/youtube_addy.rb', line 30

def self.youtube_regular_url(youtube_url)
  vid_id = extract_video_id(youtube_url)
  "http://www.youtube.com/watch?v=#{ vid_id }"
end

.youtube_shortened_url(youtube_url) ⇒ Object



35
36
37
38
# File 'lib/youtube_addy.rb', line 35

def self.youtube_shortened_url(youtube_url)
  vid_id = extract_video_id(youtube_url)
  "http://youtu.be/#{ vid_id }"
end