-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Convert itinerary legs to segments for easier rendering (#2262
- Loading branch information
1 parent
f244770
commit de2d3dc
Showing
4 changed files
with
101 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule Dotcom.TripPlan.LegToSegmentHelper do | ||
@moduledoc """ | ||
A simple algorithm to convert legs as returned by Open Trip Planner | ||
into segments to be displayed by our trip planner tool. | ||
""" | ||
|
||
alias Dotcom.TripPlan.{PersonalDetail, TransitDetail} | ||
|
||
def legs_to_segments(legs) do | ||
legs | ||
|> raw_convert_to_segments() | ||
|> prepend_start_location() | ||
|> append_end_location() | ||
end | ||
|
||
defp raw_convert_to_segments(legs) do | ||
Enum.map(legs, &to_segment/1) | ||
end | ||
|
||
defp to_segment(%{mode: %PersonalDetail{}} = leg) do | ||
{:walking_segment, leg} | ||
end | ||
|
||
defp to_segment(%{mode: %TransitDetail{}} = leg) do | ||
{:transit_segment, leg} | ||
end | ||
|
||
defp prepend_start_location([{_, leg} | _] = segments) do | ||
[ | ||
{:location_segment, %{time: leg.start, place: leg.from}} | ||
| segments | ||
] | ||
end | ||
|
||
defp append_end_location([{_, leg} = last_segment]) do | ||
[last_segment, {:location_segment, %{time: leg.stop, place: leg.to}}] | ||
end | ||
|
||
defp append_end_location([first_segment | rest_of_segments]) do | ||
[first_segment | append_end_location(rest_of_segments)] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Dotcom.TripPlan.LegToSegmentHelperTest do | ||
@moduledoc false | ||
|
||
use ExUnit.Case, async: true | ||
|
||
alias Dotcom.TripPlan.LegToSegmentHelper | ||
alias Dotcom.TripPlan.{Leg, PersonalDetail, TransitDetail} | ||
|
||
test "works for a typical walking-transit-walking itinerary and puts a location on either end" do | ||
assert [ | ||
{:location_segment, _}, | ||
{:walking_segment, _}, | ||
{:transit_segment, _}, | ||
{:walking_segment, _}, | ||
{:location_segment, _} | ||
] = | ||
LegToSegmentHelper.legs_to_segments([ | ||
%Leg{mode: %PersonalDetail{}}, | ||
%Leg{mode: %TransitDetail{}}, | ||
%Leg{mode: %PersonalDetail{}} | ||
]) | ||
end | ||
end |