diff --git a/config/config.exs b/config/config.exs index f199263..305c695 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,4 +1,3 @@ # This file is responsible for configuring your application # and its dependencies with the aid of the Mix.Config module. import Config - diff --git a/lib/mix/tasks/update_timezones_geojson.ex b/lib/mix/tasks/update_timezones_geojson.ex index 8c66fae..de70de9 100644 --- a/lib/mix/tasks/update_timezones_geojson.ex +++ b/lib/mix/tasks/update_timezones_geojson.ex @@ -34,16 +34,17 @@ defmodule Mix.Tasks.TzWorld.Update do _other -> Mix.raise( - """ - Invalid arguments found. `mix tz_world.update` accepts the following: - --include-oceans - --no-include-oceans (default) - --force - --no-force (default) - --trace - --no-trace (default) - """, - exit_status: 1) + """ + Invalid arguments found. `mix tz_world.update` accepts the following: + --include-oceans + --no-include-oceans (default) + --force + --no-force (default) + --trace + --no-trace (default) + """, + exit_status: 1 + ) end end diff --git a/lib/tz_world.ex b/lib/tz_world.ex index d51b0b4..36e48ca 100644 --- a/lib/tz_world.ex +++ b/lib/tz_world.ex @@ -13,7 +13,7 @@ defmodule TzWorld do TzWorld.Backend.Dets, TzWorld.Backend.DetsWithIndexCache, TzWorld.Backend.Ets, - TzWorld.Backend.EtsWithIndexCache, + TzWorld.Backend.EtsWithIndexCache ] @doc """ @@ -99,7 +99,7 @@ defmodule TzWorld do """ @spec timezone_at(Geo.Point.t(), backend) :: - {:ok, String.t()} | {:error, atom} + {:ok, String.t()} | {:error, atom} def timezone_at(point, backend \\ fetch_backend()) @@ -108,7 +108,7 @@ defmodule TzWorld do end @spec timezone_at(Geo.PointZ.t(), backend) :: - {:ok, String.t()} | {:error, atom} + {:ok, String.t()} | {:error, atom} def timezone_at(%PointZ{coordinates: {lng, lat, _alt}}, backend) when is_atom(backend) do point = %Point{coordinates: {lng, lat}} @@ -116,7 +116,7 @@ defmodule TzWorld do end @spec timezone_at({lng :: number, lat :: number}, backend) :: - {:ok, String.t()} | {:error, atom} + {:ok, String.t()} | {:error, atom} def timezone_at({lng, lat}, backend) when is_lng(lng) and is_lat(lat) do point = %Geo.Point{coordinates: {lng, lat}} @@ -171,7 +171,7 @@ defmodule TzWorld do """ @spec all_timezones_at(Geo.Point.t(), backend) :: - {:ok, [String.t()]} + {:ok, [String.t()]} def all_timezones_at(point, backend \\ fetch_backend()) @@ -180,7 +180,7 @@ defmodule TzWorld do end @spec all_timezones_at(Geo.PointZ.t(), backend) :: - {:ok, [String.t()]} + {:ok, [String.t()]} def all_timezones_at(%PointZ{coordinates: {lng, lat, _alt}}, backend) when is_atom(backend) do point = %Point{coordinates: {lng, lat}} @@ -188,7 +188,7 @@ defmodule TzWorld do end @spec all_timezones_at({lng :: number, lat :: number}, backend) :: - {:ok, [String.t()]} + {:ok, [String.t()]} def all_timezones_at({lng, lat}, backend) when is_lng(lng) and is_lat(lat) do point = %Geo.Point{coordinates: {lng, lat}} @@ -246,9 +246,10 @@ defmodule TzWorld do |> Enum.reject(&is_nil/1) Enum.find(backends, &Process.whereis/1) || - raise(RuntimeError, + raise( + RuntimeError, "No TzWorld backend appears to be running. " <> - "please add one of #{inspect backends} to your supervision tree" + "please add one of #{inspect(backends)} to your supervision tree" ) end diff --git a/lib/tz_world/backend.ex b/lib/tz_world/backend.ex index 360b7fb..b247211 100644 --- a/lib/tz_world/backend.ex +++ b/lib/tz_world/backend.ex @@ -27,5 +27,4 @@ defmodule TzWorld.Backend do Reloads the (potentially updated) timezone data """ @callback reload_timezone_data :: {:ok, term} - end diff --git a/lib/tz_world/backend/dets.ex b/lib/tz_world/backend/dets.ex index 9592a7c..07eaceb 100644 --- a/lib/tz_world/backend/dets.ex +++ b/lib/tz_world/backend/dets.ex @@ -56,12 +56,14 @@ defmodule TzWorld.Backend.Dets do @doc false def handle_continue(:open_dets_file, _state) do case get_geodata_table() do - {:error, {:file_error, _, :enoent}} -> - {:noreply, {:error, :enoent}} - {:error, {:not_closed, _path}} -> - raise "NOT CLOSED" - {:ok, __MODULE__} -> - {:noreply, {:ok, __MODULE__}} + {:error, {:file_error, _, :enoent}} -> + {:noreply, {:error, :enoent}} + + {:error, {:not_closed, _path}} -> + raise "NOT CLOSED" + + {:ok, __MODULE__} -> + {:noreply, {:ok, __MODULE__}} end end @@ -112,7 +114,7 @@ defmodule TzWorld.Backend.Dets do point |> select_candidates() |> Enum.filter(&TzWorld.contains?(&1, point)) - |> Enum.map(&(&1.properties.tzid)) + |> Enum.map(& &1.properties.tzid) |> wrap(:ok) end @@ -200,5 +202,4 @@ defmodule TzWorld.Backend.Dets do defp dets_options do [file: filename(), access: :read, estimated_no_objects: @slots] end - end diff --git a/lib/tz_world/backend/dets_with_index_cache.ex b/lib/tz_world/backend/dets_with_index_cache.ex index 3d9a80c..7d2ed3e 100644 --- a/lib/tz_world/backend/dets_with_index_cache.ex +++ b/lib/tz_world/backend/dets_with_index_cache.ex @@ -100,10 +100,11 @@ defmodule TzWorld.Backend.DetsWithIndexCache do @doc false def handle_continue(:open_dets_file, _state) do case get_geodata_table() do - {:error, {:file_error, _, :enoent}} -> - {:noreply, {:error, :enoent}} - {:ok, __MODULE__} -> - {:noreply, get_index_cache()} + {:error, {:file_error, _, :enoent}} -> + {:noreply, {:error, :enoent}} + + {:ok, __MODULE__} -> + {:noreply, get_index_cache()} end end @@ -162,7 +163,7 @@ defmodule TzWorld.Backend.DetsWithIndexCache do point |> select_candidates(state) |> Enum.filter(&TzWorld.contains?(&1, point)) - |> Enum.map(&(&1.properties.tzid)) + |> Enum.map(& &1.properties.tzid) |> wrap(:ok) end diff --git a/lib/tz_world/backend/ets.ex b/lib/tz_world/backend/ets.ex index bf37a30..e651a57 100644 --- a/lib/tz_world/backend/ets.ex +++ b/lib/tz_world/backend/ets.ex @@ -94,7 +94,7 @@ defmodule TzWorld.Backend.Ets do point |> select_candidates() |> Enum.filter(&TzWorld.contains?(&1, point)) - |> Enum.map(&(&1.properties.tzid)) + |> Enum.map(& &1.properties.tzid) |> wrap(:ok) end @@ -112,5 +112,4 @@ defmodule TzWorld.Backend.Ets do nil -> {:error, :time_zone_not_found} end end - end diff --git a/lib/tz_world/backend/ets_with_index_cache.ex b/lib/tz_world/backend/ets_with_index_cache.ex index 9fb1d58..a47e8cc 100644 --- a/lib/tz_world/backend/ets_with_index_cache.ex +++ b/lib/tz_world/backend/ets_with_index_cache.ex @@ -101,7 +101,7 @@ defmodule TzWorld.Backend.EtsWithIndexCache do point |> select_candidates(state) |> Enum.filter(&TzWorld.contains?(&1, point)) - |> Enum.map(&(&1.properties.tzid)) + |> Enum.map(& &1.properties.tzid) |> wrap(:ok) end diff --git a/lib/tz_world/backend/memory.ex b/lib/tz_world/backend/memory.ex index 4c34055..b061813 100644 --- a/lib/tz_world/backend/memory.ex +++ b/lib/tz_world/backend/memory.ex @@ -96,7 +96,7 @@ defmodule TzWorld.Backend.Memory do tz_data |> Enum.filter(&TzWorld.contains?(&1.properties.bounding_box, point)) |> Enum.filter(&TzWorld.contains?(&1, point)) - |> Enum.map(&(&1.properties.tzid)) + |> Enum.map(& &1.properties.tzid) |> wrap(:ok) end diff --git a/lib/tz_world/downloader.ex b/lib/tz_world/downloader.ex index 1d65b40..f05cde7 100644 --- a/lib/tz_world/downloader.ex +++ b/lib/tz_world/downloader.ex @@ -94,7 +94,6 @@ defmodule TzWorld.Downloader do {:error, :enoent} -> {latest_release, asset_url} = latest_release(include_oceans?) get_and_load_latest_release(latest_release, asset_url) - end end @@ -119,10 +118,13 @@ defmodule TzWorld.Downloader do defp get_releases(trace?) do with {:ok, json} <- get_url(@release_url), {:ok, releases} <- Jason.decode(json) do - maybe_log("Retrieved list of #{Enum.count(releases)} available timezone data releases.", trace?) + maybe_log( + "Retrieved list of #{Enum.count(releases)} available timezone data releases.", + trace? + ) + {:ok, releases} end - end def get_url(url) do @@ -159,12 +161,12 @@ defmodule TzWorld.Downloader do * `:timeout` is the number of milliseconds available for the request to complete. The default is - #{inspect @tzworld_default_timeout}. This option may also be + #{inspect(@tzworld_default_timeout)}. This option may also be set with the `CLDR_HTTP_TIMEOUT` environment variable. * `:connection_timeout` is the number of milliseconds available for the a connection to be estabklished to - the remote host. The default is #{inspect @tzworld_default_connection_timeout}. + the remote host. The default is #{inspect(@tzworld_default_connection_timeout)}. This option may also be set with the `CLDR_HTTP_CONNECTION_TIMEOUT` environment variable. @@ -233,8 +235,8 @@ defmodule TzWorld.Downloader do ``` """ - @spec get(String.t | {String.t, list()}, options :: Keyword.t) :: - {:ok, binary} | {:not_modified, any()} | {:error, any} + @spec get(String.t() | {String.t(), list()}, options :: Keyword.t()) :: + {:ok, binary} | {:not_modified, any()} | {:error, any} def get(url, options \\ []) @@ -245,7 +247,8 @@ defmodule TzWorld.Downloader do end end - def get({url, headers}, options) when is_binary(url) and is_list(headers) and is_list(options) do + def get({url, headers}, options) + when is_binary(url) and is_list(headers) and is_list(options) do case get_with_headers({url, headers}, options) do {:ok, _headers, body} -> {:ok, body} other -> other @@ -281,12 +284,12 @@ defmodule TzWorld.Downloader do * `:timeout` is the number of milliseconds available for the request to complete. The default is - #{inspect @tzworld_default_timeout}. This option may also be + #{inspect(@tzworld_default_timeout)}. This option may also be set with the `CLDR_HTTP_TIMEOUT` environment variable. * `:connection_timeout` is the number of milliseconds available for the a connection to be estabklished to - the remote host. The default is #{inspect @tzworld_default_connection_timeout}. + the remote host. The default is #{inspect(@tzworld_default_connection_timeout)}. This option may also be set with the `CLDR_HTTP_CONNECTION_TIMEOUT` environment variable. @@ -371,8 +374,8 @@ defmodule TzWorld.Downloader do """ @doc since: "2.21.0" - @spec get_with_headers(String.t | {String.t, list()}, options :: Keyword.t) :: - {:ok, list(), binary} | {:not_modified, any()} | {:error, any} + @spec get_with_headers(String.t() | {String.t(), list()}, options :: Keyword.t()) :: + {:ok, list(), binary} | {:not_modified, any()} | {:error, any} def get_with_headers(request, options \\ []) @@ -380,7 +383,8 @@ defmodule TzWorld.Downloader do get_with_headers({url, []}, options) end - def get_with_headers({url, headers}, options) when is_binary(url) and is_list(headers) and is_list(options) do + def get_with_headers({url, headers}, options) + when is_binary(url) and is_list(headers) and is_list(options) do require Logger hostname = String.to_charlist(URI.parse(url).host) @@ -392,8 +396,12 @@ defmodule TzWorld.Downloader do case URI.parse(https_proxy) do %{host: host, port: port} when is_binary(host) and is_integer(port) -> :httpc.set_options([{:https_proxy, {{String.to_charlist(host), port}, []}}]) + _other -> - Logger.bare_log(:warning, "https_proxy was set to an invalid value. Found #{inspect https_proxy}.") + Logger.bare_log( + :warning, + "https_proxy was set to an invalid value. Found #{inspect(https_proxy)}." + ) end end @@ -407,7 +415,7 @@ defmodule TzWorld.Downloader do {_, {{_version, code, message}, _headers, _body}} -> Logger.bare_log( :error, - "Failed to download #{inspect url}. " <> + "Failed to download #{inspect(url)}. " <> "HTTP Error: (#{code}) #{inspect(message)}" ) @@ -417,15 +425,15 @@ defmodule TzWorld.Downloader do if sys_message == :timeout do Logger.bare_log( :error, - "Timeout connecting to #{inspect(host)} to download #{inspect url}. " <> - "Connection time exceeded #{http_options[:connect_timeout]}ms." + "Timeout connecting to #{inspect(host)} to download #{inspect(url)}. " <> + "Connection time exceeded #{http_options[:connect_timeout]}ms." ) {:error, :connection_timeout} else Logger.bare_log( :error, - "Failed to connect to #{inspect(host)} to download #{inspect url}" + "Failed to connect to #{inspect(host)} to download #{inspect(url)}" ) {:error, sys_message} @@ -434,7 +442,7 @@ defmodule TzWorld.Downloader do {:error, {other}} -> Logger.bare_log( :error, - "Failed to download #{inspect url}. Error #{inspect other}" + "Failed to download #{inspect(url)}. Error #{inspect(other)}" ) {:error, other} @@ -442,9 +450,10 @@ defmodule TzWorld.Downloader do {:error, :timeout} -> Logger.bare_log( :error, - "Timeout downloading from #{inspect url}. " <> - "Request exceeded #{http_options[:timeout]}ms." + "Timeout downloading from #{inspect(url)}. " <> + "Request exceeded #{http_options[:timeout]}ms." ) + {:error, :timeout} end end @@ -578,7 +587,7 @@ defmodule TzWorld.Downloader do reuse_sessions: true, versions: protocol_versions(), ciphers: preferred_ciphers(), - versions: protocol_versions(), + versions: protocol_versions() ] end end @@ -618,7 +627,7 @@ defmodule TzWorld.Downloader do defp preferred_eccs do # TLS curves: X25519, prime256v1, secp384r1 preferred_eccs = [:secp256r1, :secp384r1] - :ssl.eccs() -- (:ssl.eccs() -- preferred_eccs) + :ssl.eccs() -- :ssl.eccs() -- preferred_eccs end defp secure_ssl? do @@ -632,13 +641,12 @@ defmodule TzWorld.Downloader do defp https_proxy(options) do options[:https_proxy] || - Application.get_env(:tz_world, :https_proxy) || - System.get_env("HTTPS_PROXY") || - System.get_env("https_proxy") + Application.get_env(:tz_world, :https_proxy) || + System.get_env("HTTPS_PROXY") || + System.get_env("https_proxy") end def otp_version do - :erlang.system_info(:otp_release) |> List.to_integer + :erlang.system_info(:otp_release) |> List.to_integer() end - end diff --git a/lib/tz_world/geo_data.ex b/lib/tz_world/geo_data.ex index 0175792..d68f405 100644 --- a/lib/tz_world/geo_data.ex +++ b/lib/tz_world/geo_data.ex @@ -10,8 +10,8 @@ defmodule TzWorld.GeoData do def default_data_dir do TzWorld.app_name() - |> :code.priv_dir - |> List.to_string + |> :code.priv_dir() + |> List.to_string() end def data_dir do @@ -47,7 +47,7 @@ defmodule TzWorld.GeoData do def transform_source_data(source_data, version) when is_list(source_data) do source_data - |> :erlang.list_to_binary + |> :erlang.list_to_binary() |> transform_source_data(version) end @@ -59,7 +59,7 @@ defmodule TzWorld.GeoData do |> :erlang.term_to_binary() error -> - raise RuntimeError, "Unable to unzip downloaded data. Error: #{inspect error}" + raise RuntimeError, "Unable to unzip downloaded data. Error: #{inspect(error)}" end end @@ -100,7 +100,6 @@ defmodule TzWorld.GeoData do defp calculate_bounding_box( %Geo.MultiPolygon{coordinates: polygons, properties: properties} = poly ) do - bounding_boxes = Enum.map(polygons, &calculate_bounding_box/1) properties = Map.put(properties, :bounding_box, bounding_boxes) diff --git a/test/tz_world_test.exs b/test/tz_world_test.exs index 373c8a8..743ba63 100644 --- a/test/tz_world_test.exs +++ b/test/tz_world_test.exs @@ -2,8 +2,13 @@ defmodule TzWorldTest do use ExUnit.Case doctest TzWorld - @backends [TzWorld.Backend.Memory, TzWorld.Backend.Dets, TzWorld.Backend.Ets, - TzWorld.Backend.EtsWithIndexCache, TzWorld.Backend.DetsWithIndexCache] + @backends [ + TzWorld.Backend.Memory, + TzWorld.Backend.Dets, + TzWorld.Backend.Ets, + TzWorld.Backend.EtsWithIndexCache, + TzWorld.Backend.DetsWithIndexCache + ] setup_all do for backend <- @backends do