Class: YoutubeEmbed::Video
- Inherits:
-
Object
- Object
- YoutubeEmbed::Video
- Defined in:
- lib/youtube_embed/video.rb
Class Method Summary collapse
Instance Method Summary collapse
- #embed_url ⇒ Object
- #iframe ⇒ Object
-
#initialize(video_url, options = {}) ⇒ Video
constructor
A new instance of Video.
- #video_id ⇒ Object
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, = {}) @video_url = video_url @options = { allow_fullscreen: true, show_title: true, show_similar: false, show_controls: true, width: 640, height: 360 }.merge(.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, = {}) new(video_url, ).iframe end |
Instance Method Details
#embed_url ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/youtube_embed/video.rb', line 43 def 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 |
#iframe ⇒ Object
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="#{}" frameborder="0"#{' allowfullscreen' if allow_fullscreen?}></iframe>) if result.respond_to?(:html_safe) result.html_safe else result end end |
#video_id ⇒ Object
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 |