From f49fed7c77971ec99eee07f9c0f4a50003e3a327 Mon Sep 17 00:00:00 2001 From: Brian Fauble Date: Wed, 24 Jan 2024 08:02:44 -0700 Subject: [PATCH] pass along orientation field in enhanced feed --- .../parser/gtfs_realtime_enhanced.ex | 3 +- .../vehicle_position/carriage_details.ex | 54 ++++++++++++++----- .../vehicle_position/occupancy_status.ex | 2 + .../parser/gtfs_realtime_enhanced_test.exs | 12 +++-- test/concentrate/vehicle_position_test.exs | 12 +++-- 5 files changed, 61 insertions(+), 22 deletions(-) diff --git a/lib/concentrate/parser/gtfs_realtime_enhanced.ex b/lib/concentrate/parser/gtfs_realtime_enhanced.ex index 4139a46a..338ec90d 100644 --- a/lib/concentrate/parser/gtfs_realtime_enhanced.ex +++ b/lib/concentrate/parser/gtfs_realtime_enhanced.ex @@ -216,7 +216,8 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhanced do occupancy_percentage: Map.get(vp, "occupancy_percentage"), multi_carriage_details: VehiclePosition.CarriageDetails.build_multi_carriage_details( - Helpers.parse_multi_carriage_details(vp) + Helpers.parse_multi_carriage_details(vp), + :enhanced ) ) ] diff --git a/lib/concentrate/vehicle_position/carriage_details.ex b/lib/concentrate/vehicle_position/carriage_details.ex index a7e2bdc7..f0502f0b 100644 --- a/lib/concentrate/vehicle_position/carriage_details.ex +++ b/lib/concentrate/vehicle_position/carriage_details.ex @@ -15,37 +15,65 @@ defmodule Concentrate.VehiclePosition.CarriageDetails do :occupancy_percentage, :carriage_sequence, :label, - :id + :id, + :orientation ]) - def build_multi_carriage_details(nil) do + def build_multi_carriage_details(multi_carriage_details, feed_type \\ :normal) + + def build_multi_carriage_details(nil, _) do nil end # Ensures that the nil / empty values are appropriate as per PB spec: - def build_multi_carriage_details(multi_carriage_details) do + def build_multi_carriage_details(multi_carriage_details, feed_type) do Enum.map(multi_carriage_details, fn carriage_details -> carriage_details - |> get_atomized_carriage_details() + |> get_atomized_carriage_details(feed_type) |> drop_nil_values() end) end # Convert to atomized keys, so that both atoms and string keys are supported: - defp get_atomized_carriage_details(carriage_details) do + defp get_atomized_carriage_details(carriage_details, :enhanced) do atomized_carriage_details = - for {key, val} <- carriage_details, - into: %{}, - do: - {if(is_atom(key), do: key, else: String.to_existing_atom(key)), - if(key in ["occupancy_status", :occupancy_status] and not is_atom(val), - do: OccupancyStatus.parse_to_atom(val), - else: val - )} + for pair <- carriage_details, into: %{}, do: atomize_key_value_pair(pair) + + Map.take( + atomized_carriage_details, + ~w(id label carriage_sequence occupancy_status occupancy_percentage orientation)a + ) + end + + defp get_atomized_carriage_details(carriage_details, _) do + atomized_carriage_details = + for pair <- carriage_details, into: %{}, do: atomize_key_value_pair(pair) Map.take( atomized_carriage_details, ~w(id label carriage_sequence occupancy_status occupancy_percentage)a ) end + + defp atomize_key_value_pair({key, value}) when key in ["occupancy_status", :occupancy_status] do + {atomize_key(key), OccupancyStatus.parse_to_atom(value)} + end + + defp atomize_key_value_pair({key, value}) when key in ["orientation", :orientation] do + {atomize_key(key), parse_orientation(value)} + end + + defp atomize_key_value_pair({key, value}) do + {atomize_key(key), value} + end + + defp atomize_key(key) when not is_atom(key) do + String.to_existing_atom(key) + end + + defp atomize_key(key), do: key + + defp parse_orientation("AB"), do: :AB + defp parse_orientation("BA"), do: :BA + defp parse_orientation(val), do: val end diff --git a/lib/concentrate/vehicle_position/occupancy_status.ex b/lib/concentrate/vehicle_position/occupancy_status.ex index bc407007..023ef1f3 100644 --- a/lib/concentrate/vehicle_position/occupancy_status.ex +++ b/lib/concentrate/vehicle_position/occupancy_status.ex @@ -22,6 +22,8 @@ defmodule Concentrate.VehiclePosition.OccupancyStatus do ) end + def parse_to_atom(occupancy_status) when is_atom(occupancy_status), do: occupancy_status + def parse_to_atom(occupancy_status) do atom_occ_status = String.to_existing_atom(occupancy_status) if valid_occupancy_status?(atom_occ_status), do: atom_occ_status, else: :NO_DATA_AVAILABLE diff --git a/test/concentrate/parser/gtfs_realtime_enhanced_test.exs b/test/concentrate/parser/gtfs_realtime_enhanced_test.exs index c4b985af..ef213647 100644 --- a/test/concentrate/parser/gtfs_realtime_enhanced_test.exs +++ b/test/concentrate/parser/gtfs_realtime_enhanced_test.exs @@ -405,14 +405,16 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhancedTest do label: "main-car", occupancy_status: :MANY_SEATS_FULL, occupancy_percentage: 80, - carriage_sequence: 1 + carriage_sequence: 1, + orientation: :AB }, %{ id: 1, label: "second-car", occupancy_status: :EMPTY, occupancy_percentage: 0, - carriage_sequence: 2 + carriage_sequence: 2, + orientation: :BA } ], "position" => %{ @@ -472,14 +474,16 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhancedTest do label: "main-car", occupancy_status: :MANY_SEATS_FULL, occupancy_percentage: 80, - carriage_sequence: 1 + carriage_sequence: 1, + orientation: :AB }, %{ id: 1, label: "second-car", occupancy_status: :EMPTY, occupancy_percentage: 0, - carriage_sequence: 2 + carriage_sequence: 2, + orientation: :BA } ] ) diff --git a/test/concentrate/vehicle_position_test.exs b/test/concentrate/vehicle_position_test.exs index 13f1d007..41929b6a 100644 --- a/test/concentrate/vehicle_position_test.exs +++ b/test/concentrate/vehicle_position_test.exs @@ -74,14 +74,16 @@ defmodule Concentrate.VehiclePositionTest do label: "main-car", occupancy_status: :MANY_SEATS_FULL, occupancy_percentage: 80, - carriage_sequence: 1 + carriage_sequence: 1, + orientation: :AB }, %{ id: 0, label: "second-car", occupancy_status: :EMPTY, occupancy_percentage: 0, - carriage_sequence: 2 + carriage_sequence: 2, + orientation: :BA } ] ) @@ -99,14 +101,16 @@ defmodule Concentrate.VehiclePositionTest do label: "main-car", occupancy_status: :MANY_SEATS_FULL, occupancy_percentage: 80, - carriage_sequence: 1 + carriage_sequence: 1, + orientation: :AB }, %{ id: 0, label: "second-car", occupancy_status: :EMPTY, occupancy_percentage: 0, - carriage_sequence: 2 + carriage_sequence: 2, + orientation: :BA } ] )