class ActiveModel::Validations::UrlValidator

Public: Uses `URI.regexp` to validate URLs, by default only allows the http and https protocols.

Examples

validates :url, :url => true
# => only allow http, https

validates :url, :url => %w{http https ftp ftps}
# => allow http, https, ftp and ftps

Public Class Methods

new(options) click to toggle source

Public: Creates a new instance, overrides `:protocols` if either `:with` or `:in` are defined, to allow for shortcut setters.

Examples:

validates :url, :url => { :protocols => %w{http https ftp} }
# => accepts http, https and ftp URLs

validates :url, :url => 'https'
# => accepts only https URLs (shortcut form of above)

validates :url, :url => true
# => by default allows only http and https

Raises an ArgumentError if the array with the allowed protocols is empty.

Returns a new instance.

Calls superclass method
# File lib/active_validators/active_model/validations/url_validator.rb, line 38
def initialize(options)
  options[:protocols] ||= options.delete(:protocol) || options.delete(:with) || options.delete(:in)
  super
end

Public Instance Methods

validate_each(record, attribute, value) click to toggle source

Public: Validate URL, if it fails adds an error.

Returns nothing.

# File lib/active_validators/active_model/validations/url_validator.rb, line 46
def validate_each(record, attribute, value)
  uri = as_uri(value)
  tld_requirement_fullfilled = check_tld_requirement(value)
  record.errors.add(attribute) unless uri && value.to_s =~ uri_regexp && tld_requirement_fullfilled
end

Private Instance Methods

as_uri(value) click to toggle source

Internal: Tries to convert supplied string into URI, if not possible returns nil => invalid URI.

Returns the URI or nil.

# File lib/active_validators/active_model/validations/url_validator.rb, line 94
def as_uri(value)
  URI.parse(value.to_s) rescue nil if value.present?
end
check_tld_requirement(value) click to toggle source

Internal: Checks if the tld requirements are fullfilled

When :require_tld option is set to true, the url will be searched for a dot anywhere inside the host except the first or last position

Returns a boolean value.

# File lib/active_validators/active_model/validations/url_validator.rb, line 85
def check_tld_requirement(value)
  host = URI.parse(value.to_s).host rescue value
  options[:require_tld] === true ? host =~ /.(\.)\w+/ : true
end
check_validity!() click to toggle source

Internal: Ensures that at least one protocol is defined. If the protocols are empty it throws an ArgumentError.

Raises ArgumentError if protocols is empty.

Returns nothing.

# File lib/active_validators/active_model/validations/url_validator.rb, line 59
def check_validity!
  raise ArgumentError, "At least one URI protocol is required" if protocols.empty?
end
protocols() click to toggle source

Internal: Returns an array of protocols to use with the URI regexp. The default protocols are `http` and `https`.

Returns the Array with the allowed protocols.

# File lib/active_validators/active_model/validations/url_validator.rb, line 67
def protocols
  Array.wrap(options[:protocols] || %w{http https})
end
uri_regexp() click to toggle source

Internal: Constructs the regular expression to check the URI for the configured protocols.

Returns the Regexp.

# File lib/active_validators/active_model/validations/url_validator.rb, line 75
def uri_regexp
  @uri_regexp ||= /\A#{URI::Parser.new.make_regexp(protocols)}\z/
end