ResumeLabBlog
Matt Huggins

Matt Huggins

Web & Mobile Developer

matt.huggins@gmail.comhuggiemhugginsmatt-huggins
Projects
PokerNexus
Poker social network
Codebound
Collectible card game
SVGConverter.io
Vectorize raster images
Recent Blog Posts
One GraphQL Endpoint, Many Schemas: Changing a Schema Under Shipped Mobile AppsOne GraphQL Endpoint, Many Schemas: Changing a Schema Under Shipped Mobile Apps
Nov 1, 2026
QR Codes that Route to the Appropriate App StoreQR Codes that Route to the Appropriate App Store
Sep 24, 2026
Announcing PokerNexus: The Social Network for Poker PlayersAnnouncing PokerNexus: The Social Network for Poker Players
Sep 21, 2026
Blog Topics
aidata fetchingform managementgraphicsgraphqljavascriptnativenode.jsreactrubyruby on railssecuritytanstack formtanstack querytypescriptuser experience

Parsing Domain Names - Now in Gem Form!

Jan 2, 2015
ruby
Back to Blog

Author's Note: This article was originally published as part of the QuickLeft blog while employeed there. Given that the company has been acquired and the site no longer exists, I've rehosted the content here.

Featured in Ruby Weekly

A feature for an internal Ruby project here at Quick Left necessitated parsing the domain from a URL. This seems like a problem for which there must already exist a solution, but it surprisingly turns out that there is nothing readily available for this seemingly simple task.

When I first started searching, I came across the PublicSuffix gem. At first glance, this appeared to be promising: pass it a host, and be given a domain name. Let's give it a try.

ruby
PublicSuffix.parse('www.google.com').domain # => "google.com" PublicSuffix.parse('www.example.co.uk').domain # => "example.co.uk" PublicSuffix.parse('withgoogle.com').domain # => PublicSuffix::DomainNotAllowed: `withgoogle.com' is not allowed according to Registry policy

As it turns out, the PublicSuffix gem is solving a similar problem, but not the one I'm striving to address. According to PublicSuffix.org, the Public Suffix List is an initiative to maintain suffixes under which users can register names. In other words, sites like Blogger and WordPress, which allow users to register subdomains, are considered public suffixes regardless of the registered domain name. Given the context, the PublicSuffix gem's implementation makes sense; it just does not suit our needs.

From that, I set out to solve the problem for our constraints. It's a relatively simple problem, but it's not as simple as taking the ".com" off "example.com". The reasons is that some domain extensions that allow registrations have two or more levels, such as ".co.uk" or ".com.au". Given the possibilities regarding extensions, the best approach appears to be a whitelist of valid extensions. From this whitelist, we can deduce that when parsing a host, the next level down from a whitelisted extension can be considered the registered domain. With that approach in mind, I created the Dominatrix gem.

Let's take the examples from above and plug them into the Dominatrix gem.

ruby
Dominatrix.parse('http://www.google.com') # => "google.com" Dominatrix.parse('http://www.example.co.uk') # => "example.co.uk" Dominatrix.parse('http://withgoogle.com') # => "withgoogle.com"

We're now able to extract domain names. It's worth noting that this gem does not validate whether the domain is legitimately registered via whois; it simply parses the string to compare against the extension whitelist.

As always, I'm happy to support new features, accept bug fixes, and generally merge meaningful pull requests on the project!

Comments