From 56f2b7348ec2ad9dcf8324ef888f7a45acaa7b99 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Thu, 3 May 2012 01:25:26 -0400 Subject: [PATCH] Add URL validation class. --- config/environment.rb | 3 +++ lib/url_validator.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 lib/url_validator.rb diff --git a/config/environment.rb b/config/environment.rb index b58e7e1..b6522d0 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -3,3 +3,6 @@ require File.expand_path('../application', __FILE__) # Initialize the rails application WwwWhoisandrewCom::Application.initialize! + +# Include the URL validator +require 'url_validator' \ No newline at end of file diff --git a/lib/url_validator.rb b/lib/url_validator.rb new file mode 100644 index 0000000..61c48ef --- /dev/null +++ b/lib/url_validator.rb @@ -0,0 +1,13 @@ +class UrlValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + begin + uri = URI.parse(value) + resp = uri.kind_of?(URI::HTTP) + rescue URI::InvalidURIError + resp = false + end + unless resp == true + record.errors[attribute] << (options[:message] || "is not an url") + end + end +end \ No newline at end of file