class UrlFormatValidator

Public Instance Methods

format_value(record, attribute, value) click to toggle source
# File lib/url_format/url_format_validator.rb, line 11
def format_value(record, attribute, value)
  record.send("#{attribute}=",UrlFormat.ensure_http_prefix(value))
end
url_regexp() click to toggle source

Thanks to Dean Perry and Ryan Bates github.com/deanperry/url_formatter/blob/master/lib/url_formatter.rb

# File lib/url_format/url_format_validator.rb, line 17
def url_regexp
  /\Ahttps?:\/\/([\A\s:@]+:[\A\s:@]*@)?[-[[:alnum:]]]+(\.[-[[:alnum:]]]+)+\.?(:\d{1,5})?([\/?]\S*)?\z/iux
end
validate_each(record, attribute, value) click to toggle source
# File lib/url_format/url_format_validator.rb, line 2
def validate_each(record, attribute, value)
  value = format_value(record, attribute, value)
  unless URI.parse(value).kind_of?(URI::HTTP) && value =~ url_regexp
    record.errors[attribute] << (options[:message] || "is invalid")
  end
rescue URI::InvalidURIError
  record.errors[attribute] << (options[:message] || "is invalid")
end