Skip to content

Instantly share code, notes, and snippets.

@rwjblue
Created November 28, 2012 01:32
Show Gist options
  • Save rwjblue/4158467 to your computer and use it in GitHub Desktop.
Save rwjblue/4158467 to your computer and use it in GitHub Desktop.
Validate path in ruby via net/http HEAD request
require 'net/http'
require 'uri'
def valid_path?(path, allowed_redirects=5)
allowed_redirects.times do
url = URI.parse(path)
response = Net::HTTP.new(url.host, url.port).request_head(url.path)
if response.kind_of?(Net::HTTPRedirection)
path = response['location']
else
return response.kind_of?(Net::HTTPSuccess)
end
end
false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment