Created
March 18, 2013 10:57
-
-
Save kennym/5186419 to your computer and use it in GitHub Desktop.
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 VideoValiadtor < ActiveRecord::Validator | |
def validate(record) | |
# This regex will match YouTube and Vimeo videos | |
regex = /^http:\/\/(?:.*?)\.?(youtube|vimeo)\.com\/watch\?[^#]*v=(\w+)|(\d+)/ | |
# Use it as follows: | |
# | |
# "http://www.youtube.com/watch?v=AZDAIgwbXk4".match(regex) | |
# >> #<MatchData "http://www.youtube.com/watch?v=AZDAIgwbXk4" 1:"youtube" 2:"AZDAIgwbXk4" 3:nil> | |
# | |
# "http://vimeo.com/60555406".match(regex) | |
# >> #<MatchData "http://vimeo.com/60555406" 1:"vimeo" 2:"60555406" 3:nil> | |
# | |
unless record.video_url.match(regex).nil? | |
record.errors[:video_url] << "You have not provided a valid URL to video. Please, use YouTube or Vimeo." | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment