Created
May 8, 2012 23:42
-
-
Save adamcrown/2640438 to your computer and use it in GitHub Desktop.
Rails validator for URI existence
This file contains 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
# Original credits: http://blog.inquirylabs.com/2006/04/13/simple-uri-validation/ | |
# HTTP Codes: http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTPResponse.html | |
require 'net/http' | |
class UriExistsValidator < ActiveModel::EachValidator | |
def validate_each(record, attribute, value) | |
begin # check header response | |
url = URI.parse(value) | |
request = Net::HTTP.new(url.host, url.port) | |
response = request.request_head(url.path.empty? ? '/' : url.path) | |
unless response.is_a? Net::HTTPSuccess | |
record.errors[attribute] << 'is not valid or not reponding' | |
end | |
rescue # Recover on DNS failures.. | |
record.errors[attribute] << 'is not valid or not reponding' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment