-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide agency information for route (#834)
* chore: agency model * chore: agency parser * chore: agency state module * chore: parse agency.txt from GTFS zip * feat: surface agency relationship in route view * docs: document agency relationship on route * docs: also document line relationship from route * docs: also document route_patterns relationship * fix: indicate that all of the relationships on routes are nullable
- Loading branch information
Showing
11 changed files
with
142 additions
and
2 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
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,5 @@ | ||
defmodule ApiWeb.AgencyView do | ||
use ApiWeb.Web, :api_view | ||
|
||
attributes([:agency_name]) | ||
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,22 @@ | ||
defmodule Model.Agency do | ||
@moduledoc """ | ||
Agency represents a branded agency operating transit services. | ||
""" | ||
|
||
use Recordable, [ | ||
:id, | ||
:agency_name | ||
] | ||
|
||
@type id :: String.t() | ||
|
||
@typedoc """ | ||
* `:id` - Unique ID | ||
* `:agency_name` - Full name of the agency. See | ||
[GTFS `agency.txt` `agency_name`](https://github.com/google/transit/blob/master/gtfs/spec/en/reference.md#agencytxt) | ||
""" | ||
@type t :: %__MODULE__{ | ||
id: id, | ||
agency_name: String.t() | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Parse.Agency do | ||
@moduledoc """ | ||
Parses `agency.txt` CSV from GTFS zip | ||
agency_id,agency_name,agency_url,agency_timezone,agency_lang,agency_phone | ||
1,MBTA,http://www.mbta.com,America/New_York,EN,617-222-3200 | ||
""" | ||
|
||
use Parse.Simple | ||
alias Model.Agency | ||
|
||
@doc """ | ||
Parses (non-header) row of `agency.txt` | ||
## Columns | ||
* `"agency_id"` - `Model.Agency.t` - `id` | ||
* `"agency_name"` - `Model.Agency.t` - `agency_name` | ||
""" | ||
def parse_row(row) do | ||
%Agency{ | ||
id: copy(row["agency_id"]), | ||
agency_name: copy(row["agency_name"]) | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Parse.AgencyTest do | ||
use ExUnit.Case, async: true | ||
|
||
import Parse.Agency | ||
alias Model.Agency | ||
|
||
describe "parse_row/1" do | ||
test "parses a route CSV map into an %Agency{}" do | ||
row = %{ | ||
"agency_id" => "1", | ||
"agency_name" => "MBTA", | ||
"agency_url" => "http://www.mbta.com", | ||
"agency_timezone" => "America/New_York", | ||
"agency_lang" => "EN", | ||
"agency_phone" => "617-222-3200" | ||
} | ||
|
||
expected = %Agency{ | ||
id: "1", | ||
agency_name: "MBTA" | ||
} | ||
|
||
assert parse_row(row) == expected | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule State.Agency do | ||
@moduledoc """ | ||
Stores and indexes `Model.Agency.t` from `agency.txt`. | ||
""" | ||
|
||
use State.Server, | ||
indices: [:id], | ||
fetched_filename: "agency.txt", | ||
parser: Parse.Agency, | ||
recordable: Model.Agency | ||
|
||
alias Model.Agency | ||
|
||
@spec by_id(Agency.id()) :: Agency.t() | nil | ||
def by_id(id) do | ||
case super(id) do | ||
[] -> nil | ||
[agency] -> agency | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule State.AgencyTest do | ||
use ExUnit.Case | ||
alias Model.Agency | ||
|
||
setup do | ||
State.Agency.new_state([]) | ||
end | ||
|
||
test "returns nil for unknown agency" do | ||
assert State.Agency.by_id("1") == nil | ||
end | ||
|
||
test "it can add an agency and query it" do | ||
agency = %Agency{ | ||
id: "1", | ||
agency_name: "Made-Up Transit Agency" | ||
} | ||
|
||
State.Agency.new_state([agency]) | ||
|
||
assert State.Agency.by_id("1") == agency | ||
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