Skip to content

Commit

Permalink
feat(Routes.Route): add external_agency_name (#2111)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecristen authored Jul 2, 2024
1 parent 157bc5d commit e6aeb97
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 104 deletions.
1 change: 1 addition & 0 deletions assets/ts/__v3api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export interface Route {
description: string;
direction_destinations: DirectionInfo;
direction_names: DirectionInfo;
external_agency_name?: string | null;
fare_class?: FareClassType;
id: string;
long_name: string;
Expand Down
22 changes: 2 additions & 20 deletions lib/dotcom_web/controllers/trip_plan_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,8 @@ defmodule DotcomWeb.TripPlanController do
type: type
} = Enum.find(legs, &(Leg.route_id(&1) == {:ok, id}))

custom_route = %Route{
%Route{
external_agency_name: if(type == "2", do: "Massport"),
description: description,
id: mode.route_id,
long_name: long_name,
Expand All @@ -498,25 +499,6 @@ defmodule DotcomWeb.TripPlanController do
custom_route?: true,
color: "000000"
}

case {type, description} do
# Workaround for Massport buses, manually assign type
{"2", "BUS"} ->
%Route{
custom_route
| type: "Massport-" <> type
}

# Handle MBTA busses not present via api
{"1", "BUS"} ->
%Route{
custom_route
| type: 3
}

_ ->
custom_route
end
end

defp meta_description(conn, _) do
Expand Down
135 changes: 67 additions & 68 deletions lib/routes/route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,60 @@ defmodule Routes.Route do

@derive Jason.Encoder

defstruct id: "",
type: 0,
name: "",
long_name: "",
color: "",
sort_order: 99_999,
direction_names: %{0 => "Outbound", 1 => "Inbound"},
direction_destinations: :unknown,
defstruct color: "",
custom_route?: false,
description: :unknown,
direction_destinations: :unknown,
direction_names: %{0 => "Outbound", 1 => "Inbound"},
external_agency_name: nil,
fare_class: :unknown_fare,
custom_route?: false,
line_id: ""
id: "",
line_id: "",
long_name: "",
name: "",
sort_order: 99_999,
type: 0

@type id_t :: String.t()

@typedoc """
The Route type is mostly based on the GTFS routes.txt fields.
## Fields
* `:color` - A hex code representing the color to be shown on wayfinding,
corresponding to the GTFS routes.txt `route_color` field.
* `:custom_route?` - `true` if this data comes from outside the MBTA GTFS.
* `:description` - corresponds to the GTFS routes.txt `route_desc` field
* `:direction_destinations` - map describing the terminus for each direction,
as might be described on a vehicle headsign
* `:direction_names` - map describing the name of each direction, e.g.
"Inbound" or "Outbound"
* `:external_agency_name` - `nil` if this route is part of the MBTA service,
otherwise a string representing the name of the associated transit agency,
e.g. "Massport"
* `:fare_class` - corresponds to the GTFS routes.txt `route_fare_class` field
* `:id` - corresponds to the GTFS routes.txt `route_id` field
* `:line_id` - corresponds to the GTFS routes.txt `line_id` field
* `:long_name` - corresponds to the GTFS routes.txt `route_long_name` field
* `:name` - Usually corresponds to the GTFS routes.txt `route_short_name`
field, falling back on `route_long_name` if no short name is present.
* `:sort_order` - corresponds to the GTFS routes.txt `route_sort_order` field
* `:type` - corresponds to the GTFS routes.txt `route_type` field
"""
@type t :: %__MODULE__{
id: id_t,
type: type_int(),
name: String.t(),
long_name: String.t(),
color: String.t(),
sort_order: non_neg_integer,
direction_names: %{0 => String.t() | nil, 1 => String.t() | nil},
direction_destinations: %{0 => String.t(), 1 => String.t()} | :unknown,
custom_route?: boolean,
description: gtfs_route_desc,
direction_destinations: %{0 => String.t(), 1 => String.t()} | :unknown,
direction_names: %{0 => String.t() | nil, 1 => String.t() | nil},
external_agency_name: String.t() | nil,
fare_class: gtfs_fare_class,
custom_route?: boolean,
line_id: String.t() | nil
id: id_t,
line_id: String.t() | nil,
long_name: String.t(),
name: String.t(),
sort_order: non_neg_integer,
type: type_int()
}
@type gtfs_route_type ::
:subway | :commuter_rail | :bus | :ferry | :logan_express | :massport_shuttle
Expand Down Expand Up @@ -65,6 +92,8 @@ defmodule Routes.Route do
@silver_line_set MapSet.new(@silver_line)

@spec type_atom(t | type_int | String.t()) :: route_type
def type_atom(%__MODULE__{external_agency_name: "Massport"}), do: :massport_shuttle
def type_atom(%__MODULE__{external_agency_name: "Logan Express"}), do: :logan_express
def type_atom(%__MODULE__{type: type}), do: type_atom(type)
def type_atom(0), do: :subway
def type_atom(1), do: :subway
Expand Down Expand Up @@ -101,6 +130,8 @@ defmodule Routes.Route do
def types_for_mode(:silver_line), do: [3]

@spec icon_atom(t) :: gtfs_route_type | subway_lines_type
def icon_atom(%__MODULE__{external_agency_name: "Massport"}), do: :massport_shuttle
def icon_atom(%__MODULE__{external_agency_name: "Logan Express"}), do: :logan_express
def icon_atom(%__MODULE__{id: "Red"}), do: :red_line
def icon_atom(%__MODULE__{id: "Mattapan"}), do: :mattapan_line
def icon_atom(%__MODULE__{id: "Orange"}), do: :orange_line
Expand Down Expand Up @@ -252,20 +283,12 @@ defmodule Routes.Route do
end

@spec to_json_safe(t) :: map
def to_json_safe(%__MODULE__{
id: id,
type: type,
name: name,
long_name: long_name,
color: color,
sort_order: sort_order,
direction_names: direction_names,
direction_destinations: direction_destinations,
description: description,
fare_class: fare_class,
custom_route?: custom_route?,
line_id: line_id
}) do
def to_json_safe(
%__MODULE__{
direction_names: direction_names,
direction_destinations: direction_destinations
} = route
) do
direction_destinations_value =
if direction_destinations == :unknown,
do: nil,
Expand All @@ -275,21 +298,12 @@ defmodule Routes.Route do
}

%{
id: id,
type: type,
name: name,
long_name: long_name,
color: color,
sort_order: sort_order,
direction_names: %{
"0" => direction_names[0],
"1" => direction_names[1]
},
direction_destinations: direction_destinations_value,
description: description,
fare_class: fare_class,
custom_route?: custom_route?,
line_id: line_id
Map.from_struct(route)
| direction_names: %{
"0" => direction_names[0],
"1" => direction_names[1]
},
direction_destinations: direction_destinations_value
}
end
end
Expand All @@ -303,17 +317,9 @@ end
defimpl Poison.Encoder, for: Routes.Route do
def encode(
%Routes.Route{
id: id,
type: type,
name: name,
long_name: long_name,
color: color,
sort_order: sort_order,
direction_names: direction_names,
direction_destinations: direction_destinations,
description: description,
custom_route?: custom_route?
},
direction_destinations: direction_destinations
} = route,
options
) do
direction_destinations_value =
Expand All @@ -323,16 +329,9 @@ defimpl Poison.Encoder, for: Routes.Route do

Poison.Encoder.encode(
%{
id: id,
type: type,
name: name,
long_name: long_name,
color: color,
sort_order: sort_order,
direction_names: encoded_directions(direction_names),
direction_destinations: direction_destinations_value,
description: description,
custom_route?: custom_route?
Map.from_struct(route)
| direction_names: encoded_directions(direction_names),
direction_destinations: direction_destinations_value
},
options
)
Expand Down
1 change: 1 addition & 0 deletions test/dotcom/realtime_schedule_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ defmodule Dotcom.RealtimeScheduleTest do
description: :rapid_transit,
direction_destinations: %{"0" => "Forest Hills", "1" => "Oak Grove"},
direction_names: %{"0" => "Southbound", "1" => "Northbound"},
external_agency_name: nil,
header: "Orange Line",
id: "Orange",
long_name: "Orange Line",
Expand Down
3 changes: 3 additions & 0 deletions test/dotcom_web/controllers/trip_plan_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ defmodule DotcomWeb.TripPlanControllerTest do
%Stops.Stop{}
end)

cache = Application.get_env(:dotcom, :cache)
cache.flush()

conn = default_conn()

end_of_rating =
Expand Down
20 changes: 4 additions & 16 deletions test/routes/route_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,10 @@ defmodule Routes.RouteTest do
sort_order: 5
}

expected = %{
custom_route?: false,
description: :rapid_transit,
direction_destinations: %{"0" => "Ashmont/Braintree", "1" => "Alewife"},
direction_names: %{"0" => "South", "1" => "North"},
id: "Red",
long_name: "Red Line",
name: "Red Line",
type: 1,
color: "DA291C",
sort_order: 5,
fare_class: :unknown_fare,
line_id: ""
}

assert Route.to_json_safe(route) == expected
assert %{
direction_destinations: %{"0" => "Ashmont/Braintree", "1" => "Alewife"},
direction_names: %{"0" => "South", "1" => "North"}
} = Route.to_json_safe(route)
end
end

Expand Down

0 comments on commit e6aeb97

Please sign in to comment.