Class: Zemus::Youtube

Inherits:
Object
  • Object
show all
Defined in:
lib/zemus/youtube.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Youtube

Returns a new instance of Youtube.



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

def initialize(url)
  @url = url
end

Class Method Details

.valid?(url) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
# File 'lib/zemus/youtube.rb', line 4

def self.valid?(url)
  [ /^http(s?):\/\/youtube.com/, /^http(s?):\/\/www.youtube.com/, /^http(s?):\/\/www.m.youtube.com/,
    /^http(s?):\/\/m.youtube.com/, /^http(s?)(s?):\/\/youtu.be/, /^http(s?):\/\/www.youtu.be/,
    /^m.youtube.com/, /^youtu.be/, /^youtube.com/ ].map do |link_style|
      url =~ link_style
    end.any?
end

Instance Method Details

#to_embedObject



16
17
18
# File 'lib/zemus/youtube.rb', line 16

def to_embed
  "<iframe width='100%' height='600px' src='http://www.youtube.com/embed/#{youtube_id}?wmode=opaque' frameborder='0' allowfullscreen></iframe>"
end

#to_embed_imageObject



20
21
22
# File 'lib/zemus/youtube.rb', line 20

def to_embed_image
  "http://img.youtube.com/vi/#{youtube_id}/hqdefault.jpg"
end

#youtube_idObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/zemus/youtube.rb', line 24

def youtube_id
  id = ''

  url = @url.gsub("&feature=player_embedded", "")
  
  if url =~ /\?v=/
    id = url.split('?v=').last.split("&").first
  elsif url =~ /&v=/
    id = url.split('&v=').last.split("&").first
  else
    id = url.split('/').last.split("&").first
  end
  
  id
end