tags:

views:

44

answers:

2

I've been trying to modify this method from redirecting and returning the contents of the url to returning new valid url instead.

After reading up on the Net::HTTP object, I'm still not sure how exactly the get_response method works. Is this what's downloading the page? is there another method I could call that would just ping the url instead of downloading it?

require 'net/http'

def validate(url)
    uri = URI.parse(url)
    response = Net::HTTP.get_response(uri)

    case response
    when Net::HTTPSuccess
        return response
    when Net::HTTPRedirection
        return validate(response['location'])
    else
        return nill
    end
end


puts validate('http://somesite.com/somedir/mypage.html')
A: 

Do you mean, by 'ping the url', you want to know whether the url request returns an HTTP 200 response?

I haven't looked at the implementation of get_response, but I think it just sends out an HTTP GET request, by the looks of it.

If you want to check for the HTTP 200 response, I guess you could just keep doing get_response until you get HTTPSuccess && HTTPOK.

rmk
+1  A: 

You are correct that get_response sends an HTTP GET request to the server, which requests the whole page.

You want to use a HEAD request instead of GET. This requests the same HTTP response header that a GET request would get, including the status code (200, 404, etc.), but without downloading the whole page.

See the request_head and head methods of Net::HTTP. For example

url = URI.parse('http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html')
res = Net::HTTP.start(url.host, url.port) {|http|
    http.head(url.path)
}
puts res.class
Thanks, that's working much faster. Just trying to work it in to the original method. How would the `case` work in this situation?
Aaron Moodie
It's the same as your original code. `res` is an `Net::HTTPResponse` object, just like the `response` in your code.
I'm getting a `NoMethodError` though when I try to update the object. `validate': undefined method `host' for #<String:0x100311360> (NoMethodError)`
Aaron Moodie
That means you are calling `host` on a String object instead of a URI object. Remember to use URI.parse to convert a String to a URI.
bah! had `uri = URI.parse` then was using `url.host`. Thanks for your help!
Aaron Moodie