<commit_before>//
//  DataManager.swift
//  Eatery
//
//  Created by <NAME> on 10/8/14.
//  Copyright (c) 2014 CUAppDev. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON

let separator = ":------------------------------------------"

/**
 Router Endpoints enum
 */
internal enum Router: URLStringConvertible {
    static let baseURLString = "https://now.dining.cornell.edu/api/1.0/dining"
    case Root
    case Eateries
    
    var URLString: String {
        let path: String = {
            switch self {
            case .Root:
                return "/"
            case .Eateries:
                return "/eateries.json"
            }
        }()
        return Router.baseURLString + path
    }
}

/**
 Keys for Cornell API
 These will be in the response dictionary
 */
public enum APIKey : String {
    // Top Level
    case Status    = "status"
    case Data      = "data"
    case Meta      = "meta"
    case Message   = "message"
    
    // Data
    case Eateries  = "eateries"
    
    // Eatery
    case Identifier       = "id"
    case Slug             = "slug"
    case Name             = "name"
    case NameShort        = "nameshort"
    case EateryTypes      = "eateryTypes"
    case AboutShort       = "aboutshort"
    case Latitude         = "latitude"
    case Longitude        = "longitude"
    case Hours            = "operatingHours"
    case Payment          = "payMethods"
    case PhoneNumber      = "contactPhone"
    case CampusArea       = "campusArea"
    case Address          = "location"
    case DiningItems      = "diningItems"
    
    // Hours
    case Date             = "date"
    case Events           = "events"
    
    // Events
    case StartTime        = "startTimestamp"
    case EndTime          = "endTimestamp"
    case StartFormat      = "start"
    case EndFormat        = "end"
    case Menu             = "menu"
    case Summary          = "calSummary"
    
    // Events/Payment/CampusArea/EateryTypes
    case Description      = "descr"
    case ShortDescription = "descrshort"
    
    // Menu
    case Items            = "items"
    case Category         = "category"
    case Item             = "item"
    case Healthy          = "healthy"
    
    // Meta
    case Copyright = "copyright"
    case Timestamp = "responseDttm"
}

/**
 Enumerated Server Response
 
 - Success: String for the status if the request was a success.
 */
enum Status: String {
    case Success = "success"
}

/**
 Error Types
 
 - ServerError: An error arose from the server-side of things
 */
enum DataError: ErrorType {
    case ServerError
}

/// FileName for JSON data location
let FileName = "eateryJSON.dat"

/**
 Returns the path for user documents directory
*/
func getDocumentsDirectory() -> String {
    let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!
    return path
}

/// Top-level class to communicate with Cornell Dining
public class DataManager: NSObject {
    
    /// Gives a shared instance of `DataManager`
    public static let sharedInstance = DataManager()
    
    /// List of all the Dining Locations with parsed events and menus
    private (set) public var eateries: [Eatery] = []
    
    /// Timestamp of last fetch from Cornell API
    public static var dateLastFetched: NSDate?
    
    /// Save
    public func readEateriesFromDisk() -> Bool {
        let path = getDocumentsDirectory().stringByAppendingString(FileName)
        if let data = NSData(contentsOfFile: path) {
            
            let json = JSON(data: data)
            
            let eateryList = json["data"]["eateries"]
            for eateryJSON in eateryList {
                let eatery = Eatery(json: eateryJSON.1)
                self.eateries.append(eatery)
            }
            
            return true
        } else {
            return false
        }
    }
    <commit_msg>Fix Eatery read and implement last fetched date<commit_after>//
//  DataManager.swift
//  Eatery
//
//  Created by <NAME> on 10/8/14.
//  Copyright (c) 2014 CUAppDev. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON

let separator = ":------------------------------------------"

/**
 Router Endpoints enum
 */
internal enum Router: URLStringConvertible {
    static let baseURLString = "https://now.dining.cornell.edu/api/1.0/dining"
    case Root
    case Eateries
    
    var URLString: String {
        let path: String = {
            switch self {
            case .Root:
                return "/"
            case .Eateries:
                return "/eateries.json"
            }
        }()
        return Router.baseURLString + path
    }
}

/**
 Keys for Cornell API
 These will be in the response dictionary
 */
public enum APIKey : String {
    // Top Level
    case Status    = "status"
    case Data      = "data"
    case Meta      = "meta"
    case Message   = "message"
    
    // Data
    case Eateries  = "eateries"
    
    // Eatery
    case Identifier       = "id"
    case Slug             = "slug"
    case Name             = "name"
    case NameShort        = "nameshort"
    case EateryTypes      = "eateryTypes"
    case AboutShort       = "aboutshort"
    case Latitude         = "latitude"
    case Longitude        = "longitude"
    case Hours            = "operatingHours"
    case Payment          = "payMethods"
    case PhoneNumber      = "contactPhone"
    case CampusArea       = "campusArea"
    case Address          = "location"
    case DiningItems      = "diningItems"
    
    // Hours
    case Date             = "date"
    case Events           = "events"
    
    // Events
    case StartTime        = "startTimestamp"
    case EndTime          = "endTimestamp"
    case StartFormat      = "start"
    case EndFormat        = "end"
    case Menu             = "menu"
    case Summary          = "calSummary"
    
    // Events/Payment/CampusArea/EateryTypes
    case Description      = "descr"
    case ShortDescription = "descrshort"
    
    // Menu
    case Items            = "items"
    case Category         = "category"
    case Item             = "item"
    case Healthy          = "healthy"
    
    // Meta
    case Copyright = "copyright"
    case Timestamp = "responseDttm"
}

/**
 Enumerated Server Response
 
 - Success: String for the status if the request was a success.
 */
enum Status: String {
    case Success = "success"
}

/**
 Error Types
 
 - ServerError: An error arose from the server-side of things
 */
enum DataError: ErrorType {
    case ServerError
}

/// FileName for JSON data location
let FileName = "eateryJSON.dat"

/**
 Returns the path for user documents directory
*/
func getDocumentsDirectory() -> String {
    let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!
    return path
}

/// Top-level class to communicate with Cornell Dining
public class DataManager: NSObject {
    
    /// Gives a shared instance of `DataManager`
    public static let sharedInstance = DataManager()
    
    /// List of all the Dining Locations with parsed events and menus
    private (set) public var eateries: [Eatery] = []
    
    /// Timestamp of last fetch from Cornell API
    public var dateLastFetched: NSDate?
    
    /// Save
    public func readEateriesFromDisk() -> Bool {
        let path = getDocumentsDirectory().stringByAppendingString(FileName)
        if let data = NSData(contentsOfFile: path) {
            eateries.removeAll()
            
            let json = JSON(data: data)
            
            let eateryList = json["data"]["eateries"]
            for eateryJSON in eateryList {
                let eatery = Eatery(json: eateryJSON.1)
                self.eateries.append(eatery)
            }
            
            dateLastFetched = NSDate()
            
            return true
        } else {
            return false
        }
    }
    
{'fraction_non_alphanumeric': 0.09631870902672718, 'fraction_numerical': 0.0030257186081694403, 'mean_word_length': 1.712136752136752, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 9, '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': '789917', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2228, 'n_words': 671}