-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rework takeover alert state handling (#331)
- Loading branch information
1 parent
2c4c6ec
commit 23a97fd
Showing
6 changed files
with
34 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 8 additions & 38 deletions
46
lib/screenplay/outfront_takeover_tool/alerts/local_fetch.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,18 @@ | ||
defmodule Screenplay.OutfrontTakeoverTool.Alerts.LocalFetch do | ||
@moduledoc false | ||
|
||
alias Screenplay.OutfrontTakeoverTool.Alerts.State | ||
|
||
@spec get_state() :: {:ok, State.t()} | :error | ||
def get_state do | ||
with {:ok, file_contents} <- do_get(), | ||
{:ok, json} <- Jason.decode(file_contents) do | ||
{:ok, State.from_json(json)} | ||
else | ||
_ -> :error | ||
end | ||
end | ||
|
||
# sobelow_skip ["Traversal.FileModule"] | ||
defp do_get do | ||
path = local_alerts_path() | ||
|
||
case File.read(path) do | ||
{:ok, contents} -> {:ok, contents} | ||
_ -> :error | ||
end | ||
end | ||
|
||
@spec put_state(State.t()) :: :ok | :error | ||
def put_state(state) do | ||
contents = state |> State.to_json() |> Jason.encode!(pretty: true) | ||
do_put(contents) | ||
@spec get_state!() :: binary() | ||
def get_state! do | ||
file_path() |> File.read!() | ||
end | ||
|
||
# sobelow_skip ["Traversal.FileModule"] | ||
defp do_put(contents) do | ||
path = local_alerts_path() | ||
|
||
case File.write(path, contents) do | ||
:ok -> :ok | ||
_ -> :error | ||
end | ||
@spec put_state!(binary()) :: :ok | ||
def put_state!(state) do | ||
file_path() |> File.write!(state) | ||
:ok | ||
end | ||
|
||
defp local_alerts_path do | ||
case Application.get_env(:screenplay, :local_alerts_path_spec) do | ||
{:priv, file_name} -> Path.join(:code.priv_dir(:screenplay), file_name) | ||
{:test, file_name} -> Path.join(~w[#{File.cwd!()} test fixtures #{file_name}]) | ||
end | ||
end | ||
defp file_path, do: :code.priv_dir(:screenplay) |> Path.join("alerts.json") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,18 @@ | ||
defmodule Screenplay.OutfrontTakeoverTool.Alerts.S3Fetch do | ||
@moduledoc false | ||
|
||
require Logger | ||
|
||
alias Screenplay.OutfrontTakeoverTool.Alerts.State | ||
|
||
@spec get_state() :: {:ok, State.t()} | :error | ||
def get_state do | ||
with {:ok, body} <- do_get(), | ||
{:ok, json} <- Jason.decode(body) do | ||
{:ok, State.from_json(json)} | ||
else | ||
_ -> :error | ||
end | ||
end | ||
|
||
defp do_get do | ||
bucket = Application.get_env(:screenplay, :alerts_s3_bucket) | ||
path = Application.get_env(:screenplay, :alerts_s3_path) | ||
get_operation = ExAws.S3.get_object(bucket, path) | ||
|
||
case ExAws.request(get_operation) do | ||
{:ok, %{body: body, status_code: 200}} -> | ||
{:ok, body} | ||
|
||
e -> | ||
Logger.error(e) | ||
:error | ||
end | ||
@spec get_state!() :: binary() | ||
def get_state! do | ||
%{body: body, status_code: 200} = ExAws.S3.get_object(bucket(), path()) |> ExAws.request!() | ||
body | ||
end | ||
|
||
@spec put_state(State.t()) :: :ok | :error | ||
def put_state(state) do | ||
contents = state |> State.to_json() |> Jason.encode!(pretty: true) | ||
do_put(contents) | ||
@spec put_state!(binary()) :: :ok | ||
def put_state!(state) do | ||
%{status_code: 200} = ExAws.S3.put_object(bucket(), path(), state) |> ExAws.request!() | ||
:ok | ||
end | ||
|
||
defp do_put(contents) do | ||
bucket = Application.get_env(:screenplay, :alerts_s3_bucket) | ||
path = Application.get_env(:screenplay, :alerts_s3_path) | ||
put_operation = ExAws.S3.put_object(bucket, path, contents) | ||
|
||
case ExAws.request(put_operation) do | ||
{:ok, %{status_code: 200}} -> :ok | ||
_ -> :error | ||
end | ||
end | ||
defp bucket, do: Application.get_env(:screenplay, :alerts_s3_bucket) | ||
defp path, do: Application.get_env(:screenplay, :alerts_s3_path) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
lib/screenplay/outfront_takeover_tool/alerts/test_fetch.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Screenplay.OutfrontTakeoverTool.Alerts.TestFetch do | ||
@moduledoc false | ||
|
||
@spec get_state!() :: binary() | ||
def get_state! do | ||
File.read!("test/fixtures/alerts.json") | ||
end | ||
|
||
@spec put_state!(binary()) :: :ok | ||
def put_state!(_state) do | ||
:ok | ||
end | ||
end |