Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/location.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ defmodule Location do
Logger.debug("Loading location database #{inspect(module)} took: #{time}s")
:ok
end

def version() do
version_file = Application.app_dir(:location, "priv/version")

File.read!(version_file)
Copy link
Contributor

@ruslandoga ruslandoga Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be compiled into the function? I'd like to remove most of the runtime behaviour from this library (wanted to discuss it with @ukutaht on the next call) ... Gettext-style, kind of. Probably doesn't matter since it wouldn't change the public API.

end
end
5 changes: 5 additions & 0 deletions lib/location/city.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ defmodule Location.City do
|> Stream.run()
end

def all() do
:ets.tab2list(@ets_table_by_id)
|> Enum.map(fn {id, {name, country_code}} -> to_struct(id, name, country_code) end)
end

@doc """
Finds city by GeoNames ID.
"""
Expand Down
5 changes: 5 additions & 0 deletions lib/location/subdivision.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ defmodule Location.Subdivision do
end)
end

def all() do
:ets.tab2list(@ets_table)
|> Enum.map(fn {_, entry} -> entry end)
end

def search_subdivision(search_phrase) do
search_phrase = String.downcase(search_phrase)

Expand Down
7 changes: 7 additions & 0 deletions mix_tasks/scraper.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Location.Scraper do
@version_file Application.app_dir(:location, "/priv/version")

def write_date_to_version() do
File.write!(@version_file, Date.to_iso8601(Date.utc_today()))
end
end
2 changes: 2 additions & 0 deletions mix_tasks/update_english_translations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ defmodule Mix.Tasks.UpdateEnglishTranslations do
|> Jason.encode_to_iodata!(pretty: true)

File.write!(@translations_dest, json)

Location.Scraper.write_date_to_version()
end

manual_code_to_en_name = %{
Expand Down
4 changes: 3 additions & 1 deletion mix_tasks/update_geoname_data.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Mix.Tasks.UpdateGeonameData do
use Mix.Task

@allcountries_src "https://download.geonames.org/export/dump/allCountries.zip"
# @allcountries_src "https://download.geonames.org/export/dump/allCountries.zip"
@allcountries_dest Application.app_dir(:location, "/priv/geonames.csv")

@doc """
Expand Down Expand Up @@ -30,6 +30,8 @@ defmodule Mix.Tasks.UpdateGeonameData do
IO.puts("Writing result to #{@allcountries_dest}")

File.write!(@allcountries_dest, Enum.join(result, "\n"))

Location.Scraper.write_date_to_version()
end

defp reduce_chunk(row, result) do
Expand Down
2 changes: 2 additions & 0 deletions mix_tasks/update_iso_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ defmodule Mix.Tasks.UpdateIsoData do

new_subdivisions = Jason.encode_to_iodata!(%{"3166-2" => new_subdivisions}, pretty: true)
File.write!(@subdivisions_dest, new_subdivisions)

Location.Scraper.write_date_to_version()
end
end
1 change: 1 addition & 0 deletions priv/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-07-09
39 changes: 39 additions & 0 deletions test/location_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ defmodule LocationTest do
assert match.alpha_2 == "EE"
assert match.name == "Estonia"
end

test "&all/0" do
countries = Location.Country.all()

assert length(countries) > 200 and length(countries) < 1_000

country = Enum.find(countries, &(&1.alpha_2 == "EE"))

assert country == %Location.Country{
alpha_2: "EE",
alpha_3: "EST",
name: "Estonia",
flag: "🇪🇪"
}
end
end

describe "subdivision" do
Expand Down Expand Up @@ -146,6 +161,21 @@ defmodule LocationTest do
# LU
assert Location.get_subdivision("LU-CL").name == "Clervaux"
end

test "&all/0" do
subdivisions = Location.Subdivision.all()

assert length(subdivisions) > 1_000 and length(subdivisions) < 10_000

subdivision = Enum.find(subdivisions, &(&1.code == "EE-79"))

assert subdivision == %Location.Subdivision{
name: "Tartumaa",
code: "EE-79",
country_code: "EE",
type: "County"
}
end
end

describe "city" do
Expand Down Expand Up @@ -176,5 +206,14 @@ defmodule LocationTest do
assert city.id == 8_349_432
assert city.name == "Springfield"
end

test "&all/0" do
cities = Location.City.all()

assert length(cities) > 100_000 and length(cities) < 1_000_000

city = Enum.find(cities, &(&1.id == 588_335))
assert city == %Location.City{country_code: "EE", name: "Tartu", id: 588_335}
end
end
end