Skip to content

clrung/WMATAFetcher

Repository files navigation

WMATAFetcher language Version License Platform

Description

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.

Requirements

  • iOS 8.0+
  • macOS 10.9+
  • tvOS 9.0+
  • watchOS 2.0+

Installation

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

Usage

Instantiation

First, create a new WMATAFetcher object, passing it your WMATA API key:

var wmataFetcher = WMATAFetcher(WMATA_API_KEY: "[YOUR_WMATA_KEY]")

Spaces between groups

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

Fetching

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!))")
	}
})

Error Handling

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:

  1. -1009
  • The internet connection is offline
  1. 401
  • Unauthorized. This is likely a bad WMATA key.

Dependencies

Notable Projects

  • DC Metro Widget
  • Today extension for macOS' Notification Center
  • Inspiration for this Pod

Available on the Mac App Store

License

WMATAFetcher is available under the MIT license. See the LICENSE file for more info.