Class: VideoPlayer::Parser

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

Constant Summary collapse

DefaultWidth =
'420'
DefaultHeight =
'315'
DefaultAutoPlay =
true
YouTubeRegex =
/\A(https?:\/\/)?(www.)?(youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/watch\?feature=player_embedded&v=)([A-Za-z0-9_-]*)(\&\S+)?(\?\S+)?/i
VimeoRegex =
/\Ahttps?:\/\/(www.)?vimeo\.com\/([A-Za-z0-9._%-]*)((\?|#)\S+)?/i
IzleseneRegex =
/\Ahttp:\/\/(?:.*?)\izlesene\.com\/video\/([\w\-\.]+[^#?\s]+)\/(.*)?$/i
WistiaRegex =
/\Ahttps?:\/\/(.+)?(wistia.com|wi.st)\/(medias|embed)\/([A-Za-z0-9_-]*)(\&\S+)?(\?\S+)?/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, width = DefaultWidth, height = DefaultHeight, autoplay = DefaultAutoPlay) ⇒ Parser

Returns a new instance of Parser.



20
21
22
23
24
25
# File 'lib/video_player.rb', line 20

def initialize(url, width = DefaultWidth, height = DefaultHeight, autoplay = DefaultAutoPlay)
  @url = url
  @width = width
  @height = height
  @autoplay = autoplay
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



18
19
20
# File 'lib/video_player.rb', line 18

def height
  @height
end

#urlObject

Returns the value of attribute url.



18
19
20
# File 'lib/video_player.rb', line 18

def url
  @url
end

#widthObject

Returns the value of attribute width.



18
19
20
# File 'lib/video_player.rb', line 18

def width
  @width
end

Instance Method Details

#autoplayObject



42
43
44
# File 'lib/video_player.rb', line 42

def autoplay
  !!@autoplay ? '1' : '0'
end

#embed_codeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/video_player.rb', line 27

def embed_code
  case
  when matchdata = url.match(YouTubeRegex)
    youtube_embed(matchdata[4])
  when matchdata = url.match(VimeoRegex)
    vimeo_embed(matchdata[2])
  when matchdata = url.match(IzleseneRegex)
    izlesene_embed(matchdata[2])
  when matchdata = url.match(WistiaRegex)
    wistia_embed(matchdata[4])
  else
    false
  end
end

#iframe_code(src) ⇒ Object



46
47
48
# File 'lib/video_player.rb', line 46

def iframe_code(src)
  %{<iframe src="#{src}" width="#{width}" height="#{height}" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>}
end

#izlesene_embed(video_id) ⇒ Object



60
61
62
63
# File 'lib/video_player.rb', line 60

def izlesene_embed(video_id)
  src = "//www.izlesene.com/embedplayer/#{video_id}/?autoplay=#{autoplay}&showrel=0&showinfo=0"
  iframe_code(src)
end

#vimeo_embed(video_id) ⇒ Object



55
56
57
58
# File 'lib/video_player.rb', line 55

def vimeo_embed(video_id)
  src = "//player.vimeo.com/video/#{video_id}?autoplay=#{autoplay}"
  iframe_code(src)
end

#wistia_embed(video_id) ⇒ Object



65
66
67
68
# File 'lib/video_player.rb', line 65

def wistia_embed(video_id)
  src = "//fast.wistia.net/embed/iframe/#{video_id}/?autoplay=#{autoplay}&showrel=0&showinfo=0"
  iframe_code(src)
end

#youtube_embed(video_id) ⇒ Object



50
51
52
53
# File 'lib/video_player.rb', line 50

def youtube_embed(video_id)
  src = "//www.youtube.com/embed/#{video_id}?autoplay=#{autoplay}&rel=0"
  iframe_code(src)
end