Просмотр исходного кода

Move location_store data file into priv directory

theenglishway (time) 4 лет назад
Родитель
Сommit
069a208d50
2 измененных файлов с 5 добавлено и 4 удалено
  1. 5 4
      lib/vaccins/location_store.ex
  2. 0 0
      priv/data/location_store.json

+ 5 - 4
lib/vaccins/location_store.ex

@@ -83,7 +83,6 @@ defmodule Vaccins.LocationStore do
   import Ecto.Changeset
   alias Vaccins.Queries.Doctolib
   @name Vaccins.LocationStore
-  @file_path "./location_store.json"
 
   def start_link(opts), do: GenServer.start_link(__MODULE__, opts, name: @name)
 
@@ -154,7 +153,7 @@ defmodule Vaccins.LocationStore do
       %{locations: locations |> Map.new(&{&1.id, &1})}
     else
       {:error, reason} ->
-        Logger.error("Could not read file #{@file_path} : #{reason}")
+        Logger.error("Could not read file #{file_path()} : #{reason}")
         %{locations: %{}}
     end
   end
@@ -162,7 +161,7 @@ defmodule Vaccins.LocationStore do
   defp read_file(),
     do:
       with(
-        {:ok, content} <- File.read(@file_path),
+        {:ok, content} <- file_path() |> File.read(),
         {:ok, decoded} <- Jason.decode(content, keys: :atoms),
         do: {:ok, decoded |> Enum.map(&Location.from_json/1)}
       )
@@ -174,6 +173,8 @@ defmodule Vaccins.LocationStore do
       with(
         {:ok, encoded} <-
           Jason.encode(locations |> Enum.map(&(&1 |> Map.from_struct())), pretty: true),
-        do: File.write(@file_path, encoded)
+        do: file_path() |> File.write(encoded)
       )
+
+  defp file_path(), do: Application.app_dir(:vaccins, ["priv", "data", "location_store.json"])
 end

location_store.json → priv/data/location_store.json