Skip to content

Instantly share code, notes, and snippets.

@ronaldsmartin
Created September 22, 2014 03:11
Show Gist options
  • Save ronaldsmartin/243f3d2bc7d0f227d705 to your computer and use it in GitHub Desktop.
Save ronaldsmartin/243f3d2bc7d0f227d705 to your computer and use it in GitHub Desktop.
Untested async JSON request/unmarshalling for next version of APO app
//
// Directory.swift
// APO-DZ
//
// Created by Ronald Martin on 9/21/14.
// Copyright (c) 2014 Alpha Phi Omega, Delta Zeta Chapter. All rights reserved.
//
import Foundation
typealias Json = AnyObject
typealias JsonObject = [String: Json]
typealias JsonArray = [JsonObject]
struct Brother {
let json: JsonObject
// mark: readonly computed properties
var name: String {
get {
let nameKey = "First_and_Last_Name"
return json[nameKey] as Json? as String
}
}
var pledgeClass: String {
get {
let classKey = "Pledge_Class"
return json[classKey] as Json? as String
}
}
var year: Int {
get {
let yearKey = "Graduation_Year"
return json[yearKey] as Json? as Int
}
}
var major: String {
get {
let majorKey = "Major"
return json[majorKey] as Json? as String
}
}
var email: String {
get {
let emailKey = "Email_Address"
return json[emailKey] as Json? as String
}
}
var phone: String {
get {
let phoneKey = "Phone_Number"
return json[phoneKey] as Json? as String
}
}
}
struct Directory {
// mark: constants
let BrotherDirName = "Brothers"
let PledgeDirName = "Pledges"
let AlumniDirName = "Alumni"
// mark: properties
var brothers: JsonArray?, pledges: JsonArray?, alumni: JsonArray?
// mark: initialization
init(callback: (Directory) -> ()) {
if let (broDir, pledgeDir, alumDir) = retrieveDirectories() {
self.brothers = broDir
self.pledges = pledgeDir
self.alumni = alumDir
callback(self)
} else {
downloadDirectory(callback)
}
}
func retrieveDirectories() -> (JsonArray, JsonArray, JsonArray)? {
let prefs = NSUserDefaults.standardUserDefaults()
let brothers = prefs.objectForKey(BrotherDirName) as? JsonArray
let pledges = prefs.objectForKey(PledgeDirName) as? JsonArray
let alumni = prefs.objectForKey(AlumniDirName) as? JsonArray
if brothers != nil && pledges != nil && alumni != nil {
return (brothers!, pledges!, alumni!)
} else {
return nil
}
}
mutating func downloadDirectory(callback: (Directory) -> ()) {
// GET data from sheet and deserialize the response, populating the table if successful.
let requestManager = AFHTTPSessionManager()
let url = URLs.directoryUrlWithSheetName("All")
requestManager.responseSerializer = AFJSONResponseSerializer()
requestManager.GET(url,
parameters:nil,
success: { (task: NSURLSessionDataTask!, responseObject: Json!) in
println("Directory async response: \(responseObject)")
self.brothers = responseObject[self.BrotherDirName] as? JsonArray
self.pledges = responseObject[self.PledgeDirName] as? JsonArray
self.alumni = responseObject[self.AlumniDirName] as? JsonArray
callback(self)
},
failure: { (task: NSURLSessionDataTask!, error: NSError!) in
println("Directory HTTPRequest Error: " + error.localizedDescription)
}
)
}
func brotherAtIndexPath(index: NSIndexPath) -> Brother? {
var subdirectory: [JsonObject]?
switch index.section {
case 0:
subdirectory = self.brothers
case 1:
subdirectory = self.pledges
case 2:
subdirectory = self.alumni
default:
subdirectory = nil
}
if let jsonForBrother = subdirectory?[index.row] {
return Brother(json: jsonForBrother)
} else {
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment