Use URI to build URIs and hostname= to wrap ipv6 addresses in brackets.

See: https://bugs.ruby-lang.org/issues/3788
Related: https://www.ietf.org/rfc/rfc2732.txt
@brandondunne good call, I'm actually going to open a PR on ruby to fix build so I can pass :host => "::1" and have it run hostname= under the covers so it handles "bare" ipv6 addresses.  Once that is fixed in upstream, this code can be simplified as you suggest.