Class: URLValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/url_attributes/url_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute_name, url) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/url_attributes/url_validator.rb', line 3

def validate_each(record, attribute_name, url)
  @record = record
  @attribute_name = attribute_name
  if url.blank? || !url.respond_to?(:strip) || !(url =~ /\./)
    invalid
    return
  end
  url = "http://#{url.strip}" unless /\Ahttps?:\/\// =~ url
  uri = URI.parse(url) # This can raise a URI::InvalidURIError
  invalid unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
rescue URI::InvalidURIError
  invalid
end