Last active
December 20, 2015 16:59
-
-
Save heygambo/6165056 to your computer and use it in GitHub Desktop.
Find youtube id from any source in ruby (currently 4 types are supported)
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
| def youtube_id(video_tag) | |
| tests = [] | |
| tests.push(/embed\/([a-zA-Z0-9]+)/) | |
| tests.push(/src="\/\/www.youtube.com\/v\/([a-zA-Z0-9]+)/) | |
| tests.push(/youtu.be\/([a-zA-Z0-9]+)/) | |
| tests.push(/youtube.com\/watch\?v=([a-zA-Z0-9]+)/) | |
| tests.each do |regexp| | |
| return video_tag.scan(regexp)[0][0] if video_tag.scan(regexp).length > 0 | |
| end | |
| nil | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment