View Source LivePhone.Country (live_phone v0.7.0)

The LivePhone.Country struct holds minimal information about a country, but it should be enough data for LivePhone to work its magic.

Link to this section Summary

Functions

Converts the given iso_country tuple into a LivePhone.Country struct.

This function will retrieve a Country by its country code. Also accepts a %PhoneNumber{} struct.

This function returns all known countries as LivePhone.Country structs, sorted alphabetically by country name.

Link to this section Types

@type phone() :: %ExPhoneNumber.Model.PhoneNumber{
  country_code: term(),
  country_code_source: term(),
  extension: term(),
  italian_leading_zero: term(),
  national_number: term(),
  number_of_leading_zeros: term(),
  preferred_domestic_carrier_code: term(),
  raw_input: term()
}
@type t() :: %LivePhone.Country{
  code: String.t(),
  flag_emoji: String.t(),
  name: String.t(),
  preferred: boolean(),
  region_code: String.t()
}

Link to this section Functions

@spec from_iso({String.t(), %{required(String.t()) => String.t()}}) :: t()

Converts the given iso_country tuple into a LivePhone.Country struct.

examples

Examples

iex> ISO.countries() |> Map.to_list() |> Enum.find(fn {cc, _} -> cc == "SL" end) |> LivePhone.Country.from_iso()
%LivePhone.Country{
  preferred: false,
  region_code: "232",
  flag_emoji: "πŸ‡ΈπŸ‡±",
  code: "SL",
  name: "Sierra Leone"
}

iex> LivePhone.Country.from_iso({"US", %{"name" => "United States"}})
%LivePhone.Country{
  preferred: false,
  region_code: "1",
  flag_emoji: "πŸ‡ΊπŸ‡Έ",
  code: "US",
  name: "United States"
}
@spec get(
  %ExPhoneNumber.Model.PhoneNumber{
    country_code: term(),
    country_code_source: term(),
    extension: term(),
    italian_leading_zero: term(),
    national_number: term(),
    number_of_leading_zeros: term(),
    preferred_domestic_carrier_code: term(),
    raw_input: term()
  }
  | String.t()
) :: {:ok, t()} | {:error, :not_found}

This function will retrieve a Country by its country code. Also accepts a %PhoneNumber{} struct.

examples

Examples

  iex> LivePhone.Country.get("US")
  {:ok, %LivePhone.Country{code: "US", flag_emoji: "πŸ‡ΊπŸ‡Έ", name: "United States of America (the)", preferred: false, region_code: "1"}}

  iex> LivePhone.Country.get("FAKE")
  {:error, :not_found}
@spec list([String.t()]) :: [t()]

This function returns all known countries as LivePhone.Country structs, sorted alphabetically by country name.

Optionally you can specify a list of preferred country codes, and these will be put at the top of the list.

# This will return everything alphabetically
abc_countries = LivePhone.Country.list()

# This will return it alphabetically as well, but push
# the US and GB `LivePhone.Country` structs to the top
# of the list.
my_countries = LivePhone.Country.list(["US", "GB"])