diff --git a/apps/parse/lib/parse/alerts.ex b/apps/parse/lib/parse/alerts.ex index ed8b0dc1b..dcbacac12 100644 --- a/apps/parse/lib/parse/alerts.ex +++ b/apps/parse/lib/parse/alerts.ex @@ -136,8 +136,11 @@ defmodule Parse.Alerts do copy(url) end - defp do_localized_image(%{"localized_image" => [%{"language" => "en", "url" => url} | _]}, _) do - copy(url) + defp do_localized_image(%{"localized_image" => [_ | _] = translations}, %{default: default}) do + case Enum.find(translations, &(&1["language"] == "en")) do + %{"language" => "en", "url" => url} -> url + _ -> default + end end defp active_period(%{"start" => start, "end" => stop}) do diff --git a/apps/parse/test/parse/alerts_test.exs b/apps/parse/test/parse/alerts_test.exs index d96870c03..3825732ac 100644 --- a/apps/parse/test/parse/alerts_test.exs +++ b/apps/parse/test/parse/alerts_test.exs @@ -436,7 +436,7 @@ defmodule Parse.AlertsTest do assert [%Alert{description: "Good morning"}] = parse_json(map) end - test "returns english image url if available" do + test "returns english image url over unspecified language" do url = "https://dev-mbta.pantheonsite.io/sites/default/files/styles/max_2600x2600/public/media/2023-09/QuincyCenter-Braintree-EA.png" @@ -456,15 +456,60 @@ defmodule Parse.AlertsTest do "id" => "113791", "image" => %{ "localized_image" => [ + %{ + "url" => "some_other_url", + "media_type" => "image/png", + "language" => "fr" + }, %{ "url" => url, "media_type" => "image/png", "language" => "en" - }, + } + ] + }, + "informed_entity" => [%{}], + "last_modified_timestamp" => 1_494_947_991, + "last_push_notification_timestamp" => 1_494_947_991, + "service_effect_text" => [], + "severity" => 3, + "short_header_text" => [], + "timeframe_text" => [] + } + ] + } + + assert [%Alert{image: ^url}] = parse_json(map) + end + + test "returns english image url over other language" do + url = + "https://dev-mbta.pantheonsite.io/sites/default/files/styles/max_2600x2600/public/media/2023-09/QuincyCenter-Braintree-EA.png" + + map = %{ + "timestamp" => "1496832813", + "alerts" => [ + %{ + "active_period" => [], + "alert_lifecycle" => "NEW", + "cause" => "UNKNOWN_CAUSE", + "created_timestamp" => 1_494_947_991, + "description_text" => [], + "duration_certainty" => "KNOWN", + "effect" => "NO_SERVICE", + "effect_detail" => "STATION_CLOSURE", + "header_text" => [], + "id" => "113791", + "image" => %{ + "localized_image" => [ %{ "url" => "some_other_url", + "media_type" => "image/png" + }, + %{ + "url" => url, "media_type" => "image/png", - "language" => "fr" + "language" => "en" } ] },