<commit_before>//
//  AgendaAPI.swift
//  DroidconBoston
//
//  Created by <NAME> on 3/23/17.
//  Copyright © 2017 Droidcon Boston. All rights reserved.
//

import Foundation
import Alamofire

struct Event {
    
    var name: String
    var talk: String
    var description: String
    var room: String
    var photoUrl: String
    var bio: String
    var time: String
    var date: String
    var twitter: String
    var linkedIn: String
    var facebook: String
    
    init?(json: [String: Any]) throws {
        
        guard let bio = Event.getDataFromKey(key: "gsx$bio", json: json) as? String else {
            throw SerializationError.missing("gsx$bio")
        }
        guard let date = Event.getDataFromKey(key: "gsx$date", json: json) as? String else {
            throw SerializationError.missing("gsx$date")
        }
        guard let description = Event.getDataFromKey(key: "gsx$description", json: json) as? String else {
            throw SerializationError.missing("gsx$description")
        }
        guard let facebook = Event.getDataFromKey(key: "gsx$facebook", json: json) as? String else {
            throw SerializationError.missing("gsx$facebook")
        }
        guard let linkedIn = Event.getDataFromKey(key: "gsx$linkedin", json: json) as? String else {
            throw SerializationError.missing("gsx$linkedin")
        }
        guard let name = Event.getDataFromKey(key: "gsx$name", json: json) as? String else {
            throw SerializationError.missing("gsx$name")
        }
        guard let photoUrl = Event.getDataFromKey(key: "gsx$photolink", json: json) as? String else {
            throw SerializationError.missing("gsx$photolink")
        }
        guard let room = Event.getDataFromKey(key: "gsx$room", json: json) as? String else {
            throw SerializationError.missing("gsx$room")
        }
        guard let talk = Event.getDataFromKey(key: "gsx$talk", json: json) as? String else {
            throw SerializationError.missing("gsx$talk")
        }
        guard let time = Event.getDataFromKey(key: "gsx$time", json: json) as? String else {
            throw SerializationError.missing("gsx$time")
        }
        guard let twitter = Event.getDataFromKey(key: "gsx$twitter", json: json) as? String else {
            throw SerializationError.missing("gsx$twitter")
        }
        
        self.bio = bio
        self.date = date
        self.description = description
        self.facebook = facebook
        self.linkedIn = linkedIn
        self.name = name
        self.photoUrl = photoUrl
        self.room = room
        self.talk = talk
        self.time = time
        self.twitter = twitter
    }
    
    static func getDataFromKey(key: String, json: [String: Any]) -> Any? {
        if let item = json[key] as? [String: Any] {
            // spreadsheet api embeds the value inside '$t' key
            if let value = item["$t"] {
                return value
            }
        }
        return nil;
    }

}

struct AgendaAPI {
    
    static func getAgenda(callback:@escaping (([Event]?) -> Void)) {
        let url = "https://spreadsheets.google.com/feeds/list/1wF628dwex_AkUF1biuQ7s_lpaT-htDVbysGwMv78uFM/od6/public/values?alt=json"
        
        Alamofire.request(url, method: .get).responseJSON { response in
            guard let responseDict = response.result.value as? [String: Any] else {
                // error, possibly network connection
                callback(nil)
                return
            }
            guard let feed = responseDict["feed"] as? [String: Any] else {
                callback(nil)
                return
            }
            guard let entries = feed["entry"] as? [Any] else {
                callback(nil)
                return
            }
            
            
            do {
                var results: [Event] = []
                
                for case let entryJson as [String: Any] in entries {
                    if let entry = try Event(json: entryJson) {
                        results.append(entry)
                    }
                }
                
                callback(results)
            } catch {
                callback(nil)
            }
        }
    }
}<commit_msg>Functions to get both local son and remote<commit_after>//
//  AgendaAPI.swift
//  DroidconBoston
//
//  Created by <NAME> on 3/23/17.
//  Copyright © 2017 Droidcon Boston. All rights reserved.
//

import Foundation
import Alamofire

struct Event {
    
    var name: String
    var talk: String
    var description: String
    var room: String
    var photoUrl: String
    var bio: String
    var time: String
    var date: String
    var twitter: String
    var linkedIn: String
    var facebook: String
    
    init?(json: [String: Any]) throws {
        
        guard let bio = Event.getDataFromKey(key: "gsx$bio", json: json) as? String else {
            throw SerializationError.missing("gsx$bio")
        }
        guard let date = Event.getDataFromKey(key: "gsx$date", json: json) as? String else {
            throw SerializationError.missing("gsx$date")
        }
        guard let description = Event.getDataFromKey(key: "gsx$description", json: json) as? String else {
            throw SerializationError.missing("gsx$description")
        }
        guard let facebook = Event.getDataFromKey(key: "<KEY>", json: json) as? String else {
            throw SerializationError.missing("gsx$facebook")
        }
        guard let linkedIn = Event.getDataFromKey(key: "gsx$linkedin", json: json) as? String else {
            throw SerializationError.missing("gsx$linkedin")
        }
        guard let name = Event.getDataFromKey(key: "<KEY>", json: json) as? String else {
            throw SerializationError.missing("gsx$name")
        }
        guard let photoUrl = Event.getDataFromKey(key: "gsx$photolink", json: json) as? String else {
            throw SerializationError.missing("gsx$photolink")
        }
        guard let room = Event.getDataFromKey(key: "<KEY>", json: json) as? String else {
            throw SerializationError.missing("gsx$room")
        }
        guard let talk = Event.getDataFromKey(key: "gsx$talk", json: json) as? String else {
            throw SerializationError.missing("gsx$talk")
        }
        guard let time = Event.getDataFromKey(key: "gsx$time", json: json) as? String else {
            throw SerializationError.missing("gsx$time")
        }
        guard let twitter = Event.getDataFromKey(key: "gsx$twitter", json: json) as? String else {
            throw SerializationError.missing("gsx$twitter")
        }
        
        self.bio = bio
        self.date = date
        self.description = description
        self.facebook = facebook
        self.linkedIn = linkedIn
        self.name = name
        self.photoUrl = photoUrl
        self.room = room
        self.talk = talk
        self.time = time
        self.twitter = twitter
    }
    
    static func getDataFromKey(key: String, json: [String: Any]) -> Any? {
        if let item = json[key] as? [String: Any] {
            // spreadsheet api embeds the value inside '$t' key
            if let value = item["$t"] {
                return value
            }
        }
        return nil;
    }

}

struct AgendaAPI {
    
    static func getAgendaRemote(callback:@escaping (([Event]?) -> Void)) {
        
        let key = "<KEY>"
        let url = "https://spreadsheets.google.com/feeds/list/\(key)/od6/public/values?alt=json"
        
        Alamofire.request(url, method: .get).responseJSON { response in
            guard let json = response.result.value as? [String: Any] else {
                // error, possibly network connection
                callback(nil)
                return
            }
            
            AgendaAPI.parseJson(json: json, callback: { (results) in
                callback(results)
            })
        }
    }
    
    static func getAgendaLocal(callback:@escaping (([Event]?) -> Void)) {
        do {
            if let file = Bundle.main.url(forResource: "DefaultData", withExtension: "json") {
                
                let jsonData = try Data(contentsOf: file)
                let json = try JSONSerialization.jsonObject(with: jsonData, options: []) as! [String: Any]
                
                AgendaAPI.parseJson(json: json, callback: { (results) in
                    callback(results)
                })
                return;
            }
        } catch {
            print(error.localizedDescription)
            callback(nil)
        }
    }
    
    static func parseJson(json: [String: Any], callback:@escaping (([Event]?) -> Void)) {
        guard let feed = json["feed"] as? [String: Any] else {
            callback(nil)
            return
        }
        guard let entries = feed["entry"] as? [Any] else {
            callback(nil)
            return
        }
        
        
        do {
            var results: [Event] = []
            
            for case let entryJson as [String: Any] in entries {
                if let entry = try Event(json: entryJson) {
                    results.append(entry)
                }
            }
            
            callback(results)
        } catch {
            callback(nil)
        }
    }
}
{'fraction_non_alphanumeric': 0.10323690719432196, 'fraction_numerical': 0.0030110764598343907, 'mean_word_length': 1.7736355502535044, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2549230', 'n_tokens_mistral': 2622, 'n_tokens_neox': 2563, 'n_words': 691}