BeamLabCountries (beamlab_countries v1.0.8)

View Source

Module for providing countries related functions.

Summary

Functions

Returns all countries.

Returns the total number of countries.

Returns all EU member 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

eu_members()

Returns all EU member countries.

Examples

iex> eu = BeamLabCountries.eu_members()
iex> length(eu)
27
iex> Enum.map(eu, &Map.get(&1, :alpha2)) |> Enum.take(3)
["AT", "BE", "BG"]

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(criteria)

filter_by(attribute, value)

Filters countries by given attribute.

Returns a list of BeamLabCountries.Country structs

Single attribute

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"

Multiple attributes

iex> countries = BeamLabCountries.filter_by(region: "Europe", eu_member: true)
iex> length(countries)
26

iex> countries = BeamLabCountries.filter_by(region: "Europe", eea_member: true)
iex> length(countries)
30

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