Skip to content

Commit

Permalink
feat: parse and encode the revenue field
Browse files Browse the repository at this point in the history
  • Loading branch information
Whoops committed Dec 4, 2023
1 parent 823df4a commit 28a1788
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/concentrate/encoder/trip_updates_enhanced.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ defmodule Concentrate.Encoder.TripUpdatesEnhanced do
end

defp enhanced_data(update) do
%{route_pattern_id: TripDescriptor.route_pattern_id(update)}
%{
route_pattern_id: TripDescriptor.route_pattern_id(update),
revenue: TripDescriptor.revenue(update)
}
end

defp build_stop_time_update(update) do
Expand Down
5 changes: 4 additions & 1 deletion lib/concentrate/encoder/vehicle_positions_enhanced.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ defmodule Concentrate.Encoder.VehiclePositionsEnhanced do
end

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

for vp <- vps do
%{
Expand Down
1 change: 1 addition & 0 deletions lib/concentrate/parser/gtfs_realtime_enhanced.ex
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhanced do
start_time: Map.get(trip, "start_time"),
schedule_relationship: schedule_relationship(Map.get(trip, "schedule_relationship")),
timestamp: Map.get(descriptor, "timestamp"),
revenue: Map.get(trip, "revenue", true),
vehicle_id: vehicle_id
)
]
Expand Down
1 change: 1 addition & 0 deletions lib/concentrate/trip_descriptor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Concentrate.TripDescriptor do
:start_time,
:vehicle_id,
:timestamp,
revenue: true,
schedule_relationship: :SCHEDULED
])

Expand Down
40 changes: 40 additions & 0 deletions test/concentrate/encoder/trip_updates_enhanced_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,45 @@ defmodule Concentrate.Encoder.TripUpdatesEnhancedTest do
]
} = encoded
end

test "trips encode revenue status" do
parsed = [
TripDescriptor.new(trip_id: "trip", revenue: true),
StopTimeUpdate.new(trip_id: "trip", stop_id: "stop")
]

encoded = Jason.decode!(encode_groups(group(parsed)))

assert %{
"entity" => [
%{
"trip_update" => %{
"trip" => %{
"revenue" => true
}
}
}
]
} = encoded

parsed = [
TripDescriptor.new(trip_id: "trip", revenue: false),
StopTimeUpdate.new(trip_id: "trip", stop_id: "stop")
]

encoded = Jason.decode!(encode_groups(group(parsed)))

assert %{
"entity" => [
%{
"trip_update" => %{
"trip" => %{
"revenue" => false
}
}
}
]
} = encoded
end
end
end
15 changes: 15 additions & 0 deletions test/concentrate/encoder/vehicle_positions_enhanced_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ defmodule Concentrate.Encoder.VehiclePositionsEnhancedTest do

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

test "includes revenue status" do
data = [
TripDescriptor.new(trip_id: "one", vehicle_id: "y1", revenue: false),
VehiclePosition.new(
trip_id: "one",
id: "y1",
latitude: 1,
longitude: 1,
status: :IN_TRANSIT_TO
)
]

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

defp round_trip(data, opts \\ []) do
Expand Down
71 changes: 71 additions & 0 deletions test/concentrate/parser/gtfs_realtime_enhanced_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,45 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhancedTest do
[td] = decode_trip_update(map, Helpers.parse_options([]))
assert TripDescriptor.vehicle_id(td) == "vehicle_id"
end

test "decodes revenue status" do
non_revenue_map = %{
"trip" => %{
"trip_id" => "trip",
"route_id" => "route",
"revenue" => false
},
"stop_time_update" => []
}

revenue_map = %{
"trip" => %{
"trip_id" => "trip",
"route_id" => "route",
"revenue" => true
},
"stop_time_update" => []
}

[td] = decode_trip_update(non_revenue_map, Helpers.parse_options([]))
assert TripDescriptor.revenue(td) == false

[td] = decode_trip_update(revenue_map, Helpers.parse_options([]))
assert TripDescriptor.revenue(td) == true
end

test "revenue defaults to true if not specified" do
map = %{
"trip" => %{
"trip_id" => "trip",
"route_id" => "route"
},
"stop_time_update" => []
}

[td] = decode_trip_update(map, Helpers.parse_options([]))
assert TripDescriptor.revenue(td) == true
end
end

describe "decode_vehicle/3" do
Expand Down Expand Up @@ -536,5 +575,37 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhancedTest do

assert log =~ "vehicle timestamp after feed timestamp"
end

test "decodes revenue status" do
non_revenue_map = %{
"trip" => %{
"trip_id" => "trip",
"route_id" => "route",
"revenue" => false
},
"position" => %{
"latitude" => 1.0,
"longitude" => 1.0
}
}

revenue_map = %{
"trip" => %{
"trip_id" => "trip",
"route_id" => "route",
"revenue" => true
},
"position" => %{
"latitude" => 1.0,
"longitude" => 1.0
}
}

[td, _vp] = decode_vehicle(non_revenue_map, Helpers.parse_options([]), nil)
assert TripDescriptor.revenue(td) == false

[td, _vp] = decode_vehicle(revenue_map, Helpers.parse_options([]), nil)
assert TripDescriptor.revenue(td) == true
end
end
end

0 comments on commit 28a1788

Please sign in to comment.