Skip to content

Commit

Permalink
add last_trip field to VehiclePositions enhanced feed
Browse files Browse the repository at this point in the history
  • Loading branch information
bfauble committed Mar 27, 2024
1 parent b9108b7 commit 50ff43c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/concentrate/encoder/vehicle_positions_enhanced.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ defmodule Concentrate.Encoder.VehiclePositionsEnhanced do
def encode_groups(groups, opts \\ []) when is_list(groups) do
message = %{
"header" => feed_header(opts),
"entity" => Enum.flat_map(groups, &build_entity/1)
"entity" => Enum.flat_map(groups, fn group -> build_entity(group, &enhanced_data/1) end)
}

Jason.encode!(message)
end

def build_entity({%TripDescriptor{} = td, vps, _stus}) do
defp enhanced_data(update) do
%{
last_trip: TripDescriptor.last_trip(update)
}
end

def build_entity(_, enhanced_data \\ fn _ -> %{} end)

def build_entity({%TripDescriptor{} = td, vps, _stus}, enhanced_data_fn) do
trip =
td
|> trip_descriptor()
|> Map.put("revenue", TripDescriptor.revenue(td))
|> Map.merge(enhanced_data_fn.(td))
|> drop_nil_values()

for vp <- vps do
%{
Expand All @@ -32,7 +42,7 @@ defmodule Concentrate.Encoder.VehiclePositionsEnhanced do
end
end

def build_entity({nil, vps, _stus}) do
def build_entity({nil, vps, _stus}, _enhanced_data_fn) do
# vehicles without a trip
for vp <- vps,
trip_id = VehiclePosition.trip_id(vp),
Expand Down
29 changes: 29 additions & 0 deletions test/concentrate/encoder/vehicle_positions_enhanced_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,35 @@ defmodule Concentrate.Encoder.VehiclePositionsEnhancedTest do
}
] == FeedUpdate.updates(round_trip(data))
end

test "includes last_trip field" do
data = [
TripDescriptor.new(trip_id: "one", vehicle_id: "y1"),
VehiclePosition.new(
trip_id: "one",
id: "y1",
latitude: 1,
longitude: 1,
status: :IN_TRANSIT_TO
),
TripDescriptor.new(trip_id: "two", vehicle_id: "y2", last_trip: true),
VehiclePosition.new(
trip_id: "two",
id: "y2",
latitude: 2,
longitude: 2,
status: :IN_TRANSIT_TO,
occupancy_status: :FULL,
occupancy_percentage: 101,
consist: [
VehiclePositionConsist.new(label: "y2-1"),
VehiclePositionConsist.new(label: "y2-2")
]
)
]

assert data == FeedUpdate.updates(round_trip(data))
end
end

defp round_trip(data, opts \\ []) do
Expand Down

0 comments on commit 50ff43c

Please sign in to comment.