Skip to content

annatel/antl_phonenumber

Repository files navigation

AntlPhonenumber

GitHub Workflow Status GitHub issues License Hex.pm Hex.pm

AntlPhonenumber is a third-party port of the google libphonenumber library.
The library is based on google libphonenumber cpp implementation and implements NIF functions.

Installation

If available in Hex, the package can be installed by adding antl_phonenumber to your list of dependencies in mix.exs:

def deps do
  [
    {:antl_phonenumber, "~> 0.1.4"}
  ]
end

The library supposes that google libphonenumber is installed. Please refer to the installation doc of C++ version to build the library.

Usage

AntlPhonenumber provides a set of predicates and functions to check or manipulate a number.

Ecto types

It also provides a PlusE164 Ecto type ensuring the number will be dump and load only in plus e164 format and a E164 Ecto type ensuring the number will be dump and load only in e164 format.
A number in any other format won't be cast, as country code is required to format it in plus e164 format.
However, if iso_country_code is specified, the type will ensure the number belongs to the given country code and casting will be performed accordingly to this country code.
In that case, any number of another country (a.k.a any plus_e164 of another country) will cause casting to fail.

Example:

    import AntlPhonenumber.Ecto.PlusE164

    embedded_schema do
      field(:number, PlusE164)
      field(:french_number, PlusE164, iso_country_code: "FR")

       field(:e164, E164)
      field(:french_e164, E164, iso_country_code: "FR")
    end

PlusE164 and E164 fit our needs but any type can be created to express a combination of number characteristics.
For instance, one could need a type for local french numbers. It is easy to implement another type based on existing implementation.

Ecto changeset

Changeset validations are also a AntlPhonenumber feature.
For the moment, the validators are:

  validate_iso_country_code/3
  validate_type/3

Ranges

Number ranges are also supported and implement the Enumerable protocol so we can apply Enum functions on them. The ranges can be ascendant or descendant.
For example:

  range = AntlPhonenumber.Range.new("+33148413200", "+33148413210", "FR")
  range = AntlPhonenumber.Range.new("0148413200", "0148413210", "FR")
  range = AntlPhonenumber.Range.new("0148413210", "0148413200", "FR")

Testing

Google provides number examples. The function get_plus_e164_example/2, receiving a country code and a number type allow to request an example number.
It is useful for testing.

  AntlPhonenumber.get_plus_e164_example("FR", :mobile)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •