I've come across a few different ways to validate URLs in my day, but they all seem a bit more complicated than necessary. Perhaps I'll see the wisdom of these techniques soon, but for now it seems like there's an easy solution to the problem:
class Link < ActiveRecord::Base attr_accessible :url validate :validate_url private def validate_url errors.add(:url) unless %w(200 301 302).include?(Link.status_code(self.url)) end def self.status_code(url) regexp = url.match(/https?:\/\/([^\/]+)(.*)/) path = regexp[2].blank? ? '/' : regexp[2] Net::HTTP.start(regexp[1]) {|http| http.head(path).code} rescue nil end end
Et VoilĂ .

Abusable? Sure HEAD isn’t *supposed* to do anything, but I’ll bet money there are sites out there that have URLs you shouldn’t be blindly hitting. I guess a simple timeout wouldn’t be the end of the world, but could be moderately annoying. Also, I could make your servers show connections to TERRORISTS or kiddie porn servers, if I knew where to connect to them (which I don’t).
All in all, I’m not sure you want to leave your server connecting up to another one to a blind check.