Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor of routes-serving-stop fetching #2120

Merged
merged 1 commit into from
Aug 1, 2024
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
2 changes: 1 addition & 1 deletion lib/screens/routes/route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule Screens.Routes.Route do
end

@doc "Fetches routes that serve the given stop."
@spec serving_stop(Stop.id()) :: {:ok, t()} | :error
@spec serving_stop(Stop.id()) :: {:ok, [t()]} | :error
def serving_stop(
stop_id,
get_json_fn \\ &V3Api.get_json/2,
Expand Down
6 changes: 5 additions & 1 deletion lib/screens/v2/candidate_generator/dup/departures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,11 @@ defmodule Screens.V2.CandidateGenerator.Dup.Departures do
) do
routes =
stop_ids
|> Enum.flat_map(&fetch_routes_serving_stop_fn.(&1))
|> Enum.map(&fetch_routes_serving_stop_fn.(&1))
|> Enum.flat_map(fn
{:ok, routes} -> routes
:error -> []
end)
|> Enum.uniq()

if route_ids == [] do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.ElevatorClosures do
|> MapSet.new()
|> MapSet.put(home_parent_station_id)
|> Enum.map(fn station_id ->
{station_id, station_id |> Route.serving_stop() |> routes_to_icons()}
{station_id, station_id |> routes_serving_stop() |> routes_to_icons()}
end)
|> Enum.into(%{})
end
Expand All @@ -66,6 +66,13 @@ defmodule Screens.V2.CandidateGenerator.Widgets.ElevatorClosures do
end)
end

defp routes_serving_stop(stop_id) do
case Route.serving_stop(stop_id) do
{:ok, routes} -> routes
:error -> []
end
end
Comment on lines +69 to +74
Copy link
Contributor

@sloanelybutsurely sloanelybutsurely Aug 1, 2024

Choose a reason for hiding this comment

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

off-topic: maybe someday we'll have something like fromMaybe from Data.Maybe... :: sigh ::

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can I interest you in some Gleam? 😺


defp routes_to_icons(routes) do
routes
|> Enum.map(fn
Expand Down
12 changes: 6 additions & 6 deletions test/screens/v2/candidate_generator/dup/departures_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,12 @@ defmodule Screens.V2.CandidateGenerator.Dup.DeparturesTest do
fetch_vehicles_fn = fn _, _ -> [struct(Vehicle)] end

fetch_routes_serving_stop_fn = fn
"Boat" -> [%{id: "Ferry", type: :ferry}]
"place-A" -> [%{id: "Orange", type: :subway}, %{id: "Green", type: :light_rail}]
"bus-A" -> [%{id: "Bus A", type: :bus}]
"bus-B" -> [%{id: "Bus B", type: :bus}]
"place-overnight" -> [%{id: "Red", type: :subway}]
_ -> [%{id: "test", type: :test}]
"Boat" -> {:ok, [%{id: "Ferry", type: :ferry}]}
"place-A" -> {:ok, [%{id: "Orange", type: :subway}, %{id: "Green", type: :light_rail}]}
"bus-A" -> {:ok, [%{id: "Bus A", type: :bus}]}
"bus-B" -> {:ok, [%{id: "Bus B", type: :bus}]}
"place-overnight" -> {:ok, [%{id: "Red", type: :subway}]}
_ -> {:ok, [%{id: "test", type: :test}]}
end

%{
Expand Down
Loading