http icon indicating copy to clipboard operation
http copied to clipboard

Refactor `HTTP::URI` to be more consistent.

Open ixti opened this issue 6 years ago • 2 comments

Right now some of the methods are returning unexpected results:

HTTP::URI.new("https://example.com").join("/foo")

I expect the above to result in a HTTP::URI instance, but it returns Addresable::URI. That came as a surprise for me as I had some HTTP::URI inherited class which started to act weirdly after I have upgraded http gem. :D

ixti avatar Jul 19 '18 23:07 ixti

So far I am using following kludge (monkey-patch) in my code:

module HTTP
  class URI
    def join(uri)
      self.class.new(@uri.join(uri))
    end

    def normalize
      self.class.new(@uri.normalize)
    end

    def omit(*components)
      self.class.new(@uri.omit(*components))
    end

    def freeze
      @uri.freeze
      hash
      super
    end
  end
end

Notice that this kludge reveals one more problem wiht our HTTP::URI:

HTTP::URI.new("https://example.com").freeze.hash

The above will fail without monkey-patching shown above.

ixti avatar Jul 19 '18 23:07 ixti

Oof, yeah HTTP::URI's goal was to encapsulate Addressable so as to ensure it isn't part of the public API. I'll admit it was a bit of a rush/hack job and not particularly well thought out, and if it's leaking Addressable in places that's definitely a bug.

tarcieri avatar Jul 20 '18 00:07 tarcieri