Class: UriValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- UriValidator
- Defined in:
- app/validators/uri_validator.rb
Overview
Thanks Ilya! http://www.igvita.com/2006/09/07/validating-url-in-ruby-on-rails/
Original credits: http://blog.inquirylabs.com/2006/04/13/simple-uri-validation/
Instance Method Summary collapse
Instance Method Details
#validate_each(object, attribute, value) ⇒ Object
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 [:format].nil? or [:format].is_a?(Regexp) configuration = { message: "is invalid or not responding", format: URI::regexp(%w[http https]) } configuration.update() 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 |