diff --git a/lib/numerus/classifier.ex b/lib/numerus/classifier.ex index dfb501b..2f92e2a 100644 --- a/lib/numerus/classifier.ex +++ b/lib/numerus/classifier.ex @@ -54,6 +54,18 @@ defmodule Numerus.Classifier do # this regex splits the full did into country code + number @parser ~r/(\+|011)(?9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)(?\d{1,14})$/i + # nadp n11 classification + @n11_dids %{ + "211" => "Community Services", + "311" => "Municipal Government Services", + "411" => "Directory Information", + "511" => "Traffic Information", + "611" => "Telco Customer Service & Repair", + "711" => "TDD and Relay", + "811" => "Public Utility Location", + "911" => "Emergency Services" + } + # -- public functions -- # @doc """ @@ -66,32 +78,12 @@ defmodule Numerus.Classifier do """ @spec classify(did :: bitstring()) :: {:ok, map()} | {:error, term()} def classify(did) when is_bitstring(did) do - # get the formatting - format = - cond do - is_n11?(did) -> :n11 - is_e164?(did) -> :e164 - is_1npan?(did) -> :one_npan - is_npan?(did) -> :npan - is_shortcode?(did) -> :shortcode - is_usintl?(did) -> :us_intl - true -> :unknown - end - - # get the region for this did, :nadp, or :world - region = case is_nadp?(did) do - true -> :nadp - false -> case String.match?(did, @intl_n) or String.match?(did, @intl) do - true -> :world - false -> case format do - :shortcode -> :nadp - :n11 -> :nadp - _ -> :unknown - end - end - end + classification = %{ + "region" => region(did), + "format" => format(did), + "tollstate" => tollstate(did) + } - classification = %{"region" => region, "format" => format} {:ok, classification} end def classify(_), do: {:error, :invalid_number} @@ -315,4 +307,46 @@ defmodule Numerus.Classifier do end end def metadata(_), do: {:error, :invalid_number} + + # -- private functions -- # + + # determine region for the supplied did + @spec region(did :: bitstring()) :: atom() + defp region(did) when is_bitstring(did) do + cond do + is_nadp?(did) -> :nadp + is_shortcode?(did) -> :nadp + is_usintl?(did) -> :international + is_intl?(did) -> :international + is_n11?(did) -> :n11 + true -> :unknown + end + end + + # determine the format of the supplied did + @spec format(did :: bitstring()) :: atom() + defp format(did) when is_bitstring(did) do + cond do + is_n11?(did) -> :n11 + is_e164?(did) -> :e164 + is_npan?(did) -> :npan + is_1npan?(did) -> :one_npan + is_shortcode?(did) -> :shortcode + is_usintl?(did) -> :us_intl + true -> :unknown + end + end + + # determine tollable state for this did + @spec tollstate(did :: bitstring()) :: atom() + defp tollstate(did) when is_bitstring(did) do + cond do + is_tollfree?(did) -> :tollfree_us + is_shortcode?(did) -> :shortcode + is_n11?(did) -> :emergency + is_nadp?(did) -> :us_toll + is_usintl?(did) or is_intl?(did) -> :international + true -> :unkown + end + end end diff --git a/lib/numerus/formatter.ex b/lib/numerus/formatter.ex index a695214..7b58d1d 100644 --- a/lib/numerus/formatter.ex +++ b/lib/numerus/formatter.ex @@ -82,7 +82,7 @@ defmodule Numerus.Formatter do end end - def normalize(_,_), do: {:error, :invalid_format} + def normalize(_, _), do: {:error, :invalid_format} def normalize(did) when is_bitstring(did) do case Classifier.classify(did) do @@ -112,7 +112,7 @@ defmodule Numerus.Formatter do {:ok, result} -> "+1 (#{result["area_code"]}) #{result["exch"]} #{result["number"]}" end - :world -> + :international -> case Classifier.extract(did) do {:error, _} -> did {:ok, result} -> @@ -157,7 +157,7 @@ defmodule Numerus.Formatter do case Classifier.classify(did) do {:ok, %{"region" => region, "format" => format}} -> case region do - :world -> + :international -> # npan is exclusively for NADP regions :error :nadp -> @@ -186,7 +186,7 @@ defmodule Numerus.Formatter do case Classifier.classify(did) do {:ok, %{"region" => region, "format" => format}} -> case region do - :world -> + :international -> # npan is exclusively for NADP regions :error :nadp -> @@ -216,7 +216,7 @@ defmodule Numerus.Formatter do {:ok, %{"region" => region, "format" => format}} -> case region do :nadp -> :error - :world -> + :international -> case format do :us_intl -> did :e164 -> String.replace(did, ~r/\+/, "011") diff --git a/mix.exs b/mix.exs index 217ee9b..60ea8a0 100644 --- a/mix.exs +++ b/mix.exs @@ -26,7 +26,7 @@ defmodule Numerus.MixProject do def project do [ app: :numerus, - version: "0.1.5", + version: "0.2.0", elixir: "~> 1.14", start_permanent: Mix.env() == :prod, deps: deps(),