Sporkmonger

purveyor of fabulously ambiguous eating utensils

Introducing Addressable

Posted by sporkmonger
Written September 5th, 2007

So, after 3 months, I’m back from my excursion to Africa. Don’t worry, I’m not coming back empty-handed.

I thought my fellow rubyists might benefit from the URI implementation I’ve been using in various projects, so I’ve extracted it out into its own library. In the process, I improved a few things here and there, added support for URI Templates, and fleshed out the specifications a bit more.

I introduce to you the Addressable library.

It has about a 2:1 spec to code ratio, 100.0% code coverage, and it has gone through plenty of code heckling. It ought to be pretty reliable.

Hope you like it. Let me know how it works.

Update:

I’ve added URI Template variable extraction and I updated the documentation. The extract_mapping method should be useful for template-based routing systems for frameworks.

  1. Grant McInnes Grant McInnes :
    Written September 6th, 2007 at 01:36 AM

    seriously, chief, that’s the most beautiful spec I’ve ever seen. Well done.

  2. Written September 7th, 2007 at 02:49 PM

    Why thank you!

  3. Written November 30th, 2007 at 10:58 PM

    Hi, is this a drop-in replacement for URI? I’m thinking of doing either

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    
    begin
      require 'rubygems'
      require 'addressable/uri'
      def URI.parse(*args)
        Addressable::URI.parse(*args)
      end
    rescue Exception
    end
    
    or simply
    1
    2
    3
    4
    5
    6
    7
    
    
    begin
      require 'rubygems'
      require 'addressable/uri'
      URI = Addressable::URI
    rescue Exception
    end
    
  4. Written December 3rd, 2007 at 05:22 PM

    choonkeat:

    Neither of those will work with open-uri I’m afraid.

    1
    2
    3
    4
    
    
    require 'open-uri'
    open('http://google.com/') { |http| http.read }
    # => Google's content
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    
    require 'open-uri'
    begin
      require 'rubygems'
      require 'addressable/uri'
      def URI.parse(*args)
        Addressable::URI.parse(*args)
      end
    rescue Exception
    end
    open('http://google.com/') { |http| http.read }
    # => Exception
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    
    require 'open-uri'
    begin
      require 'rubygems'
      require 'addressable/uri'
      URI = Addressable::URI
    rescue Exception
    end
    open('http://google.com/') { |http| http.read }
    # => Exception
    

    The best immediate option is not to rely on the interface similarity between URI and Addressable::URI and code directly to Addressable::URI. If that doesn’t work because you need to rely on some other piece of software, then patch that software to use Addressable instead, and submit the patch to the author of the library in question.

    It is my intention to eventually go to ruby-core and ask for Addressable to replace URI, but I wanted to make sure Addressable was bullet-proof first. I’m currently quite satisfied with it, so that will likely happen soon, but there’s no guarantees that URI will be going away anytime soon.

  5. Written May 25th, 2008 at 01:27 PM

    Hey, I’m using Addressable and it’s really nice!

    Just one thing about the heuristic parser, if I do something like:

    Addressable::URI.heuristic_parse(‘mailto:jose.valim@gmail.com’), it replaces the mailto scheme for http.

    I looked into Your code, did some changes, but, obviously, a few specs started to fail! =)

    Any idea?

    Thanks, José Valim!

Leave a Response

NOTE: I'm afraid Javascript needs to be on in order to comment.

Comments should be formatted using Textile.

Ruby code should be enclosed within a <macro:code lang="ruby"> element. Other languages are supported. For output you can simply omit the lang attribute.