BeamLabCountries (beamlab_countries v1.0.6)

View Source

Module for providing countries related functions.

Summary

Functions

Returns all countries.

Returns the total number of countries.

Checks if country for specific attribute and value exists.

Filters countries by given attribute.

Returns one country given its alpha2 country code, or nil if not found.

Returns one country given its alpha2 country code, or raises if not found.

Returns one country matching the given attribute and value, or nil if not found.

Returns one country given its alpha3 country code, or nil if not found.

Functions

all()

Returns all countries.

count()

Returns the total number of countries.

Examples

iex> BeamLabCountries.count()
250

exists?(attribute, value)

Checks if country for specific attribute and value exists.

Returns boolean.

Examples

iex> BeamLabCountries.exists?(:name, "Poland")
true

iex> BeamLabCountries.exists?(:name, "Polande")
false

filter_by(attribute, value)

Filters countries by given attribute.

Returns a list of BeamLabCountries.Country structs

Examples

iex> countries = BeamLabCountries.filter_by(:region, "Europe")
iex> Enum.count(countries)
51
iex> Enum.map(countries, &Map.get(&1, :alpha2)) |> Enum.take(5)
["AD", "AL", "AT", "AX", "BA"]

iex> countries = BeamLabCountries.filter_by(:unofficial_names, "Reino Unido")
iex> Enum.count(countries)
1
iex> Enum.map(countries, &Map.get(&1, :name)) |> List.first
"United Kingdom of Great Britain and Northern Ireland"

get(country_code)

Returns one country given its alpha2 country code, or nil if not found.

Examples

iex> %BeamLabCountries.Country{name: name} = BeamLabCountries.get("PL")
iex> name
"Poland"

iex> BeamLabCountries.get("INVALID")
nil

get!(country_code)

Returns one country given its alpha2 country code, or raises if not found.

Examples

iex> %BeamLabCountries.Country{name: name} = BeamLabCountries.get!("PL")
iex> name
"Poland"

get_by(attribute, value)

Returns one country matching the given attribute and value, or nil if not found.

Examples

iex> %BeamLabCountries.Country{alpha2: alpha2} = BeamLabCountries.get_by(:name, "Poland")
iex> alpha2
"PL"

iex> BeamLabCountries.get_by(:name, "Atlantis")
nil

get_by_alpha3(country_code)

Returns one country given its alpha3 country code, or nil if not found.

Examples

iex> %BeamLabCountries.Country{name: name} = BeamLabCountries.get_by_alpha3("POL")
iex> name
"Poland"

iex> BeamLabCountries.get_by_alpha3("INVALID")
nil