Class: UriValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/uri_validator.rb

Overview

Instance Method Summary collapse

Instance Method Details

#validate_each(object, attribute, value) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/validators/uri_validator.rb', line 5

def validate_each(object, attribute, value)
  raise(ArgumentError, "A regular expression must be supplied as the :format option of the options hash") unless options[:format].nil? or options[:format].is_a?(Regexp)
  configuration = { message: "is invalid or not responding", format: URI::regexp(%w[http https]) }
  configuration.update(options)

  if value =~ configuration[:format]
    begin
      response = Faraday.head(value)
      object.errors.add(attribute, configuration[:message]) unless response.success?
    rescue # Recover on DNS failures, SSL errors, etc.
      object.errors.add(attribute, configuration[:message])
    end
  else
    object.errors.add(attribute, configuration[:message])
  end
end