Skip to content

Instantly share code, notes, and snippets.

@stereosupersonic
Created March 24, 2021 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stereosupersonic/28346d9618bd78785c69accd60a41428 to your computer and use it in GitHub Desktop.
Save stereosupersonic/28346d9618bd78785c69accd60a41428 to your computer and use it in GitHub Desktop.
def remote_file_exits?(url, limit=10)
raise ArgumentError, 'too many HTTP redirects' if limit == 0
uri = URI(url)
response = Net::HTTP.get_response(uri)
case response
when Net::HTTPNotFound then
false
when Net::HTTPSuccess then
true
when Net::HTTPRedirection then
location = response['location']
warn "redirected to #{location}"
file_exists?(location, limit - 1)
else
raise response.value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment