Search code examples
rubysslhttpsopen-urinet-http

Elegantly using Kernel#open for https via open-uri


After requiring open-uri, one can conveniently download and use files from the web via Kernel#open. However, trying to do this with https results in a root cert error, because ruby doesn't have all the root certs.

This can be solved like this, but that's for using a Net::HTTP object with a block.

Is there an elegant way to set use_ssl and ca_file for the Net::HTTP library globally, so that it will apply to my whole app, and commands like Kernel#open?


Solution

  • Alright, after a couple hours I came up with this:

    require 'open-uri'
    require 'net/https'
    
    module Net
      class HTTP
        alias_method :original_use_ssl=, :use_ssl=
        def use_ssl=(flag)
          self.ca_file = "/path/to/ca-bundle.crt"
          self.original_use_ssl = flag
        end
      end
    end
    

    Described more here: https://gist.github.com/996510