Class: UrlValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/url_validator.rb,
lib/url_validator/engine.rb,
lib/url_validator/version.rb

Defined Under Namespace

Classes: Engine

Constant Summary collapse

VERSION =
'0.0.3'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_optionsObject



14
15
16
# File 'lib/url_validator.rb', line 14

def default_options
  { scheme: nil, allow_no_scheme: false, allow_no_host: false }
end

.valid?(value, options = {}) ⇒ Boolean



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

def valid?(value, options = {})
  options = default_options.merge(options)
  uri = URI.parse(value)

  [:scheme_appearance, :host_appearance, :scheme_type].all? do |method|
    send("validate_#{method}", uri, options)
  end
rescue URI::Error
  false
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



41
42
43
44
45
# File 'lib/url_validator.rb', line 41

def validate_each(record, attribute, value)
  unless self.class.valid?(value, options)
    record.errors.add(attribute, options[:message] || :invalid_url)
  end
end