<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Igrigorik's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 04:41:51 GMT</pubDate>
    <description>DZone Snippets: Igrigorik's Code Snippets</description>
    <item>
      <title>URL/URI Validations in Rails</title>
      <link>http://snippets.dzone.com/posts/show/2563</link>
      <description>Here is a quick/basic URI validation library for your Rails app: &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'net/http'&lt;br /&gt;&lt;br /&gt;# Original credits: http://blog.inquirylabs.com/2006/04/13/simple-uri-validation/&lt;br /&gt;# HTTP Codes: http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTPResponse.html&lt;br /&gt;&lt;br /&gt;class ActiveRecord::Base&lt;br /&gt;  def self.validates_uri_existence_of(*attr_names)&lt;br /&gt;    configuration = { :message =&gt; "is not valid or not responding", :on =&gt; :save, :with =&gt; nil }&lt;br /&gt;    configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)&lt;br /&gt;    &lt;br /&gt;    raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp)&lt;br /&gt;    &lt;br /&gt;    validates_each(attr_names, configuration) do |r, a, v|&lt;br /&gt;        if v.to_s =~ configuration[:with] # check RegExp&lt;br /&gt;              begin # check header response&lt;br /&gt;                  case Net::HTTP.get_response(URI.parse(v))&lt;br /&gt;                    when Net::HTTPSuccess then true&lt;br /&gt;                    else r.errors.add(a, configuration[:message]) and false&lt;br /&gt;                  end &lt;br /&gt;              rescue # Recover on DNS failures.. &lt;br /&gt;                  r.errors.add(a, configuration[:message]) and false&lt;br /&gt;              end&lt;br /&gt;        else&lt;br /&gt;          r.errors.add(a, configuration[:message]) and false&lt;br /&gt;        end &lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Save the code above into a file in your 'lib' directory. ex: validates_uri_existence_of.rb. Include the file in your environment.rb (ex: require 'validates_uri_existence_of'). &lt;br /&gt;&lt;br /&gt;And now you can use the validator in any model by calling:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;validates_uri_existence_of :url, :with =&gt;&lt;br /&gt;        /(^$)|(^(http|https)://[a-z0-9] ([-.]{1}[a-z0-9] )*.[a-z]{2,5}(([0-9]{1,5})?/.*)?$)/ix&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Above code validates the URI/URL against a regular expression first, and then does a HEAD query to confirm that the page is alive (HTTPSuccess code is returned). You could easily modify the code to extend the functionality as you wish. &lt;br /&gt;&lt;br /&gt;More info in &lt;a href="http://www.igvita.com/blog/2006/09/07/validating-url-in-ruby-on-rails/"&gt; this post.&lt;/a&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 08 Sep 2006 07:56:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2563</guid>
      <author>igrigorik (Ilya Grigorik)</author>
    </item>
  </channel>
</rss>
