Skip to content

Commit

Permalink
add revenue attribute to prediction, trip, and vehicle views
Browse files Browse the repository at this point in the history
  • Loading branch information
bfauble committed Dec 14, 2023
1 parent 96583b7 commit 0a2571f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 6 deletions.
12 changes: 10 additions & 2 deletions apps/api_web/lib/api_web/views/prediction_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ defmodule ApiWeb.PredictionView do
:schedule_relationship,
:status,
:stop_sequence,
:track
:track,
:revenue
])

def preload(predictions, conn, include_opts) do
Expand Down Expand Up @@ -43,7 +44,8 @@ defmodule ApiWeb.PredictionView do
direction_id: p.direction_id,
schedule_relationship: schedule_relationship(p),
status: p.status,
stop_sequence: p.stop_sequence
stop_sequence: p.stop_sequence,
revenue: revenue(p)
}

add_legacy_attributes(attributes, p, conn.assigns.api_version)
Expand Down Expand Up @@ -204,6 +206,12 @@ defmodule ApiWeb.PredictionView do
|> String.upcase()
end

def revenue(%{revenue: atom}) do
atom
|> Atom.to_string()
|> String.upcase()
end

def format_time(%DateTime{} = dt), do: DateTime.to_iso8601(dt)
def format_time(nil), do: nil
end
25 changes: 24 additions & 1 deletion apps/api_web/lib/api_web/views/trip_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,21 @@ defmodule ApiWeb.TripView do

def trip_location(trip, conn), do: trip_path(conn, :show, trip.id)

attributes([:name, :headsign, :direction_id, :wheelchair_accessible, :block_id, :bikes_allowed])
attributes([
:name,
:headsign,
:direction_id,
:wheelchair_accessible,
:block_id,
:bikes_allowed,
:revenue
])

def attributes(%Model.Trip{} = t, conn) do
t
|> super(conn)
|> encode_revenue(t)
end

has_one(
:route,
Expand Down Expand Up @@ -153,4 +167,13 @@ defmodule ApiWeb.TripView do
end
end)
end

defp encode_revenue(attributes, %{revenue: atom}) do
string_val =
atom
|> Atom.to_string()
|> String.upcase()

Map.put(attributes, :revenue, string_val)
end
end
13 changes: 12 additions & 1 deletion apps/api_web/lib/api_web/views/vehicle_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ defmodule ApiWeb.VehicleView do
:current_stop_sequence,
:updated_at,
:occupancy_status,
:carriages
:carriages,
:revenue
])

has_one(
Expand Down Expand Up @@ -45,6 +46,7 @@ defmodule ApiWeb.VehicleView do
|> super(conn)
|> backwards_compatible_attributes(vehicle, conn.assigns.api_version)
|> encode_carriages()
|> encode_revenue(vehicle)
end

for status <- ~w(in_transit_to incoming_at stopped_at)a do
Expand Down Expand Up @@ -101,4 +103,13 @@ defmodule ApiWeb.VehicleView do
occupancy_percentage: carriage.occupancy_percentage
}
end

defp encode_revenue(attributes, %{revenue: atom}) do
string_val =
atom
|> Atom.to_string()
|> String.upcase()

Map.put(attributes, :revenue, string_val)
end
end
3 changes: 2 additions & 1 deletion apps/api_web/test/api_web/views/prediction_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ defmodule ApiWeb.PredictionViewTest do
"departure_time" => "2016-06-07T00:00:00-04:00",
"status" => "All Aboard",
"schedule_relationship" => "ADDED",
"stop_sequence" => 5
"stop_sequence" => 5,
"revenue" => "REVENUE"
}
end

Expand Down
3 changes: 2 additions & 1 deletion apps/api_web/test/api_web/views/trip_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ defmodule ApiWeb.TripViewTest do
"headsign" => "North Station",
"wheelchair_accessible" => 1,
"block_id" => "block",
"bikes_allowed" => 0
"bikes_allowed" => 0,
"revenue" => "REVENUE"
}

assert rendered["data"]["relationships"] ==
Expand Down
39 changes: 39 additions & 0 deletions apps/api_web/test/api_web/views/vehicle_view_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule ApiWeb.VehicleViewTest do
use ApiWeb.ConnCase

# Bring render/3 and render_to_string/3 for testing custom views
import Phoenix.View

alias Model.Vehicle

@vehicle %Vehicle{
id: "vehicle",
revenue: :REVENUE
}

setup %{conn: conn} do
conn = Phoenix.Controller.put_view(conn, ApiWeb.VehicleView)
{:ok, %{conn: conn}}
end

test "render returns JSONAPI", %{conn: conn} do
rendered = render(ApiWeb.VehicleView, "index.json-api", data: @vehicle, conn: conn)
assert rendered["data"]["type"] == "vehicle"
assert rendered["data"]["id"] == "vehicle"

assert rendered["data"]["attributes"] == %{
"direction_id" => nil,
"revenue" => "REVENUE",
"bearing" => nil,
"carriages" => [],
"current_status" => nil,
"current_stop_sequence" => nil,
"label" => nil,
"latitude" => nil,
"longitude" => nil,
"occupancy_status" => nil,
"speed" => nil,
"updated_at" => nil
}
end
end

0 comments on commit 0a2571f

Please sign in to comment.