Skip to content

Commit

Permalink
Handle added trips in finder_api
Browse files Browse the repository at this point in the history
FinderApi.departures implicitly assumed that all trips have associated
schedules, which is not true of added trips. This assumption led to a
nil creeping into an unexpected place, leading ultimately to an
UndefinedFunctionError. This case is now accounted for.
  • Loading branch information
phildarnowsky committed Jan 16, 2020
1 parent 3443052 commit 5a13770
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
27 changes: 18 additions & 9 deletions apps/site/lib/site_web/controllers/schedule/finder_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,11 @@ defmodule SiteWeb.ScheduleController.FinderApi do
@spec enhance_journeys(Journey.t()) :: map
defp enhance_journeys(%{departure: departure} = journey) do
now = Timex.now()
diff = DateTime.diff(Timex.now(), departure.schedule.time)

time_map =
departure
|> TransitNearMe.build_time_map(now: now)
|> recent_departure(departure.prediction, diff)
|> recent_departure(departure, now)

Map.put(journey, :realtime, time_map)
end
Expand All @@ -177,13 +176,23 @@ defmodule SiteWeb.ScheduleController.FinderApi do
# a prediction's status and limit recent trips to a certain time range.
# NOTE: Only works for N/S Station and Back Bay, and predictions will
# drop off (become nil) anytime during the duration of the trip.
defp recent_departure({_, details}, %{status: "Departed"} = prediction, time_elapsed)
when time_elapsed <= @recent_departure_max_age do
Map.put(details, :prediction, %{
time: details.scheduled_time,
track: prediction.track,
status: "Departed"
})
defp recent_departure(
{_, details},
%{schedule: schedule, prediction: %{status: "Departed"} = prediction},
now
)
when not is_nil(schedule) do
time_elapsed = DateTime.diff(now, schedule.time)

if time_elapsed <= @recent_departure_max_age do
Map.put(details, :prediction, %{
time: details.scheduled_time,
track: prediction.track,
status: "Departed"
})
else
details
end
end

defp recent_departure({_, details}, _, _), do: details
Expand Down
20 changes: 20 additions & 0 deletions apps/site/test/site_web/controllers/schedule/finder_api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ defmodule SiteWeb.ScheduleController.FinderApiTest do
}
] = response
end

test "can handle added trips", %{conn: conn} do
added_prediction = %Prediction{@prediction | schedule_relationship: :added}

path =
finder_api_path(conn, :departures, %{
id: "CR-Providence",
direction: "0",
stop: "place-sstat"
})

response =
conn
|> assign(:schedules_fn, fn _, _ -> [] end)
|> assign(:predictions_fn, fn _ -> [added_prediction] end)
|> get(path)
|> json_response(200)

assert [%{"departure" => %{"prediction" => added_prediction, "schedule" => nil}}] = response
end
end

describe "trip/2" do
Expand Down

0 comments on commit 5a13770

Please sign in to comment.