Skip to content

Commit

Permalink
GBFSMetadata : ajuste first_feed pour GBFS v3.0 (#4210)
Browse files Browse the repository at this point in the history
* GBFSMetadata : ajuste first_feed pour GBFS v3.0

* Update tests
  • Loading branch information
AntoineAugusti authored Sep 23, 2024
1 parent fcff6c2 commit b356e44
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
11 changes: 9 additions & 2 deletions apps/shared/lib/gbfs_metadata.ex
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,15 @@ defmodule Transport.Shared.GBFSMetadata do
end
end

def first_feed(%{"data" => data} = payload) do
(data["en"] || data["fr"] || data[payload |> languages() |> Enum.at(0)])["feeds"]
def first_feed(%{"data" => data, "version" => version} = payload) do
# From GBFS 1.1 until GBFS 2.3
if String.starts_with?(version, ["1.", "2."]) do
first_language = payload |> languages() |> Enum.at(0)
(data["en"] || data["fr"] || data[first_language])["feeds"]
# From GBFS 3.0 onwards
else
data["feeds"]
end
end

defp languages(%{"data" => data}) do
Expand Down
27 changes: 27 additions & 0 deletions apps/shared/test/gbfs_metadata_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,33 @@ defmodule Transport.Shared.GBFSMetadataTest do
end
end

describe "feeds" do
test "3.0 feed" do
json =
Jason.decode!("""
{
"last_updated": "2023-07-17T13:34:13+02:00",
"ttl": 0,
"version": "3.0",
"data": {
"feeds": [
{
"name": "system_information",
"url": "https://www.example.com/gbfs/1/system_information"
},
{
"name": "station_information",
"url": "https://www.example.com/gbfs/1/station_information"
}
]
}
}
""")

assert ["system_information", "station_information"] == feeds(json)
end
end

defp setup_validation_result(summary \\ nil) do
Shared.Validation.GBFSValidator.Mock
|> expect(:validate, fn url ->
Expand Down
3 changes: 2 additions & 1 deletion apps/transport/test/transport/gbfs_to_geojson_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ defmodule Transport.GbfsToGeojsonTest do
"fr" => %{
"feeds" => feeds
}
}
},
"version" => "2.3"
}
|> Jason.encode!()
end
Expand Down

0 comments on commit b356e44

Please sign in to comment.