Class: YoutubeEmbed::Video

Inherits:
Object
  • Object
show all
Defined in:
lib/youtube_embed/video.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(video_url, options = {}) ⇒ Video

Returns a new instance of Video.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/youtube_embed/video.rb', line 9

def initialize(video_url, options = {})
  @video_url = video_url
  @options = {
    allow_fullscreen: true,
    show_title:       true,
    show_similar:     false,
    show_controls:    true,
    width:            640,
    height:           360
  }.merge(options.inject({}){ |o,(k,v)| o[k.to_sym] = v; o })
end

Class Method Details

.iframe(video_url, options = {}) ⇒ Object



4
5
6
# File 'lib/youtube_embed/video.rb', line 4

def iframe(video_url, options = {})
  new(video_url, options).iframe
end

Instance Method Details

#embed_urlObject



43
44
45
46
47
48
49
50
51
# File 'lib/youtube_embed/video.rb', line 43

def embed_url
  params = {}.tap do |p|
    p['rel'] = 0 if !show_similar?
    p['showinfo'] = 0 if !show_title?
    p['controls'] = 0 if !show_controls?
  end

  "https://www.youtube.com/embed/#{video_id}#{params.size == 0 ? '' : '?' + params.map{ |k, v| "#{k}=#{v}" }.join('&')}"
end

#iframeObject



53
54
55
56
57
58
59
60
# File 'lib/youtube_embed/video.rb', line 53

def iframe
  result = %(<iframe width="#{width}" height="#{height}" src="#{embed_url}" frameborder="0"#{' allowfullscreen' if allow_fullscreen?}></iframe>)
  if result.respond_to?(:html_safe)
    result.html_safe
  else
    result
  end
end

#video_idObject



21
22
23
24
25
26
27
28
29
# File 'lib/youtube_embed/video.rb', line 21

def video_id
  @video_id ||=
    if @video_url[/youtu\.be\/([^\?]*)/]
      $1
    else
      @video_url[/^.*((v\/)|(embed\/)|(watch\?))\??v?=?([^\&\?]*).*/]
      $5
    end
end