Class: WhyValidationsSuckIn96::ValidatesUrl

Inherits:
WhyValidationsSuckIn96::Validation show all
Includes:
WhyValidationsSuckIn96::AttributeBasedValidation, WhyValidationsSuckIn96::SkippableValidation
Defined in:
lib/whyvalidationssuckin96/macros/validates_url.rb

Overview

Checks whether a given attribute is a valid URL

Examples:

Default usage

  setup_validations do
    validates_as_url :website
  end

Specifying valid schemes instead of the defaults

  setup_validations do
    validates_as_url :website, :schemes => %w[ldap mailto]
  end

Constant Summary

DefaultOptions =
{:message => "is not a valid URL", :schemes => %w[http https]}

Constants inherited from WhyValidationsSuckIn96::Validation

DefaultOptions

Instance Method Summary

Methods included from WhyValidationsSuckIn96::AttributeBasedValidation

#attribute, #attribute_value, #initialize

Methods inherited from WhyValidationsSuckIn96::Validation

#failed?, #has_run?, #initialize, #message, new_subclass, #passed?, #validates?

Constructor Details

This class inherits a constructor from WhyValidationsSuckIn96::AttributeBasedValidation

Instance Method Details

- (Object) validate



24
25
26
27
28
29
30
# File 'lib/whyvalidationssuckin96/macros/validates_url.rb', line 24

def validate
  super
  uri = URI.parse(attribute_value)
  options[:schemes].include?(uri.scheme) ? pass : fail
rescue URI::InvalidURIError => e
  fail
end