DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Validate URIs By Pinging The Server
require 'open-uri'
class ActiveRecord::Base
def self.validates_uri_existence_of(*attr_names)
configuration = { :message => "is not a valid web address" }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
validates_each attr_names do |m, a, v|
begin
# Try to open the URI
open v
rescue
# Report the error if it throws an exception
m.errors.add(a, configuration[:message])
end
end
end
end
Details on my blog.





