<commit_before>//
//  FS20.swift
//  FS20
//
//  Created by <NAME> on 27.07.15.
//  Copyright (c) 2015 Mani. All rights reserved.
//

import Foundation

class FS20 {
    struct House {
        var id: Int
        var name: String
        var code1: Int
        var code2: Int
    }
    
    struct Device {
        var house: House
        var id: Int
        var name: String
        var code: Int
    }
    
    var host: String
    var sharedSession: NSURLSession
    
    init(host: String) {
        self.host = host
        self.sharedSession = NSURLSession.sharedSession()
    }
    
    func houses(completion: [House]? -> Void) {
        self.dataTask("/house") { data, response, error in
            if data == nil || error != nil {
                return completion(nil)
            }
            
            if let json = NSJSONSerialization.JSONObjectWithData(data!, options: .allZeros, error: nil) as? [[String: AnyObject]] {
                var houses: [House] = []
                for entry in json {
                    houses += [
                        House(
                            id: entry.cast("id", def: 0),
                            name: entry.cast("name", def: ""),
                            code1: entry.cast("house_code_1", def: 1111),
                            code2: entry.cast("house_code_2", def: 1111)
                        )
                    ]
                }
                
                return completion(houses)
            }
            
            return completion(nil)
        }
    }
    
    func devicesForHouse(house: House, completion: [Device]? -> Void) {
        self.dataTask("/house/\(house.id)/device") { data, response, error in
            if data == nil || error != nil {
                return completion(nil)
            }
            
            if let json = NSJSONSerialization.JSONObjectWithData(data!, options: .allZeros, error: nil) as? [[String: AnyObject]] {
                var devices: [Device] = []
                for entry in json {
                    devices += [
                        Device(
                            house: house,
                            id: entry.cast("id", def: 0),
                            name: entry.cast("name", def: ""),
                            code: entry.cast("code", def: 0)
                        )
                    ]
                }
                
                return completion(devices)
            }
            
            return completion(nil)
        }
    }
    
    private func dataTask(path: String, completion: (NSData?, NSURLResponse?, NSError?) -> Void) {
        self.sharedSession.dataTaskWithURL(NSURL(scheme: "http", host: self.host, path: path)!, completionHandler: completion).resume()
    }
}<commit_msg>Houses and devices can be fetched<commit_after>//
//  FS20.swift
//  FS20
//
//  Created by <NAME> on 27.07.15.
//  Copyright (c) 2015 Mani. All rights reserved.
//

import Foundation

class FS20 {
    struct House {
        var id: Int
        var name: String
        var code1: Int
        var code2: Int
        var devices: [Device]
        
        private init(fromJSON: [String: AnyObject]) {
            self.id = fromJSON.cast("id", def: 0)
            self.name = fromJSON.cast("name", def: "")
            self.code1 = fromJSON.cast("code1", def: 1111)
            self.code2 = fromJSON.cast("code2", def: 1111)
            
            self.devices = []
            for device in fromJSON.cast("devices", def: [[String: AnyObject]]()) {
                self.devices += [Device(fromJSON: device)]
            }
        }
    }
    
    struct Device {
        //var house: House
        var id: Int
        var name: String
        var code: Int
        
        private init(fromJSON: [String: AnyObject]) {
            self.id = fromJSON.cast("id", def: 0)
            self.name = fromJSON.cast("name", def: "")
            self.code = fromJSON.cast("code", def: 0)
        }
    }
    
    var host: String
    var sharedSession: NSURLSession
    
    init(host: String) {
        self.host = host
        self.sharedSession = NSURLSession.sharedSession()
    }
    
    func houses(completion: [House]? -> Void) {
        self.dataTask("/api") { data, response, error in
            if data == nil || error != nil {
                return completion(nil)
            }
            
            if let json = NSJSONSerialization.JSONObjectWithData(data!, options: .allZeros, error: nil) as? [[String: AnyObject]] {
                var houses: [House] = []
                for entry in json {
                    houses += [
                        House(fromJSON: entry)
                    ]
                }
                
                return completion(houses)
            }
            
            return completion(nil)
        }
    }
    
    private func dataTask(path: String, completion: (NSData?, NSURLResponse?, NSError?) -> Void) {
        self.sharedSession.dataTaskWithURL(NSURL(scheme: "http", host: self.host, path: path)!, completionHandler: completion).resume()
    }
}
{'fraction_non_alphanumeric': 0.11235513651541937, 'fraction_numerical': 0.01296405421331762, 'mean_word_length': 1.3240529438612505, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '3060297', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1358, 'n_words': 356}