title: Checking URL availability Ruby on Rails
body: <p>Suppose that after primitive validation of user submitted URL I have the string that looks like URL:</p>
  
  <pre><code>url = 'http://www.thisdomaindoesntexist.com/dont_even_ask_about/this/uri'
  </code></pre>
  
  <p>How can I check if its available or not?</p>
  
  <p>I tried this in my is_valid_link function:</p>
  
  <pre><code>require "net/http"
  url = URI.parse(url)
  req = Net::HTTP.new(url.host, url.port)
  res = req.request_head(url.path)
  </code></pre>
  
  <p>It works if the server exists giving me back the HTTP response, but the problem is that in case of bad url I get an error like this:</p>
  
  <pre><code>SocketError in PostsController#create
  
  getaddrinfo: nodename nor servname provided, or not known
  </code></pre>
  
  <p>How should I do this kind of validation properly?</p>
  
  <p>Thanks in advance.</p>