Last active
August 29, 2015 14:19
-
-
Save zokioki/26a624e99f39faa46273 to your computer and use it in GitHub Desktop.
URL Response Code Checker - Ruby Script
This file contains 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 "csv" | |
def response_code_for_url(url) | |
begin | |
url = URI.parse(url) | |
req = Net::HTTP.new(url.host, url.port) | |
res = req.request_head(url.path) | |
if res.code == '301' | |
response_code_for_url(res['location']) | |
else | |
res.code | |
end | |
rescue URI::InvalidURIError | |
'500' | |
end | |
end | |
ok, redirect, error = 0, 0, 0 | |
CSV.foreach("urls_to_check.csv") do |row| | |
url = row[0] | |
code = response_code_for_url(url) | |
case code | |
when '200' | |
ok += 1 | |
when '301' | |
redirect += 1 | |
else | |
error += 1 | |
puts "#{code}: for url #{url}" | |
end | |
end | |
puts "#{ok}: pages found" | |
puts "#{redirect}: pages redirected" | |
puts "#{error}: pages in error" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment