public
Description: ActiveRecord property URL validation
Home | Edit | New

Home

Validates that a URL is accessible and is an HTTP URL by opening a connection to the server.


class Company < ActiveRecord::Base
  validates_http_url :homepage, :wrong_response => "did not return a successful HTTP code"
end

Configuration options:

  • valid_responses – An array of valid HTTP responses (as subclasses of Net::HTTPResponse). By default this contains Net::HTTPSuccess (and therefore all subclasses of it, such as Net::HTTPOK). If you would like to add your own valid responses, make sure to include Net::HTTPSuccess unless you explicitly want to remove it from the set of valid responses.
  • wrong_response – A custom error message for when the server does not return a valid HTTP response
  • wrong_protocol – A custom error message for when the URL provided is not an HTTP URL
  • malformed_url – A custom error message for when a malformed URL is provided
  • no_response – A custom error message for when there is no response from the address provided
  • message – A custom error message to be used for any of the above error messages that are not specified

If you would like to specify a custom error message for a particular response code, pass a hash instead of a string for wrong_response. Map each Net::HTTPResponse subclass to the error message you’d like to use for that response. If you map an error message to Net::HTTPResponse, it will be used as the default error message for response codes without custom error messages. An example:


validates_http_url :url, :wrong_response => { Net::HTTPMovedPermanently => "has been moved", Net::HTTPResponse => "is not working correctly" }
Last edited by RISCfuture, Fri Jun 27 19:49:05 -0700 2008
Home | Edit | New
Versions: