Skip to content

😈 A library for parsing TLDs from urls in Elixir

Notifications You must be signed in to change notification settings

Zensavona/domainatrex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Domainatrex

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

hex.pm version hex.pm downloads License

Installation

Add the following to your mix.exs

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

Usage

Domainatrex should be able to handle all valid hostnames, it uses 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"}}

Configuration

For maximum performance, Domainatrex reads the list of all known top-level domains at compile time. Likewise, by default, the package will attempt to fetch the latest list of TLDs from the web before falling back to a local (potentially out of date) copy. You can configure this behavior in your config.exs as follows:

  • :fetch_latest: A Boolean flag to determine whether Domainatrex should try to fetch the latest list of public suffixes at compile time; default is true
  • :public_suffix_list_url: A charlist URL to the latest public suffix file that Domainatrex will try to fetch at compile time; default is 'https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat'
  • :fallback_local_copy: The path to the local suffix file that Domainatrex will use if it wasn't able to fetch a fresh file from the URL, or if fetching updated files was disabled; default is the "lib/public_suffix_list.dat" file included in the package.

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 (not necessarily tested with Domainatrex!)
  public_suffix_list_url: 'https://publicsuffix.org/list/public_suffix_list.dat',
  fallback_local_copy: "priv/my_app_custom_suffix_list.dat"