Class: HttpUrlValidator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compliant?(value) ⇒ Boolean


3
4
5
6
7
8
9
# File 'app/validators/http_url_validator.rb', line 3

def self.compliant?(value)
  escaped_address = URI.escape(value)
  uri = URI.parse(escaped_address)
  uri.is_a?(URI::HTTP) && !uri.host.nil?
rescue URI::InvalidURIError
  false
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object


11
12
13
14
15
# File 'app/validators/http_url_validator.rb', line 11

def validate_each(record, attribute, value)
  unless value.present? && self.class.compliant?(value)
    record.errors.add(attribute, "is not a valid HTTP URL")
  end
end