Skip to content

Instantly share code, notes, and snippets.

@kennym
Created March 18, 2013 10:57
Show Gist options
  • Save kennym/5186419 to your computer and use it in GitHub Desktop.
Save kennym/5186419 to your computer and use it in GitHub Desktop.
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