Domainatrex

Domainatrex is a TLD parsing library for Elixir, using the Public Suffix list

hex.pm versionhex.pm downloadsLicense

Read the docs

Installation

Add the following to your mix.exs

defp deps do
  [
    {:domainatrex, "~> 3.2"},
  ]

Usage

Domainatrex parses host names using the Public Suffix List and is heavily inspired by the fantastic Domainatrix library for Ruby

iex> Domainatrex.parse("someone.com")
{:ok, %{domain: "someone", subdomain: "", tld: "com"}}

iex> Domainatrex.parse("blog.someone.id.au")
{:ok, %{domain: "someone", subdomain: "blog", tld: "id.au"}}

iex> Domainatrex.tld?("com")
true

iex> Domainatrex.tld?("someone.com")
false

Configuration

For maximum performance, Domainatrex reads the list of all known top-level domains at compile time. By default, the package will attempt to fetch the latest list of TLDs from the web. If fetching fails (or is disabled), it falls back to a local copy. If the fallback file is also missing, compilation will exit with an error.

You can configure this behavior in your config.exs as follows:

Here's a complete example of how you might customize this behavior in your config.exs:

config :domainatrex,
  # Explicitly allow compile-time HTTP request to fetch the latest list of TLDs (default)
  fetch_latest: true,
  # Download the public suffix list from the official source
  public_suffix_list_url: ~c"https://publicsuffix.org/list/public_suffix_list.dat",
  fallback_local_copy: "priv/my_app_custom_suffix_list.dat",
  # Add custom suffix for com.be
  custom_suffixes: ["com.be"],
  # Include private domains (default)
  include_private: true

Limitations