Fork me on GitHub

Project Notes

Simple URIs with addressable

Using the addressable gem for better handling of URIs/URLs


URI/URL handling in Ruby is usually handled by default with the URI class from the standard library. It suffers from a few annoyances, for example:

  • a lot of legacy cruft (try finding the correct encode/escape function for a given situation!)
  • does a pretty good job of decomposing URLs, but doesn’t provide much help when going the other way - composing URLs from parts
  • it has a very old school and un-ruby-ish API

The addressable gem solves many of these problems, and may in fact already be in your project - it’s a popular dependency of many widely used gems like fog and capybara.

I’m just going to test drive a few common scenarios and compare the URI and addressable capabilities. The examples.rb script tests the actual code.

URL Decomposition

The simplest task, and no surpises here. Given a URI-ish string, parse out the component parts.

The URI way:

uri = URI(given)

The addressable way:

uri = Addressable::URI.parse(given)

The main advantage of addressable here is that it provides some nice additional methods for examining the result, including: ip_based?, default_port, inferred_port, tld, domain, site, basename, extname.

URL Composition

Given the individual component parts (scheme, host, path etc), create a valid URI.

The URI way:

uri ='http', nil, '', nil, nil, '/foo/bar', false, nil, nil)
uri ={:scheme => 'http', :host => '', :path => '/foo/bar'})
uri ={:host => '', :path => '/foo/bar'})
uri ={:host => '', :path => '/foo/bar'})

The addressable way:

uri ={:scheme => 'http', :host => '', :path => '/foo/bar'})

URI Templates

More goodness in addressable: RFC 6570 URI templates.

An Addressable::Template defines to expand or extract the components of a URI. This could make funky argument parsing easier if one is not working within a web framework that already provides this capability.

Running the Examples

See the examples.rb script for details.

$ ruby examples.rb
Run options: --seed 49659

# Running:


Finished in 0.034040s, 235.0176 runs/s, 940.0705 assertions/s.

8 runs, 32 assertions, 0 failures, 0 errors, 0 skips

Credits and References

About LCK#169 ruby
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.