From 28a1788f82d59fdebc812a685010c440eacba037 Mon Sep 17 00:00:00 2001 From: Walton Hoops Date: Mon, 27 Nov 2023 14:44:36 -0700 Subject: [PATCH] feat: parse and encode the revenue field --- .../encoder/trip_updates_enhanced.ex | 5 +- .../encoder/vehicle_positions_enhanced.ex | 5 +- .../parser/gtfs_realtime_enhanced.ex | 1 + lib/concentrate/trip_descriptor.ex | 1 + .../encoder/trip_updates_enhanced_test.exs | 40 +++++++++++ .../vehicle_positions_enhanced_test.exs | 15 ++++ .../parser/gtfs_realtime_enhanced_test.exs | 71 +++++++++++++++++++ 7 files changed, 136 insertions(+), 2 deletions(-) diff --git a/lib/concentrate/encoder/trip_updates_enhanced.ex b/lib/concentrate/encoder/trip_updates_enhanced.ex index bc9b3b61..3cfb7db7 100644 --- a/lib/concentrate/encoder/trip_updates_enhanced.ex +++ b/lib/concentrate/encoder/trip_updates_enhanced.ex @@ -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 diff --git a/lib/concentrate/encoder/vehicle_positions_enhanced.ex b/lib/concentrate/encoder/vehicle_positions_enhanced.ex index 725bb421..b00173fe 100644 --- a/lib/concentrate/encoder/vehicle_positions_enhanced.ex +++ b/lib/concentrate/encoder/vehicle_positions_enhanced.ex @@ -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 %{ diff --git a/lib/concentrate/parser/gtfs_realtime_enhanced.ex b/lib/concentrate/parser/gtfs_realtime_enhanced.ex index fc46c12f..4139a46a 100644 --- a/lib/concentrate/parser/gtfs_realtime_enhanced.ex +++ b/lib/concentrate/parser/gtfs_realtime_enhanced.ex @@ -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 ) ] diff --git a/lib/concentrate/trip_descriptor.ex b/lib/concentrate/trip_descriptor.ex index 96dd8d6a..57b75382 100644 --- a/lib/concentrate/trip_descriptor.ex +++ b/lib/concentrate/trip_descriptor.ex @@ -13,6 +13,7 @@ defmodule Concentrate.TripDescriptor do :start_time, :vehicle_id, :timestamp, + revenue: true, schedule_relationship: :SCHEDULED ]) diff --git a/test/concentrate/encoder/trip_updates_enhanced_test.exs b/test/concentrate/encoder/trip_updates_enhanced_test.exs index c0ed728f..1e73278e 100644 --- a/test/concentrate/encoder/trip_updates_enhanced_test.exs +++ b/test/concentrate/encoder/trip_updates_enhanced_test.exs @@ -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 diff --git a/test/concentrate/encoder/vehicle_positions_enhanced_test.exs b/test/concentrate/encoder/vehicle_positions_enhanced_test.exs index a621871b..aa6307b9 100644 --- a/test/concentrate/encoder/vehicle_positions_enhanced_test.exs +++ b/test/concentrate/encoder/vehicle_positions_enhanced_test.exs @@ -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 diff --git a/test/concentrate/parser/gtfs_realtime_enhanced_test.exs b/test/concentrate/parser/gtfs_realtime_enhanced_test.exs index 472829f7..04489ef0 100644 --- a/test/concentrate/parser/gtfs_realtime_enhanced_test.exs +++ b/test/concentrate/parser/gtfs_realtime_enhanced_test.exs @@ -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 @@ -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