Skip to content

Commit

Permalink
Handle itineraries with steps w/o absolute dir
Browse files Browse the repository at this point in the history
Steps in an OpenTripPlanner itinerary have an `absoluteDirection` field.
It's valid for that field to be missing, but we don't handle that case
correctly, leading to occasional failures in trip planning. Corrected.
  • Loading branch information
phildarnowsky committed Nov 19, 2019
1 parent 3b5be55 commit 383405b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions apps/trip_plan/lib/trip_plan/api/open_trip_planner/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ defmodule TripPlan.Api.OpenTripPlanner.Parser do
defp parse_absolute_direction(unquote(String.upcase(Atom.to_string(dir)))), do: unquote(dir)
end

defp parse_absolute_direction(nil), do: nil

defp id_after_colon(agency_colon_id) do
[_agency, id] = String.split(agency_colon_id, ":", parts: 2)
id
Expand Down
2 changes: 1 addition & 1 deletion apps/trip_plan/lib/trip_plan/personal_detail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule TripPlan.PersonalDetail.Step do
@type t :: %__MODULE__{
distance: float,
relative_direction: relative_direction,
absolute_direction: absolute_direction
absolute_direction: absolute_direction | nil
}
@type relative_direction ::
:depart
Expand Down
9 changes: 8 additions & 1 deletion apps/trip_plan/test/api/open_trip_planner/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ defmodule TripPlan.Api.OpenTripPlanner.ParserTest do
assert first.stop == Timex.to_datetime(~N[2017-05-19T14:03:19], "America/New_York")
end

test "returns an error if JSON is invalid" do
test "allows null absoluteDirection" do
pattern = "absoluteDirection\": \"SOUTH\""
replacement = "absoluteDirection\": null"
json = String.replace(@fixture, pattern, replacement)
assert {:ok, _itinerary} = parse_json(json)
end

test "returns an error if JSON is invalid" do
pattern = "absoluteDirection\": \"SOUTH\""
replacement = "absoluteDirection\": \"UP\""
json = String.replace(@fixture, pattern, replacement)
assert {:error, :invalid_json} = parse_json(json)
end

Expand Down

0 comments on commit 383405b

Please sign in to comment.