Created
November 28, 2012 01:32
-
-
Save rwjblue/4158467 to your computer and use it in GitHub Desktop.
Validate path in ruby via net/http HEAD request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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