Skip to content

Commit

Permalink
feat: allow integer vehicle IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
bklebe committed Sep 15, 2023
1 parent 303a150 commit a95dbc6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/concentrate/parser/gtfs_realtime_enhanced.ex
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,18 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhanced do
defp decode_trip_descriptor(%{"trip" => trip} = descriptor) do
vehicle_id =
case descriptor do
%{"vehicle" => %{"id" => vehicle_id}} -> vehicle_id
_ -> nil
%{"vehicle" => %{"id" => vehicle_id}} when is_binary(vehicle_id) ->
vehicle_id

%{"vehicle" => %{"id" => vehicle_id}} when is_integer(vehicle_id) ->
Logger.warning(fn ->
"#{__MODULE__}: treating integer vehicle ID as string: #{inspect(vehicle_id)}"
end)

Integer.to_string(vehicle_id)

_ ->
nil
end

[
Expand Down
17 changes: 17 additions & 0 deletions test/concentrate/parser/gtfs_realtime_enhanced_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,23 @@ defmodule Concentrate.Parser.GTFSRealtimeEnhancedTest do
end
end

test "parses vehicle_id into string and logs warning if int" do
map = %{
"trip" => %{
"trip_id" => "trip",
"route_id" => "route"
},
"vehicle" => %{
"id" => 1234
},
"stop_time_update" => []
}

{[td], log} = with_log(fn -> decode_trip_update(map, Helpers.parse_options([])) end)
assert TripDescriptor.vehicle_id(td) == "1234"
assert log =~ "treating integer vehicle ID as string: 1234"
end

describe "decode_vehicle/3" do
test "returns nothing if there's an empty map" do
assert decode_vehicle(%{}, Helpers.parse_options([]), nil) == []
Expand Down

0 comments on commit a95dbc6

Please sign in to comment.