gtfs/realtime/feed
GTFS Realtime Feed
Module for working with GTFS Realtime feeds. Provides functions to decode and query realtime feed data.
Example
import gtfs/realtime/feed
import gleam/io
pub fn main() {
// Decode a feed from bytes
let assert Ok(feed_message) = feed.decode(my_protobuf_bytes)
// Get all trip updates
let updates = feed.get_trip_updates(feed_message)
// Get all alerts
let alerts = feed.get_alerts(feed_message)
}
Types
Error type for HTTP fetch operations
pub type FetchError {
HttpError(String)
StatusError(Int)
DecodeError(protobin.ParseError)
}
Constructors
-
HttpError(String)HTTP request failed
-
StatusError(Int)Response was not successful (non-2xx status)
-
DecodeError(protobin.ParseError)Protocol Buffer decoding failed
Values
pub fn decode(
data: BitArray,
) -> Result(types.FeedMessage, protobin.ParseError)
Decode a GTFS Realtime feed from protobuf binary data
pub fn entity_count(feed: types.FeedMessage) -> Int
Get the total number of entities in the feed
pub fn fetch(
url: String,
) -> Result(types.FeedMessage, FetchError)
Fetch and decode a GTFS Realtime feed from a URL
Example
import gtfs/realtime/feed
pub fn main() {
case feed.fetch("https://api.transit.example.com/gtfs-rt/vehiclepositions") {
Ok(feed_message) -> {
let vehicles = feed.get_vehicle_positions(feed_message)
// Process vehicles...
}
Error(feed.HttpError(msg)) -> io.println("HTTP error: " <> msg)
Error(feed.StatusError(code)) -> io.println("Bad status: " <> int.to_string(code))
Error(feed.DecodeError(_)) -> io.println("Failed to decode protobuf")
}
}
pub fn from_bytes(
data: BitArray,
) -> Result(types.FeedMessage, protobin.ParseError)
Decode a GTFS Realtime feed from raw bytes (alias for decode)
pub fn get_alerts(feed: types.FeedMessage) -> List(types.Alert)
Get all alerts from a feed
pub fn get_alerts_for_route(
feed: types.FeedMessage,
route_id: String,
) -> List(types.Alert)
Get all alerts affecting a specific route
pub fn get_alerts_for_stop(
feed: types.FeedMessage,
stop_id: String,
) -> List(types.Alert)
Get all alerts affecting a specific stop
pub fn get_entity(
feed: types.FeedMessage,
id: String,
) -> option.Option(types.FeedEntity)
Get a specific entity by ID
pub fn get_timestamp(
feed: types.FeedMessage,
) -> option.Option(Int)
Get the feed timestamp (when the content was created)
pub fn get_trip_delay(
feed: types.FeedMessage,
trip_id: String,
) -> option.Option(Int)
Get delay for a specific trip (in seconds, positive = late)
pub fn get_trip_update(
feed: types.FeedMessage,
trip_id: String,
) -> option.Option(types.TripUpdate)
Get trip update for a specific trip ID
pub fn get_trip_updates(
feed: types.FeedMessage,
) -> List(types.TripUpdate)
Get all trip updates from a feed
pub fn get_vehicle_position(
feed: types.FeedMessage,
vehicle_id: String,
) -> option.Option(types.VehiclePosition)
Get vehicle position for a specific vehicle ID
pub fn get_vehicle_positions(
feed: types.FeedMessage,
) -> List(types.VehiclePosition)
Get all vehicle positions from a feed
pub fn get_vehicles_on_route(
feed: types.FeedMessage,
route_id: String,
) -> List(types.VehiclePosition)
Get all vehicle positions for a specific route
pub fn is_full_dataset(feed: types.FeedMessage) -> Bool
Check if the feed is a full dataset or differential update
pub fn trip_update_count(feed: types.FeedMessage) -> Int
Get count of trip updates in the feed
pub fn vehicle_position_count(feed: types.FeedMessage) -> Int
Get count of vehicle positions in the feed