The code below will take any vimeo or youtube URL and return the video ID and the provider.
In your model
def parse_video_url(url)
@url = url
youtube_formats = [
%r(https?://youtu\.be/(.+)),
%r(https?://www\.youtube\.com/watch\?v=(.*?)(&|#|$)),
%r(https?://www\.youtube\.com/embed/(.*?)(\?|$)),
%r(https?://www\.youtube\.com/v/(.*?)(#|\?|$)),
%r(https?://www\.youtube\.com/user/.*?#\w/\w/\w/\w/(.+)\b)
]
vimeo_formats = [%r(https?://vimeo.com\/(\d+)), %r(https?:\/\/(www\.)?vimeo.com\/(\d+))]
@url.strip!
if @url.include? "youtu"
youtube_formats.find { |format| @url =~ format } and $1
@results = {provider: "youtube", id: $1}
@results
elsif @url.include? "vimeo"
vimeo_formats.find { |format| @url =~ format } and $1
@results = {provider: "vimeo", id: $1}
@results
else
return nil # There should probably be some error message here
end
end
Then in your controller just call:
@results = @course.parse_video_url(@course.video_url)
# Access the hash with @results[:provider] or @results [:id]
And in your view you can write an IF statement to display the relevant embed code for the provider/id combo.