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

🧠 Surface orientation field through Concentrate #339

Merged
merged 2 commits into from
Feb 6, 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
3 changes: 2 additions & 1 deletion lib/concentrate/parser/gtfs_realtime_enhanced.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
]
Expand Down
54 changes: 41 additions & 13 deletions lib/concentrate/vehicle_position/carriage_details.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/concentrate/vehicle_position/occupancy_status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions test/concentrate/parser/gtfs_realtime_enhanced_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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" => %{
Expand Down Expand Up @@ -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
}
]
)
Expand Down
12 changes: 8 additions & 4 deletions test/concentrate/vehicle_position_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
)
Expand All @@ -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
}
]
)
Expand Down
Loading