Public Suffix List

Description

The Public Suffix List (publicsuffix.org/) is “a cross-vendor initiative to provide an accurate list of domain name suffixes”. Such a list is necessary because “there was and remains no algorithmic method of finding the highest level at which a domain may be registered for a particular top-level domain (the policies differ with each registry)…”. Public Suffix List is also a small Ruby library designed to make the Public Suffix List (publicsuffix.org/) easier to use. Public Suffix List will transparently download the latest list of top-level domains, parse the list, and optionally cache the parsed data.

The public API is as simple as I could make it. Instantiating the PublicSuffixList class downloads, parses, and caches the data. There are three instance methods:

tld(domain)

return the top-level domain name

cdn(domain)

return the canonical domain name

split(domain)

split the domain name into parts

Features

  • Transparent download of the TLD data file

  • Optional caching of optimized, parsed TLD data

  • Tiny API

Synopsis

require "public_suffix_list"

# Downloads and parses the latest data file and returns "com".
PublicSuffixList.new.tld("foobar.com")

# Downloads and parses the latest data file and returns "foobar.com".
PublicSuffixList.new.cdn("foobar.com")

# Downloads and parses the latest data file, caches it in /tmp,
# and returns ["abc", "xyz", "co.uk"].
PublicSuffixList.new(cache_dir: "/tmp").split("abc.xyz.co.uk")

# Loads the cached data in /tmp if it is less than 100 seconds old,
# downloads, parses, and caches it if it is older, and returns
# ["test", "parliament", "uk"].
PublicSuffixList.new(cache_dir: "/tmp", cache_expiry_period: 100).split("test.parliament.uk")

# You don't have to instantiate PublicSuffixList every time you
# use it, of course...
p = PublicSuffixList.new
p.split("fee.fi.fo.com") # => ["fee.fi", "fo", "com"]
p.cdn("fee.fi.fo.com")   # => "fo.com", "cdn" is "canonical domain name"
p.tld("fee.fi.fo.com")   # => "com", "tld" is "top-level domain"

# You can even use other data files, both local and remote
# (as long as they conform to the Public Suffix List file format).
PublicSuffixList.new(url: "spec/test.dat")

Requirements

None that I am aware of.

Install

Install the gem directly:

gem install public-suffix-list

Or add it to your Gemfile and use bundler:

gem 'public-suffix-list', '~> 0.1.4'

License

Copyright © 2010-2016 Todd Sundsted

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.