Skip to content

Commit 0fee5cd

Browse files
refactor: Make config optional
Co-authored-by: Brandyn Bennett <brandynbennett@users.noreply.github.com>
1 parent c67ce94 commit 0fee5cd

7 files changed

Lines changed: 60 additions & 21 deletions

File tree

.formatter.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Used by "mix format"
22
[
3-
inputs: ["lib/tzdata/period_builder.ex", "test/tz_period_builder_test.exs"]
3+
inputs: ["lib/tzdata/period_builder.ex", "test/data_loader_test.exs", "test/tz_period_builder_test.exs"]
44
]

.tool-versions

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
erlang 24.3.4.10
2+
elixir 1.14.2-otp-24
3+

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ in the release_ets sub-dir of the "data_dir" (see the "Data directory and releas
8888
When IANA releases new versions of the time zone data, this Tzdata library can be used to generate
8989
a new .ets file containing the new data.
9090

91+
## Set IANA database updates origin
92+
93+
It is also possible to override IANA database url in case you want to pin a specific database version.
94+
95+
Available versions can be found at: https://data.iana.org/time-zones/releases
96+
97+
```elixir
98+
config :tzdata, :download_url, "https://data.iana.org/time-zones/tzdata2024a.tar.gz"
99+
```
100+
101+
91102
## Changes from 0.1.x to 0.5.x
92103

93104
The 0.5.1+ versions uses ETS tables and automatically polls the IANA

lib/tzdata/data_loader.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ defmodule Tzdata.DataLoader do
44
require Logger
55
# Can poll for newest version of tz data and can download
66
# and extract it.
7+
8+
@default_download_url "https://data.iana.org/time-zones/tzdata-latest.tar.gz"
9+
710
def download_url do
8-
Application.fetch_env!(:tzdata, :download_url)
11+
Application.get_env(:tzdata, :download_url, @default_download_url)
912
end
1013

1114
def download_new do

lib/tzdata/release_updater.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ defmodule Tzdata.ReleaseUpdater do
4545
:do_nothing
4646

4747
{:ok, false} ->
48+
try do
4849
case Tzdata.DataBuilder.load_and_save_table() do
4950
{:ok, _, _} ->
5051
Tzdata.EtsHolder.new_release_has_been_downloaded()
5152

5253
{:error, error} ->
5354
{:error, error}
5455
end
55-
56+
rescue
57+
error -> Logger.error("Unable to retrieve latest tz database due to unexpected error #{error}")
58+
{:error, error}
59+
end
5660
_ ->
5761
:do_nothing
5862
end

mix.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule Tzdata.Mixfile do
3434
defp docs do
3535
[
3636
main: "readme",
37-
extras: ["README.md", "VERSION"],
37+
extras: ["README.md"],
3838
source_ref: "v#{version()}"
3939
]
4040
end
@@ -58,7 +58,7 @@ defmodule Tzdata.Mixfile do
5858
licenses: ["MIT"],
5959
maintainers: ["Lau Taarnskov"],
6060
links: %{"GitHub" => "https://github.com/lau/tzdata"},
61-
files: ~w(lib priv mix.exs README* LICENSE*
61+
files: ~w(lib priv mix.exs README* LICENSE* VERSION
6262
CHANGELOG*)
6363
}
6464
end

test/data_loader_test.exs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,45 @@ defmodule Tzdata.DataLoaderTest do
66

77
setup :verify_on_exit!
88

9+
@default_download_url "https://data.iana.org/time-zones/tzdata-latest.tar.gz"
10+
11+
@config_download_url "https://data.iana.org/time-zones/tzdata2024a.tar.gz"
12+
13+
@custom_download_url "https://data.iana.org/time-zones/tzdata2024b.tar.gz"
14+
915
setup do
1016
client = Application.get_env(:tzdata, :http_client)
11-
Application.put_env(:tzdata, :http_client, Tzdata.HTTPClient.Mock)
1217

13-
Application.put_env(
14-
:tzdata,
15-
:download_url,
16-
"https://data.iana.org/time-zones/tzdata-latest.tar.gz"
17-
)
18+
:ok = Application.put_env(:tzdata, :http_client, Tzdata.HTTPClient.Mock)
1819

1920
on_exit(fn ->
2021
Application.put_env(:tzdata, :http_client, client)
2122
:ok
2223
end)
2324
end
2425

25-
describe "download_new/1" do
26-
test "when url not given, should download content from default url" do
27-
expect(Mock, :get, fn "https://data.iana.org/time-zones/tzdata-latest.tar.gz",
28-
_,
29-
[follow_redirect: true] ->
26+
describe "download_new/0" do
27+
test "when download_url config not set, should download content from default url" do
28+
expect(Mock, :get, fn @default_download_url, _, [follow_redirect: true] ->
29+
{:ok,
30+
{200, [{"Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT"}],
31+
File.read!("test/tzdata_fixtures/tzdata2024a.tar.gz")}}
32+
end)
33+
34+
assert {:ok, 451_270, "2024a", _new_dir_name, "Wed, 21 Oct 2015 07:28:00 GMT"} =
35+
Tzdata.DataLoader.download_new()
36+
end
37+
38+
test "when download_url config set, should download content from given url" do
39+
download_url = Application.get_env(:tzdata, :download_url)
40+
:ok = Application.put_env(:tzdata, :download_url, @config_download_url)
41+
42+
on_exit(fn ->
43+
Application.put_env(:tzdata, :download_url, download_url)
44+
:ok
45+
end)
46+
47+
expect(Mock, :get, fn @config_download_url, _, [follow_redirect: true] ->
3048
{:ok,
3149
{200, [{"Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT"}],
3250
File.read!("test/tzdata_fixtures/tzdata2024a.tar.gz")}}
@@ -35,18 +53,18 @@ defmodule Tzdata.DataLoaderTest do
3553
assert {:ok, 451_270, "2024a", _new_dir_name, "Wed, 21 Oct 2015 07:28:00 GMT"} =
3654
Tzdata.DataLoader.download_new()
3755
end
56+
end
3857

39-
test "when url given, should download content from given url" do
40-
expect(Mock, :get, fn "https://data.iana.org/time-zones/tzdata2024a.tar.gz", _, _ ->
58+
describe "download_new/1" do
59+
test "should download content from given url" do
60+
expect(Mock, :get, fn @custom_download_url, _, _ ->
4161
{:ok,
4262
{200, [{"Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT"}],
4363
File.read!("test/tzdata_fixtures/tzdata2024a.tar.gz")}}
4464
end)
4565

4666
assert {:ok, 451_270, "2024a", _new_dir_name, "Wed, 21 Oct 2015 07:28:00 GMT"} =
47-
Tzdata.DataLoader.download_new(
48-
"https://data.iana.org/time-zones/tzdata2024a.tar.gz"
49-
)
67+
Tzdata.DataLoader.download_new(@custom_download_url)
5068
end
5169
end
5270
end

0 commit comments

Comments
 (0)