class UrlValidator
Constants
- VERSION
Public Class Methods
default_options()
click to toggle source
# File lib/url_validator.rb, line 14 def default_options { scheme: nil, allow_no_scheme: false, allow_no_host: false } end
valid?(value, options = {})
click to toggle source
# 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
Private Class Methods
validate_host_appearance(uri, options)
click to toggle source
# File lib/url_validator.rb, line 24 def validate_host_appearance(uri, options) uri.host.nil? ? options[:allow_no_host] : true end
validate_scheme_appearance(uri, options)
click to toggle source
# File lib/url_validator.rb, line 20 def validate_scheme_appearance(uri, options) uri.scheme.nil? ? options[:allow_no_scheme] : true end
validate_scheme_type(uri, options)
click to toggle source
# File lib/url_validator.rb, line 28 def validate_scheme_type(uri, options) if options[:scheme] if uri.scheme.nil? && options[:allow_no_scheme] true else options[:scheme].include?(uri.scheme) end else true end end
Public Instance Methods
validate_each(record, attribute, value)
click to toggle source
# 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