Class: Onebox::Engine::YoutubeOnebox

Inherits:
Object
  • Object
show all
Includes:
Onebox::Engine, StandardEmbed
Defined in:
lib/onebox/engine/youtube_onebox.rb

Instance Attribute Summary

Attributes included from Onebox::Engine

#cache, #timeout, #url

Instance Method Summary collapse

Methods included from StandardEmbed

#raw

Methods included from Onebox::Engine

engines, included, #initialize

Instance Method Details

#append_params(html) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/onebox/engine/youtube_onebox.rb', line 39

def append_params(html)
  result = html.dup
  result.gsub! /(src="[^"]+)/, '\1&wmode=opaque'
  if url =~ /t=(\d+)/
    result.gsub! /(src="[^"]+)/, '\1&start=' + Regexp.last_match[1]
  end
  result
end

#placeholder_htmlObject



18
19
20
21
22
23
24
# File 'lib/onebox/engine/youtube_onebox.rb', line 18

def placeholder_html
  if video_id
    "<img src='http://i1.ytimg.com/vi/#{video_id}/hqdefault.jpg' width='480' height='270'>"
  else
    to_html
  end
end

#rewrite_agnostic(html) ⇒ Object



48
49
50
# File 'lib/onebox/engine/youtube_onebox.rb', line 48

def rewrite_agnostic(html)
  html.gsub(/https?:\/\//, '//')
end

#to_htmlObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/onebox/engine/youtube_onebox.rb', line 26

def to_html
  if video_id
    # Avoid making HTTP requests if we are able to get the video ID from the
    # URL.
    html = "<iframe width=\"480\" height=\"270\" src=\"https://www.youtube.com/embed/#{video_id}?feature=oembed\" frameborder=\"0\" allowfullscreen></iframe>"
  else
    # Fall back to making HTTP requests.
    html = raw[:html]
  end

  rewrite_agnostic(append_params(html))
end

#video_idObject



13
14
15
16
# File 'lib/onebox/engine/youtube_onebox.rb', line 13

def video_id
  match = @url.match(/^https?:\/\/(www\.)?(youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([a-zA-Z0-9_\-]{11})$/)
  match && match[3]
end