I have an admission to make.
I hate Ruby’s URI class. There, I’ve said it. It’s out in the open, I can’t take it back. I just really don’t like it. It constantly tells me that my URIs are invalid and that I’m a bad person and it basically just hates me. So yeah. I’m just not gonna take it anymore. URI class, I’m quitting you. I’ve found someone else who treats me nicely, someone who knows how to normalize without making me do lots of extra work, someone who actually understands IRIs, and who makes allowance for the fact that sometimes people are ignorant morons who don’t read the specifications.
I’d like to introduce GentleCMS’s replacement for Ruby’s standard library URI implementation: GentleCMS::URI. It’s based on RFC 3986 instead of RFC 2396 and RFC 2732, so it’s a little bit more modern, it’s fairly close in functionality to Ruby’s URI class, and for the most part, it shares all of the same methods. The opaque method is gone because it’s no longer used in RFC 3986, although if necessary, an alias could be created I suppose, since path now takes opaque’s place.
The class will automatically use Ruby’s bindings for libidn if they’re present:
1
2
3
4
5
6
|
require 'system/uri'
uri = GentleCMS::URI.parse(
"http://www.詹姆斯.com/atomtests/iri/詹.html")
uri.normalize.to_s
# => "http://www.xn--8ws00zhy3a.com/atomtests/iri/%E8%A9%B9.html"
|
I wanted to write a pure-Ruby IDNA implementation, but in the interests of not yak-shaving for the rest of my life, I decided not to bother.
The code, thus far, is checked into the repository if you want to play with it. Complete with an executable specification and 100.0% C0 code coverage according to RCov.
It is my intention to polish it up a bit longer, improve the documentation, run with it for awhile in the real world, and then see what it would take to have it replace the implementation in Ruby’s standard library. I’ll probably start having FeedTools use the code as well.
651 lines of implementation, 739 lines of specification.