Information services for trains in the UK, using the National Rail open API
The API is a stab at a friendlier interface for the National Rail Live Departure Boards Web Service
TrainInformationService can be installed using Swift Package Manager or CocoaPods.
Swift Package Manager requires Swift version 4.0 or higher. First, create a
Package.swift
file. It should look like:
dependencies: [
.package(url: "https://github.com/dchakarov/TrainInformationService.git", from: "0.1.0"),
]
swift build
should then pull in and compile TrainInformationService for you to begin using.
To install it, simply add the following line to your Podfile:
pod 'TrainInformationService'
In order to use this framework you need a token for National Rail Enquiries LDBWS (OpenLDBWS). You can register for free and receive you token here - http://realtime.nationalrail.co.uk/OpenLDBWSRegistration/. For more information about the available APIs the National Rail offers you can read here - http://www.nationalrail.co.uk/100296.aspx
After you get your token you can instantiate the service like this:
import TrainInformationService
let trainInformationService = TrainInformationService(
apiUrl: "https://lite.realtime.nationalrail.co.uk/OpenLDBWS/ldb11.asmx",
token: "your-token-goes-here")
Currently the service has support for two of the OpenLDWS methods. Both are asynchronous as they are fetching the data in real time from the National Rail Live Departure Boards Web Service. For a full list of methods and documentation you can go here - https://lite.realtime.nationalrail.co.uk/OpenLDBWS/
You can find the list of stations with their CSR codes here - http://www.nationalrail.co.uk/stations_destinations/48541.aspx
let stationCSRCode = "PAD" // Paddington
let paddingtonBoard = await trainService.stationBoard(for: stationCSRCode, items: 10)
The resulting StationBoard
would be an object with a generatedAt date, full origin and destination names, and an array of up to 10 DepartingService objects.
You can fetch results only to a specific destination, using its CSR code
let originCSRCode = "KGX" // Kings Cross
let destinationCSRCode = "FPK" // Finsbury Park
let filteredBoard = await trainService.stationBoard(for: originCSRCode, to: destinationCSRCode, items: 10)
let stationCSRCode = "PAD" // Paddington
trainInformationService.departureBoard(for: stationCSRCode, items: 5) { result in
switch result {
case .success(let board):
print(board)
case .error(_):
break
}
}
The resulting board
would be an array of up to 5 DepartingService objects.
Given you have a service ID (e.g. a DepartingService object from the above call), you can get a list of stops remaining on its schedule by doing this:
trainInformationService.serviceDetails(service.serviceId) { callingPoints in
print(callingPoints)
}
Here callingPoints
is an array of CallingPoint objects.
Contributions are welcome! Just submit a PR.
Dimitar Chakarov, dimitar@dchakarov.com
This is free and unencumbered software released into the public domain. See the LICENSE file for more info.