Skip to content

Instantly share code, notes, and snippets.

@SeanRoberts
Created August 24, 2015 16:18
Show Gist options
  • Save SeanRoberts/246d501a50b00d2f5fef to your computer and use it in GitHub Desktop.
Save SeanRoberts/246d501a50b00d2f5fef to your computer and use it in GitHub Desktop.
.embed-container
- if obj.external_video_url.downcase.match('youtube')
%iframe.external-video{ :frameborder => 0, :allowfullscreen => true, :src => "//www.youtube.com/embed/#{youtube_video_id_from_url(obj.external_video_url)}?rel=0" }
- elsif obj.external_video_url.downcase.match('vimeo')
%iframe.external-video{ frameborder: 0, webkitallowfullscreen: true, mozallowfullscreen: true, allowfullscreen: true, src: "//player.vimeo.com/video/#{vimeo_video_id_from_url(obj.external_video_url)}?color=#{current_site.brand_color.gsub('#', '')}&title=0&byline=0&portrait=0"}
# The CSS
.embed-container {
position: relative;
padding-bottom: 56.25%;
height: 0;
overflow: hidden;
max-width: 100%;
margin-bottom: 15px;
}
.embed-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
# The helper
module ExternalVideoHelper
def youtube_video_id_from_url(url)
regex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/
match = url.match(regex)
if (match && match[2].length == 11)
match[2];
else
nil
end
end
def vimeo_video_id_from_url(url)
regex = /(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/
match = url.match(regex)
if match && match[5]
match[5]
else
nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment