This is an API library for reading data from the NextBus public JSON feed in Go.
import "github.com/cogspace/nextbus"
agencies, err := nextbus.GetAgencies()
routes, err := nextbus.GetRoutes("glendale")
routeConfig, err := nextbus.GetRouteConfig("glendale", "12", false, true)
schedules, err := nextbus.GetSchedules("glendale", "12")
predictions, err := nextbus.GetPredictions("glendale", "12", "gtc_d")
predictions, err := nextbus.GetPredictionsMulti(
"glendale",
[]nextbus.Stop{
{ Route: "12", Stop: "gtc_d" },
{ Route: "12", Stop: "sanchev" },
},
)
In the interest of concise, sequential code, some of the types are composed of rather deeply nested anonymous subtypes, for example:
type Schedule struct {
ServiceClass string
Title string
Tr []struct {
Stop []struct {
Content string
Tag string
EpochTime string
}
BlockId string
}
Direction string
Tag string
Header struct {
Stop []struct {
Content string
Tag string
}
}
ScheduleClass string
}
The only real problem with this is declaring a type for, say, Tr
can be...
cumbersome.
func getBlockId(tr struct{ Stop []struct{ Content, Tag, EpochTime string}; BlockId string }) string {
return tr.BlockId
}
func getStops(tr struct{ Stop []struct{ Content, Tag, EpochTime string }; BlockId string }) []struct{ Content, Tag, EpochTime string } {
// Now that's just silly...
return tr.Stop
}
To alleviate this problem, simply name whichever types you need to use.
type Stop struct{ Content, Tag, EpochTime string }
type Tr struct{ Stop []struct{ Content, Tag, EpochTime string }; BlockId string }
func getBlockId(tr Tr) string {
return tr.BlockId
}
func getStops(tr Tr) []Stop {
return tr.Stop
}
In practice, naming theses types is not usually necessary in Go client code.
ch := make(chan string)
go func(ch chan string) {
schedules, err := nextbus.GetSchedules("glendale", "12")
if err != nil {
log.Fatal(err)
}
for _, schedule := range schedules {
for _, tr := range schedule.Tr {
ch <- tr.BlockId
}
}
}(ch)
...
The NextBus feed documentation only covers the XML API, but the JSON API is very similar.