Skip to content

Commit

Permalink
feat(MBTA.Api): guard checking type and length (#2086)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecristen authored Jun 5, 2024
1 parent 70a3e19 commit 118305d
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 14 deletions.
4 changes: 4 additions & 0 deletions lib/mbta/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ defmodule MBTA.Api do

@req Application.compile_env(:dotcom, :req_module)

defmacro is_valid_potential_id(id) do
quote do: is_binary(unquote(id)) and unquote(id) != ""
end

@impl MBTA.Api.Behaviour
def get_json(url, params \\ []) do
case client() |> @req.get(url: URI.encode(url), params: params) do
Expand Down
8 changes: 7 additions & 1 deletion lib/mbta/api/route_patterns.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule MBTA.Api.RoutePatterns do
Responsible for fetching Route Pattern data from the V3 API.
"""

import MBTA.Api, only: [is_valid_potential_id: 1]

alias Routes.Route

@mbta_api Application.compile_env!(:dotcom, :mbta_api_module)
Expand All @@ -15,7 +17,11 @@ defmodule MBTA.Api.RoutePatterns do
end

@spec get(Route.id_t(), keyword()) :: api_response_t()
def get(id, params \\ []) do
def get(id, params \\ [])

def get(id, params) when is_valid_potential_id(id) do
@mbta_api.get_json("/route_patterns/#{id}", params)
end

def get(id, _), do: {:error, {:invalid_id, id}}
end
8 changes: 7 additions & 1 deletion lib/mbta/api/routes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule MBTA.Api.Routes do
Responsible for fetching Route data from the V3 API.
"""

import MBTA.Api, only: [is_valid_potential_id: 1]

alias Routes.Route
alias Stops.Stop

Expand All @@ -16,10 +18,14 @@ defmodule MBTA.Api.Routes do
end

@spec get(Route.id_t(), keyword()) :: api_response_t()
def get(id, params \\ []) do
def get(id, params \\ [])

def get(id, params) when is_valid_potential_id(id) do
@mbta_api.get_json("/routes/#{id}", params)
end

def get(id, _), do: {:error, {:invalid_id, id}}

@spec by_type(Route.type_int(), keyword()) :: api_response_t()
def by_type(type, params \\ []) do
params = put_in(params[:type], type)
Expand Down
12 changes: 10 additions & 2 deletions lib/mbta/api/services.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
defmodule MBTA.Api.Services do
@moduledoc false
@moduledoc """
Fetches Service data from the MBTA V3 API.
"""

import MBTA.Api, only: [is_valid_potential_id: 1]

@mbta_api Application.compile_env!(:dotcom, :mbta_api_module)

def all(params \\ []) do
@mbta_api.get_json("/services/", params)
end

def get(id, params \\ []) do
def get(id, params \\ [])

def get(id, params) when is_valid_potential_id(id) do
@mbta_api.get_json("/services/#{id}", params)
end

def get(id, _), do: {:error, {:invalid_id, id}}
end
8 changes: 6 additions & 2 deletions lib/mbta/api/shapes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ defmodule MBTA.Api.Shapes do
Responsible for fetching Shape data from the V3 API.
"""

import MBTA.Api, only: [is_valid_potential_id: 1]

@mbta_api Application.compile_env!(:dotcom, :mbta_api_module)

def all(params \\ []) do
@mbta_api.get_json("/shapes/", params)
end

def by_id(id) do
@mbta_api.get_json("/shapes/" <> id)
def by_id(id) when is_valid_potential_id(id) do
@mbta_api.get_json("/shapes/#{id}")
end

def by_id(id), do: {:error, {:invalid_id, id}}
end
8 changes: 7 additions & 1 deletion lib/mbta/api/stops.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ defmodule MBTA.Api.Stops do
Responsible for fetching Stop data from the V3 API.
"""

import MBTA.Api, only: [is_valid_potential_id: 1]

@mbta_api Application.compile_env!(:dotcom, :mbta_api_module)

def all(params \\ []) do
@mbta_api.get_json("/stops/", params)
end

def by_gtfs_id(gtfs_id, params \\ []) do
def by_gtfs_id(gtfs_id, params \\ [])

def by_gtfs_id(gtfs_id, params) when is_valid_potential_id(gtfs_id) do
@mbta_api.get_json("/stops/#{gtfs_id}", params)
end

def by_gtfs_id(gtfs_id, _), do: {:error, {:invalid_id, gtfs_id}}
end
10 changes: 8 additions & 2 deletions lib/mbta/api/trips.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ defmodule MBTA.Api.Trips do
Responsible for fetching Trip data from the MBTA Api.
"""

import MBTA.Api, only: [is_valid_potential_id: 1]

@mbta_api Application.compile_env!(:dotcom, :mbta_api_module)

def by_id(id, params \\ []) do
@mbta_api.get_json("/trips/" <> id, params)
def by_id(id, params \\ [])

def by_id(id, params) when is_valid_potential_id(id) do
@mbta_api.get_json("/trips/#{id}", params)
end

def by_id(id, _), do: {:error, {:invalid_id, id}}

def by_route(route_id, params \\ []) do
params = Kernel.put_in(params[:route], route_id)

Expand Down
2 changes: 1 addition & 1 deletion test/dotcom_web/controllers/trip_plan_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ defmodule DotcomWeb.TripPlanControllerTest do
end

test "doesn't set custom_route? flag for regular routes", %{itineraries: itineraries} do
expect(MBTA.Api.Mock, :get_json, fn "/routes/" <> _, _ ->
stub(MBTA.Api.Mock, :get_json, fn "/routes/" <> _, _ ->
%JsonApi{
data: [build(:route_item)]
}
Expand Down
4 changes: 2 additions & 2 deletions test/mbta/api/route_patterns_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Mbta.Api.RoutePatternsTest do

test "get/1 returns a route pattern" do
# Setup
id = 1
id = Faker.Internet.slug()

expect(Mock, :get_json, fn url, _ ->
assert url == "/route_patterns/#{id}"
Expand All @@ -34,7 +34,7 @@ defmodule Mbta.Api.RoutePatternsTest do
end)

# Exercise
route_pattern = RoutePatterns.get(1)
route_pattern = RoutePatterns.get(id)

# Verify
assert route_pattern == %{}
Expand Down
2 changes: 1 addition & 1 deletion test/mbta/api/routes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule MBTA.Api.RoutesTest do

test "get/1 returns a route" do
# Setup
id = :rand.uniform(100)
id = Faker.Internet.slug()

expect(Mock, :get_json, fn url, _ ->
assert url == "/routes/#{id}"
Expand Down
2 changes: 1 addition & 1 deletion test/mbta/api/services_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule MBTA.Api.ServicesTest do

test "get/1 returns a service" do
# Setup
id = :rand.uniform(100)
id = Faker.Internet.slug()

expect(Mock, :get_json, fn url, _ ->
assert url == "/services/#{id}"
Expand Down

0 comments on commit 118305d

Please sign in to comment.