Skip to content

Commit

Permalink
Replace with with case (#2079)
Browse files Browse the repository at this point in the history
* replace with with case

* fix new credo issue
  • Loading branch information
anthonyshull authored May 29, 2024
1 parent 0666e37 commit ffdd687
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 60 deletions.
13 changes: 6 additions & 7 deletions lib/alerts/cache/bus_stop_change_s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ defmodule Alerts.Cache.BusStopChangeS3 do
"""
@spec copy_alerts_to_s3([Alert.t()], any) :: :ok
def copy_alerts_to_s3(bus_alerts, _) do
with stop_change_alerts when stop_change_alerts != [] <-
Enum.filter(bus_alerts, &(&1.effect in [:stop_closure, :stop_moved])) do
stop_change_alerts
|> Enum.sort(&(&1.id >= &2.id))
|> maybe_write_alerts_to_s3()
else
case Enum.filter(bus_alerts, &(&1.effect in [:stop_closure, :stop_moved])) do
[] ->
# No stop change alerts to write.
:ok

stop_change_alerts ->
stop_change_alerts
|> Enum.sort(&(&1.id >= &2.id))
|> maybe_write_alerts_to_s3()
end
end

Expand Down
39 changes: 19 additions & 20 deletions lib/dotcom_web/controllers/bus_stop_change_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,22 @@ defmodule DotcomWeb.BusStopChangeController do
defp get_old_alerts() do
folder = Application.app_dir(:dotcom) |> Path.join("priv/bus-stop-change")

with {:ok, files} <- File.ls(folder) do
Enum.reduce(files, [], fn filepath, acc ->
alert_info =
Path.join(folder, filepath)
|> File.stream!()
|> CSV.decode!(headers: true, escape_max_lines: 1000)
|> Enum.to_list()

[alert_info | acc]
end)
|> Enum.flat_map(& &1)
|> remove_old_versions()
|> Enum.reject(&excluded_alerts/1)
|> Enum.map(&parse_alert/1)
else
case File.ls(folder) do
{:ok, files} ->
Enum.reduce(files, [], fn filepath, acc ->
alert_info =
Path.join(folder, filepath)
|> File.stream!()
|> CSV.decode!(headers: true, escape_max_lines: 1000)
|> Enum.to_list()

[alert_info | acc]
end)
|> Enum.flat_map(& &1)
|> remove_old_versions()
|> Enum.reject(&excluded_alerts/1)
|> Enum.map(&parse_alert/1)

_ ->
[]
end
Expand Down Expand Up @@ -136,11 +137,9 @@ defmodule DotcomWeb.BusStopChangeController do
end

defp do_affected_info(texts, arg) when arg in ["route", "stop"] do
with found when not is_nil(found) <-
Enum.find(texts, &String.contains?(&1, "Affected #{arg}")) do
String.split(found, ~r/\n\r\n/)
else
_ -> ["Affected #{arg}s:"]
case Enum.find(texts, &String.contains?(&1, "Affected #{arg}")) do
nil -> ["Affected #{arg}s:"]
found -> String.split(found, ~r/\n\r\n/)
end
end

Expand Down
35 changes: 20 additions & 15 deletions lib/dotcom_web/controllers/places_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,31 @@ defmodule DotcomWeb.PlacesController do
/places/search/Prudential/6 returns 6 results, each with an object containing URLs specific to that location for Transit Near Me, the Retail Sales Location page, and the Proposed Sales Location pages.
"""
def search(conn, %{"query" => query, "hit_limit" => hit_limit_str}) do
with {hit_limit, ""} <- Integer.parse(hit_limit_str) do
case @location_service.autocomplete(query, hit_limit) do
{:ok, suggestions} ->
json(conn, %{result: with_coordinates(suggestions)})

{:error, error} ->
case error do
:zero_results ->
json(conn, %{result: []})

_ ->
ControllerHelpers.return_internal_error(conn)
end
end
else
case Integer.parse(hit_limit_str) do
{hit_limit, ""} when hit_limit > 0 ->
do_search(conn, query, hit_limit)

_ ->
ControllerHelpers.return_invalid_arguments_error(conn)
end
end

defp do_search(conn, query, hit_limit) do
case @location_service.autocomplete(query, hit_limit) do
{:ok, suggestions} ->
json(conn, %{result: with_coordinates(suggestions)})

{:error, error} ->
case error do
:zero_results ->
json(conn, %{result: []})

_ ->
ControllerHelpers.return_internal_error(conn)
end
end
end

@spec with_coordinates([Suggestion.t()]) :: [
%{
required(:latitude) => number,
Expand Down
21 changes: 11 additions & 10 deletions lib/dotcom_web/controllers/schedule/hours_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ defmodule DotcomWeb.ScheduleController.HoursController do
end

defp do_hours_of_operation(conn, route_id, hours_fn) do
with %Route{description: description, id: id} <- Routes.Repo.get(route_id) do
hours_of_operation =
hours_fn.(
id,
conn.assigns.date,
description
)

json(conn, hours_of_operation)
else
case Routes.Repo.get(route_id) do
%Route{description: description, id: id} ->
hours_of_operation =
hours_fn.(
id,
conn.assigns.date,
description
)

json(conn, hours_of_operation)

error ->
ControllerHelpers.return_error(conn, :internal_server_error, inspect(error))
end
Expand Down
17 changes: 9 additions & 8 deletions lib/predictions/predictions_pub_sub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@ defmodule Predictions.PredictionsPubSub do
@spec subscribe(String.t()) :: [Prediction.t()]
@spec subscribe(String.t(), GenServer.server()) :: [Prediction.t()] | {:error, term()}
def subscribe(topic, server \\ __MODULE__) do
with %StreamTopic{} = stream_topic <- StreamTopic.new(topic) do
:ok = StreamTopic.start_streams(stream_topic)
case StreamTopic.new(topic) do
%StreamTopic{} = stream_topic ->
:ok = StreamTopic.start_streams(stream_topic)

{registry_key, predictions} = GenServer.call(server, {:subscribe, stream_topic})
{registry_key, predictions} = GenServer.call(server, {:subscribe, stream_topic})

for key <- StreamTopic.registration_keys(stream_topic) do
Registry.register(@subscribers, registry_key, key)
end
for key <- StreamTopic.registration_keys(stream_topic) do
Registry.register(@subscribers, registry_key, key)
end

predictions

predictions
else
{:error, _} = error ->
error
end
Expand Down

0 comments on commit ffdd687

Please sign in to comment.