Skip to content

Commit

Permalink
Merge pull request #8 from Recruitee/subregions-gb-id-862kpyz5k
Browse files Browse the repository at this point in the history
Subregions for Great Britain, Indonesia and Belgium
  • Loading branch information
WojciechJasinski authored Apr 25, 2024
2 parents f3c6643 + b9a37e1 commit 57f32e8
Show file tree
Hide file tree
Showing 10 changed files with 23,059 additions and 8,256 deletions.
42 changes: 37 additions & 5 deletions dev/cadastre_dev/source/subdivisions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ defmodule CadastreDev.Source.Subdivisions do
@json_path "data/iso_3166-2.json"
@po_dir "iso_3166-2"

@depths "priv/dev/subdivision_depths.json" |> File.read!() |> Jason.decode!()

@impl Source
def msgid_per_id do
case API.download_json(@json_path) do
%{"3166-2" => list} when is_list(list) ->
list = list |> Enum.filter(&valid?/1) |> Enum.map(&Map.put_new(&1, "parent", nil))

list
|> Enum.flat_map(fn
%{"parent" => _} -> []
%{"code" => id, "name" => msgid} -> [{id, msgid}]
_ -> []
end)
|> fetch_country_ids()
|> Enum.map(&fetch_subdivisions(list, &1, Map.get(@depths, &1, 1)))
|> List.flatten()
|> Enum.into(%{})

json ->
Expand All @@ -28,4 +30,34 @@ defmodule CadastreDev.Source.Subdivisions do

@impl Source
def msgstr_per_msgid_per_lang, do: Source.load_msgstr_per_msgid_per_lang(@po_dir)

defp fetch_country_ids(list) do
list
|> Enum.map(fn
%{"code" => code} ->
String.slice(code, 0..1)

_ ->
nil
end)
|> Enum.uniq()
|> Enum.reject(&is_nil/1)
end

defp fetch_subdivisions(list, country_id, depth, parent \\ nil)

defp fetch_subdivisions(_list, _country_id, 0 = _depth, _parent), do: []

defp fetch_subdivisions(list, country_id, depth, parent) do
list
|> Enum.filter(&(String.starts_with?(&1["code"], country_id) and &1["parent"] == parent))
|> Enum.map(fn %{"code" => id, "name" => msgid} ->
subdivision = {id, msgid}
children = fetch_subdivisions(list, country_id, depth - 1, id)
[subdivision | children]
end)
end

defp valid?(%{"code" => _code, "name" => _name}), do: true
defp valid?(_), do: false
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Cadastre.MixProject do
use Mix.Project

@source_url "https://github.com/Recruitee/cadastre"
@version "0.2.14"
@version "0.2.15"

def project do
[
Expand Down
Binary file modified priv/data/countries.etf
Binary file not shown.
Binary file modified priv/data/languages.etf
Binary file not shown.
Binary file modified priv/data/subdivisions.etf
Binary file not shown.
Loading

0 comments on commit 57f32e8

Please sign in to comment.