Latest 2.2.2
Homepage https://github.com/clrung/WMATAFetcher
License MIT
Platforms ios 8.0, osx 10.9, tvos 9.0, watchos 2.0
Dependencies SwiftyJSON
Authors

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
  2. 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.

Latest podspec

{
    "name": "WMATAFetcher",
    "version": "2.2.2",
    "summary": "Wrapper Pod for the WMATA API",
    "description": "WMATAFetcher contains helper methods to fetch predictions from the WMATA API.nIt uses [SwiftyJSON](https://cocoapods.org/pods/SwiftyJSON) to populate an array of [Train](http://cocoadocs.org/docsets/WMATAFetcher/1.0.1/Classes/Train.html) objects, which contain the number of cars, destination, group, line, location, and minutes until train arrival.",
    "homepage": "https://github.com/clrung/WMATAFetcher",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Christopher Rung": "[email protected]"
    },
    "source": {
        "git": "https://github.com/clrung/WMATAFetcher.git",
        "tag": "2.2.2"
    },
    "social_media_url": "https://twitter.com/clrung",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "source_files": "WMATAFetcher/**/*.swift",
    "dependencies": {
        "SwiftyJSON": [
            "~> 3.0"
        ]
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This