<commit_before>//
//  JSONWarehouse.swift
//  JSONWarehouse
//
//  Created by <NAME> on 10/29/15.
//  Copyright © 2015 That Thing in Swift. All rights reserved.
//

import Foundation

/** 
JSONWarehouse serializes and deserializes data 

A `JSONWarehouse` is passed in the init function of a struct that conforms to `Storable`
*/
public class JSONWarehouse: Warehouseable, WarehouseCacheable {
    var key: String
    var context: AnyObject?
    
    public init(key: String) {
        self.key = key
    }
    
    public init(context: AnyObject) {
        self.key = ""
        self.context = context
    }

    /**
     Retrieve a `StorableDefaultType` for a given key
     - parameter valueKey: The item's key
     - returns: T?

     - SeeAlso: `StorableDefaultType`
     */
    public func get<T: StorableDefaultType>(valueKey: String) -> T? {

        guard let dictionary = loadCache(),
            let result = dictionary[valueKey] as? T else {
                return nil
        }
        return result
    }

    /**
     Retrieve a collection of `StorableDefaultType`s for a given key
     - parameter valueKey: The item's key
     - returns: [T]?

     - SeeAlso: `StorableDefaultType`
     */
    public func get<T: StorableDefaultType>(valueKey: String) -> [T]? {

        guard let dictionary = loadCache() as? Dictionary<String, AnyObject>,
            let result = dictionary[valueKey] as? Array<AnyObject> else {
                return nil
        }

        var unpackedItems = [T]()
        for case let item as T in result {
            unpackedItems.append(item)
        }

        return unpackedItems
    }

    /**
     Retrieve a generic object conforming to `Storable` for a given key
     - parameter valueKey: The item's key
     - returns: T?

     - SeeAlso: `Storable`
     */
    public func get<T: Storable>(valueKey: String) -> T? {

        guard let dictionary = loadCache() as? Dictionary<String, AnyObject>,
            let result = dictionary[valueKey] else {
                return nil
        }

        let warehouse = JSONWarehouse(context: result)
        return T(warehouse: warehouse)
    }

    /**
     Retrieve a collection of generic objects conforming to `Storable` for a given key
     - parameter valueKey: The item's key
     - returns: [T]?

     - SeeAlso: `Storable`
     */
    public func get<T: Storable>(valueKey: String) -> [T]? {

        guard let dictionary = loadCache() as? Dictionary<String, AnyObject>,
            let result = dictionary[valueKey] as? Array<AnyObject> else {
                return nil
        }

        var unpackedItems = [T]()
        for case let item as Dictionary<String, AnyObject> in result {
            let warehouse = JSONWarehouse(context: item)
            if let item = T(warehouse: warehouse) {
                unpackedItems.append(item)
            }
        }

        return unpackedItems
    }
    
    func write(object: AnyObject, expires: StorageExpiry) {
        let cacheLocation = cacheFileURL()
        var storableDictionary = [String: AnyObject]()
        
        storableDictionary["expires"] = expires.toDate().timeIntervalSince1970
        storableDictionary["storage"] = object
        
        let _ = (storableDictionary as NSDictionary).writeToURL(cacheLocation, atomically: true)
    }
    
    func removeCache() {
        try! NSFileManager.defaultManager().removeItemAtURL(cacheFileURL())
    }
    
    func loadCache() -> AnyObject? {

        guard context == nil else {
            return context
        }

        let cacheLocation = cacheFileURL()
        
        if let metaDictionary = NSDictionary(contentsOfURL: cacheLocation),
            let cache = metaDictionary["storage"] {
                return cache
        }

        return nil
    }
    
    func cacheExists() -> Bool {

        guard NSFileManager.defaultManager().fileExistsAtPath(cacheFileURL().path!),
            let metaDictionary = NSDictionary(contentsOfURL: cacheFileURL()) else {
                return false
        }

        guard let expires = metaDictionary["expires"] as? NSTimeInterval else {
            // no expire time means old cache, never expires
            return true
        }

        let nowInterval = NSDate().timeIntervalSince1970
        
        if expires > nowInterval {
            return true
        } else {
            removeCache()
            return false
        }
    }
    
    func cacheFileURL() -> NSURL {
        let url = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first!
        
        let writeDirectory = url.URLByAppendingPathComponent("com.thatthinginswift.pantry")
        let cacheLocation = writeDirectory.URLByAppendingPathComponent(self.key)
        
        try! NSFileManager.defaultManager().createDirectoryAtURL(writeDirectory, withIntermediateDirectories: true, attributes: nil)
        
        return cacheLocation
    }
}
<commit_msg>Remove warnings when removing cache.<commit_after>//
//  JSONWarehouse.swift
//  JSONWarehouse
//
//  Created by <NAME> on 10/29/15.
//  Copyright © 2015 That Thing in Swift. All rights reserved.
//

import Foundation

/** 
JSONWarehouse serializes and deserializes data 

A `JSONWarehouse` is passed in the init function of a struct that conforms to `Storable`
*/
public class JSONWarehouse: Warehouseable, WarehouseCacheable {
    var key: String
    var context: AnyObject?
    
    public init(key: String) {
        self.key = key
    }
    
    public init(context: AnyObject) {
        self.key = ""
        self.context = context
    }

    /**
     Retrieve a `StorableDefaultType` for a given key
     - parameter valueKey: The item's key
     - returns: T?

     - SeeAlso: `StorableDefaultType`
     */
    public func get<T: StorableDefaultType>(valueKey: String) -> T? {

        guard let dictionary = loadCache(),
            let result = dictionary[valueKey] as? T else {
                return nil
        }
        return result
    }

    /**
     Retrieve a collection of `StorableDefaultType`s for a given key
     - parameter valueKey: The item's key
     - returns: [T]?

     - SeeAlso: `StorableDefaultType`
     */
    public func get<T: StorableDefaultType>(valueKey: String) -> [T]? {

        guard let dictionary = loadCache() as? Dictionary<String, AnyObject>,
            let result = dictionary[valueKey] as? Array<AnyObject> else {
                return nil
        }

        var unpackedItems = [T]()
        for case let item as T in result {
            unpackedItems.append(item)
        }

        return unpackedItems
    }

    /**
     Retrieve a generic object conforming to `Storable` for a given key
     - parameter valueKey: The item's key
     - returns: T?

     - SeeAlso: `Storable`
     */
    public func get<T: Storable>(valueKey: String) -> T? {

        guard let dictionary = loadCache() as? Dictionary<String, AnyObject>,
            let result = dictionary[valueKey] else {
                return nil
        }

        let warehouse = JSONWarehouse(context: result)
        return T(warehouse: warehouse)
    }

    /**
     Retrieve a collection of generic objects conforming to `Storable` for a given key
     - parameter valueKey: The item's key
     - returns: [T]?

     - SeeAlso: `Storable`
     */
    public func get<T: Storable>(valueKey: String) -> [T]? {

        guard let dictionary = loadCache() as? Dictionary<String, AnyObject>,
            let result = dictionary[valueKey] as? Array<AnyObject> else {
                return nil
        }

        var unpackedItems = [T]()
        for case let item as Dictionary<String, AnyObject> in result {
            let warehouse = JSONWarehouse(context: item)
            if let item = T(warehouse: warehouse) {
                unpackedItems.append(item)
            }
        }

        return unpackedItems
    }
    
    func write(object: AnyObject, expires: StorageExpiry) {
        let cacheLocation = cacheFileURL()
        var storableDictionary = [String: AnyObject]()
        
        storableDictionary["expires"] = expires.toDate().timeIntervalSince1970
        storableDictionary["storage"] = object
        
        let _ = (storableDictionary as NSDictionary).writeToURL(cacheLocation, atomically: true)
    }
    
    func removeCache() {
        do {
            try NSFileManager.defaultManager().removeItemAtURL(cacheFileURL())
        } catch {
            print("error removing cache",error)
        }
    }
    
    func loadCache() -> AnyObject? {

        guard context == nil else {
            return context
        }

        let cacheLocation = cacheFileURL()
        
        if let metaDictionary = NSDictionary(contentsOfURL: cacheLocation),
            let cache = metaDictionary["storage"] {
                return cache
        }

        return nil
    }
    
    func cacheExists() -> Bool {

        guard NSFileManager.defaultManager().fileExistsAtPath(cacheFileURL().path!),
            let metaDictionary = NSDictionary(contentsOfURL: cacheFileURL()) else {
                return false
        }

        guard let expires = metaDictionary["expires"] as? NSTimeInterval else {
            // no expire time means old cache, never expires
            return true
        }

        let nowInterval = NSDate().timeIntervalSince1970
        
        if expires > nowInterval {
            return true
        } else {
            removeCache()
            return false
        }
    }
    
    func cacheFileURL() -> NSURL {
        let url = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first!
        
        let writeDirectory = url.URLByAppendingPathComponent("com.thatthinginswift.pantry")
        let cacheLocation = writeDirectory.URLByAppendingPathComponent(self.key)
        
        try! NSFileManager.defaultManager().createDirectoryAtURL(writeDirectory, withIntermediateDirectories: true, attributes: nil)
        
        return cacheLocation
    }
}

{'fraction_non_alphanumeric': 0.08897080801036167, 'fraction_numerical': 0.0035867291023214108, 'mean_word_length': 2.0914690483523253, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 41, 'https://': 0, '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': '1973198', 'n_tokens_mistral': 2786, 'n_tokens_neox': 2653, 'n_words': 763}