Skip to content

Commit

Permalink
handle cases where only one prediction exists
Browse files Browse the repository at this point in the history
use with and pattern matching to handle sequence of possible errors
  • Loading branch information
sloanelybutsurely committed May 16, 2024
1 parent 391e82f commit 94b2052
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lib/screens/v2/widget_instance/line_map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,6 @@ defmodule Screens.V2.WidgetInstance.LineMap do
def serialize_scheduled_departure(_departures, _direction_id, _stops, true = _is_terminal?),
do: nil

def serialize_scheduled_departure([] = _departures, _direction_id, _stops, _is_terminal?),
do: nil

def serialize_scheduled_departure(departures, direction_id, stops, _is_terminal?) do
# Number of departures with predictions (not just schedules) in this direction
prediction_count =
Expand All @@ -282,16 +279,18 @@ defmodule Screens.V2.WidgetInstance.LineMap do
if prediction_count < 2 do
%{name: origin_stop_name} = Enum.at(stops, 0)

{:ok, local_time} =
departures
|> Enum.filter(fn d -> is_nil(d.prediction) end)
|> Enum.at(0)
|> Departure.time()
|> DateTime.shift_zone("America/New_York")
if departure = Enum.find(departures, &is_nil(&1.prediction)) do
{:ok, local_time} =
departure
|> Departure.time()
|> DateTime.shift_zone("America/New_York")

{:ok, timestamp} = Timex.format(local_time, "{h12}:{m}")
{:ok, timestamp} = Timex.format(local_time, "{h12}:{m}")

%{timestamp: timestamp, station_name: origin_stop_name}
%{timestamp: timestamp, station_name: origin_stop_name}
else
nil
end
else
nil
end
Expand Down
6 changes: 6 additions & 0 deletions test/screens/v2/widget_instance/line_map_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ defmodule Screens.V2.WidgetInstance.LineMapTest do
assert nil == LineMap.serialize_scheduled_departure([], direction_id, stops, false)
end

test "returns nil when there are only predictions", %{stops: stops} do
direction_id = 1
d1 = %Departure{prediction: %Prediction{trip: %Trip{direction_id: direction_id}}}
assert nil == LineMap.serialize_scheduled_departure([d1], direction_id, stops, false)
end

test "returns the correct origin", %{stops: stops} do
direction_id = 1

Expand Down

0 comments on commit 94b2052

Please sign in to comment.