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
10 changes: 8 additions & 2 deletions lib/signs_ui_web/controllers/messages_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,14 @@ defmodule SignsUiWeb.MessagesController do
end

defp decode_zones(zones) do
{Enum.at(zones, 0) |> String.split("-") |> List.first(),
MapSet.new(zones, &(String.split(&1, "-") |> List.last()))}
case Enum.at(zones, 0) do
nil ->
{nil, MapSet.new()}

zone ->
{String.split(zone, "-") |> List.first(),
MapSet.new(zones, &(String.split(&1, "-") |> List.last()))}
end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My clever-pattern-matching neuron is activating:

defp decode_zones([]), do: {nil, MapSet.new()}

defp decode_zones([first | _] = zones) do
  # ...
end

Opinions on whether this is more readable may vary.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Definitely would have done that, but the parameter is a MapSet, which is less amenable to matching.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ohhhhhh. I guess I assumed it was a list since you were explicitly picking out the "first" item, not realizing this was just a way to pick a single arbitrary item out of a set.

end

defp parse_visual_data(conn) do
Expand Down
42 changes: 42 additions & 0 deletions test/signs_ui_web/controllers/messages_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,48 @@ defmodule SignsUiWeb.MessagesControllerTest do
sign_id: "BAIR-e"
})
end

test "play", %{conn: conn} do
subscribe_and_join!(socket(), SignsUiWeb.SignsChannel, "signs:all", %{})

conn
|> add_api_req_header()
|> post(messages_path(conn, :play), %{
"zones" => ["BAIR-e"],
"visual_data" => %{
"pages" => [%{"top" => "top", "bottom" => "bottom", "duration" => 6}]
},
"audio_data" => [%{"type" => "chime"}],
"expiration" => 180
})

assert_broadcast("sign_update", %{
audios: [
%{
station: "BAIR",
zones: ["e"],
visual_data: %{pages: [%{top: "top", bottom: "bottom", duration: 6}]}
}
],
sign_id: "BAIR-e"
})
end

test "ignores messages with no zones", %{conn: conn} do
subscribe_and_join!(socket(), SignsUiWeb.SignsChannel, "signs:all", %{})

assert %{status: 200} =
conn
|> add_api_req_header()
|> post(messages_path(conn, :play), %{
"zones" => [],
"visual_data" => nil,
"audio_data" => [%{"type" => "chime"}],
"expiration" => 180
})

refute_broadcast("sign_update", %{})
end
end

defp add_api_req_header(conn) do
Expand Down
Loading