DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Check If A URL Exists
Source: <a href="http://snippets.dzone.com/posts/show/4638">Ruby remote file checker</a> [dzone.com]
<snip>
require 'open-uri'
require 'net/http'
def remote_file_exists?(url)
url = URI.parse(url)
Net::HTTP.start(url.host, url.port) do |http|
return http.head(url.request_uri).code == "200"
end
end
</snip>
remote_file_exists? 'http://www.www.www' #=> false remote_file_exists? 'http://www.wired.com/' #=> true remote_file_exists? 'http://www.wired.com/blogs/' #=> true remote_file_exists? 'http://www.wired.com/i-love-ruby/' #=> false






Comments
Snippets Manager replied on Wed, 2009/07/01 - 7:42am
require 'rubygems' require 'rest-open-uri' def remote_file_exist?(url) open( url , :method => :head).status rescue false end