tags:

views:

253

answers:

1

I would like to check if the URI will need SSL authentication:

url = URI.parse("http://www.google.com")

# [some code]

if url.instance_of? URI::HTTPS
   http.use_ssl=true
   http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

However, those few lines throw the following error..

/usr/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?): HTTPS (URI::InvalidURIError)
    from /usr/lib/ruby/1.8/uri/common.rb:485:in `parse'
    from /usr/lib/ruby/1.8/uri/common.rb:608:in `URI'
    from links.rb:18

Why is it happening?

+4  A: 
>> uri = URI.parse("http://www.google.com")
=> #<URI::HTTP:0x1014ca458 URL:http://www.google.com&gt;
>> uri.scheme
=> "http"
>> uri = URI.parse("https://mail.google.com")
=> #<URI::HTTPS:0x1014c2e60 URL:https://mail.google.com&gt;
>> uri.scheme
=> "https"

So you could check uri's scheme against simple "https" string.

Eimantas