Sahil Dhankhar Sahil Dhankhar - 7 months ago 11
Ruby Question

Converting to valid urls which can be opened by open-uri

I need to open some webpages using open-uri in ruby and then parse the content of those pages using Nokogori.
I just did:

require 'open-uri'
content_file = open(user_input_url)


This worked for:
http://www.google.co.in
and
http://google.co.in
but fails when user give inputs like
www.google.co.in
or
google.co.in
.

One thing i can do for such inputs i can append
http://
and
https://
and return the content of the page that opens. But this seems like a big hack to me.
Is there any better way to achieve this in ruby(i.e converting these user_inputs to valid open_uri urls).

Answer
uri = URI("www.google.com")
if uri.instance_of?(URI::Generic)
    uri = URI::HTTP.build({:host => uri.to_s}) 
end
content_file = open(uri)

There are other ways as well see ref: http://www.ruby-doc.org/stdlib-2.0.0/libdoc/uri/rdoc/URI/HTTP.html