diff --git a/apps/api_web/lib/api_web/views/prediction_view.ex b/apps/api_web/lib/api_web/views/prediction_view.ex index a8ac8819..b0078603 100644 --- a/apps/api_web/lib/api_web/views/prediction_view.ex +++ b/apps/api_web/lib/api_web/views/prediction_view.ex @@ -9,7 +9,8 @@ defmodule ApiWeb.PredictionView do :schedule_relationship, :status, :stop_sequence, - :track + :track, + :revenue ]) def preload(predictions, conn, include_opts) do @@ -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) @@ -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 diff --git a/apps/api_web/lib/api_web/views/trip_view.ex b/apps/api_web/lib/api_web/views/trip_view.ex index 9755c79d..86f1e3af 100644 --- a/apps/api_web/lib/api_web/views/trip_view.ex +++ b/apps/api_web/lib/api_web/views/trip_view.ex @@ -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, @@ -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 diff --git a/apps/api_web/lib/api_web/views/vehicle_view.ex b/apps/api_web/lib/api_web/views/vehicle_view.ex index c046d4fd..fb9e4830 100644 --- a/apps/api_web/lib/api_web/views/vehicle_view.ex +++ b/apps/api_web/lib/api_web/views/vehicle_view.ex @@ -16,7 +16,8 @@ defmodule ApiWeb.VehicleView do :current_stop_sequence, :updated_at, :occupancy_status, - :carriages + :carriages, + :revenue ]) has_one( @@ -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 @@ -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 diff --git a/apps/api_web/test/api_web/views/prediction_view_test.exs b/apps/api_web/test/api_web/views/prediction_view_test.exs index 20314062..6672e94b 100644 --- a/apps/api_web/test/api_web/views/prediction_view_test.exs +++ b/apps/api_web/test/api_web/views/prediction_view_test.exs @@ -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 diff --git a/apps/api_web/test/api_web/views/trip_view_test.exs b/apps/api_web/test/api_web/views/trip_view_test.exs index ae7a5ea9..0cde027c 100644 --- a/apps/api_web/test/api_web/views/trip_view_test.exs +++ b/apps/api_web/test/api_web/views/trip_view_test.exs @@ -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"] == diff --git a/apps/api_web/test/api_web/views/vehicle_view_test.exs b/apps/api_web/test/api_web/views/vehicle_view_test.exs new file mode 100644 index 00000000..7899baee --- /dev/null +++ b/apps/api_web/test/api_web/views/vehicle_view_test.exs @@ -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