defmodule Tld do @moduledoc """ Top-Level Domain module for BEAM. Lets you get the current list of TLDs, as well as find the TLD in a domain name. """ alias Tld.DomainName alias Tld.ListServer @doc """ Get list of all Top-Level Domains. ## Examples iex> list = Tld.get_list() iex> Enum.member?(list, "NET") true iex> Enum.member?(list, "TEST") false """ def get_list() do ListServer.get_list() end @doc """ If input string contains a domain name, get its TLD. Returns {:ok, "COM"} if found (COM being replaced with the actual TLD found), {:err, :not_found} if TLD was not found in string, and {:err, :invalid_input} if not given a string. ## Examples iex> Tld.get_tld_from_domain_name("contoso.com") {:ok, "COM"} iex> Tld.get_tld_from_domain_name("FACE.BORG.TEST") {:err, :not_found} iex> Tld.get_tld_from_domain_name("CORRUPT.GOV") {:ok, "GOV"} iex> Tld.get_tld_from_domain_name('NOTASTRING.NET') {:err, :invalid_input} iex> Tld.get_tld_from_domain_name(42) {:err, :invalid_input} iex> Tld.get_tld_from_domain_name(nil) {:err, :invalid_input} """ def get_tld_from_domain_name(""), do: {:err, :not_found} def get_tld_from_domain_name(name) when is_binary(name) do match = name # Domain names are case-insensitive, but our matching logic is not, so # upcase the candidate string before matching. |> String.upcase() |> DomainName.match_tld(get_list()) if is_binary(match) do {:ok, match} else {:err, :not_found} end end def get_tld_from_domain_name(_), do: {:err, :invalid_input} end