A cross-platform CocoaPod that fetches information from WMATA's API and stores it in Train
objects, which are modeled after the API's AIMPredictionTrainInfo object.
- iOS 8.0+
- macOS 10.9+
- tvOS 9.0+
- watchOS 2.0+
WMATAFetcher is available via CocoaPods. To install it, add the following to your Podfile
:
target 'TargetName' do
pod 'WMATAFetcher'
end
Next, run the following in a Terminal:
$ pod install
First, create a new WMATAFetcher
object, passing it your WMATA API key:
var wmataFetcher = WMATAFetcher(WMATA_API_KEY: "[YOUR_WMATA_KEY]")
The isSpaceInTrainArray
BOOL
determines if a Station.Space
object will separate each group in the Train
array (default: true
).
You may initialize this value when instantiating a new WMATAFetcher
object:
var wmataFetcher = WMATAFetcher(WMATA_API_KEY: "[YOUR_WMATA_KEY]", isSpaceInTrainArray: false)
You may also change the value of isSpaceInTrainArray
directly:
wmataFetcher.isSpaceInTrainArray = false
Pass wmataFetcher.getStationPredictions
a station code to get predictions. Implement onCompleted
to handle the TrainResponse
returned.
If trainResponse.errorCode
is nil
, we can safely force upwrap trainResponse.trains?
.
let wmataFetcher = WMATAFetcher(WMATA_API_KEY: "[API KEY HERE]")
let metroCenterStationCode = Station(description: "Metro Center")!.rawValue
wmataFetcher.getStationPredictions(stationCode: metroCenterStationCode, onCompleted: {
trainResponse in
if trainResponse.errorCode == nil {
for train in trainResponse.trains! {
print(train.debugDescription);
}
} else {
switch trainResponse.errorCode! {
case -1009:
print("Internet connection is offline")
default:
print("Prediction fetch failed (Code: \(trainResponse.errorCode!))")
}
})
TrainResponse.errorCode
is an Int?
representing the HTTP status code returned by WMATA's API. If it is nil
, the fetch was successful. The most common error codes are:
- -1009
- The internet connection is offline
- 401
- Unauthorized. This is likely a bad WMATA key.
- DC Metro Widget
- Today extension for macOS' Notification Center
- Inspiration for this
Pod
WMATAFetcher is available under the MIT license. See the LICENSE file for more info.