My users submit urls (to mixes on mixcloud.com) and my app uses them to perform web requests.
A good url returns a 200 status code:
uri = URI.parse("http://www.mixcloud.com/ErolAlkan/hard-summer-mix/")
request = Net::HTTP.get_response(uri)(
#<Net::HTTPOK 200 OK readbody=true>
uri = "http://www.mixcloud.com/ErolAlkan/hard-summer-mix"
#<Net::HTTPMovedPermanently 301 MOVED PERMANENTLY readbody=true>
# bad path returns a 404
# bad path minus trailing slash returns a 301
301 redirects are fairly common if you do not type the URL exactly as the web server expects it. They happen much more frequently than you'd think, you just don't normally ever notice them while browsing because the browser does all that automatically for you.
Two alternatives come to mind:
open-uri handles redirects automatically. So all you'd need to do is:
require 'open-uri' ... response = open('http://xyz...').read
def get_response_with_redirect(uri) r = Net::HTTP.get_response(uri) if r.code == "301" r = Net::HTTP.get_response(URI.parse(r.header['location'])) end r end
If you want to be even smarter you could try to add or remove missing backslashes to the URL when you get a 404 response. You could do that by creating a method like
get_response_smart which handles this URL fiddling in addition to the redirects.