Created
November 27, 2019 10:42
-
-
Save dpsk/c61bed37ce7c7b46eed90ca7fe06862e to your computer and use it in GitHub Desktop.
Small lib for extracting provider and video id from url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VideoUrl | |
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+)) | |
] | |
attr_accessor :url, :id, :provider | |
def initialize(url) | |
@url = url.strip | |
@provider, @id = parse_video_url | |
end | |
def parse_video_url | |
if url.include? "youtu" | |
YOUTUBE_FORMATS.find { |format| url =~ format } and $1 | |
["youtube", $1] | |
elsif url.include? "vimeo" | |
VIMEO_FORMATS.find { |format| url =~ format } and $1 | |
["vimeo", $1] | |
else | |
[] | |
end | |
end | |
end% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment