<commit_before>//
//  ChatClient.swift
//  OraChat
//
//  Created by <NAME> on 8/22/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Alamofire

private let endpoint = "https://private-d9e5b-oracodechallenge.apiary-mock.com/chats"

class ChatClient {
    class func getList(search search: String? = nil, success: ([Chat])->()) {
        var params = ["page": "1", "limit": "20"]
        if search != nil { params["q"] = search }

        Alamofire.request(.GET, endpoint, parameters: params)
            .responseJSON(completionHandler: { response in
                print(response.request)  // original URL request
                print(response.response) // URL response
                print(response.data)     // server data
                print(response.result)   // result of response serialization

                if let JSON = response.result.value {
                    print("JSON: \(JSON)")
                    success(chatsFromJSON(JSON))
                }
            })
    }

    class func createChat(name name: String, success: (Chat)->()) {
        Alamofire.request(.POST, endpoint, parameters: ["name": name], encoding: .JSON)
            .responseJSON(completionHandler: { response in
                print(response.request)  // original URL request
                print(response.response) // URL response
                print(response.data)     // server data
                print(response.result)   // result of response serialization

                if let JSON = response.result.value {
                    print("JSON: \(JSON)")
                    if let chatObject = chatFromJSON(JSON) {
                        success(chatObject)
                    }
                }
            })
    }

    private class func chatsFromJSON(json: AnyObject) -> [Chat] {
        if let data = json["data"] as? [AnyObject] {
            let chats = data.map({(chatData: AnyObject) -> Chat? in

                if let chatID = chatData["id"] as? Int,
                    let name = chatData["name"] as? String,
                    let author = chatData["user"]??["name"] as? String,
                    let participant = chatData["last_message"]??["user"]??["name"] as? String,
                    let creationString = chatData["created"] as? String,
                    let lastMessage = chatData["last_message"]??["message"] as? String {

                    return Chat(chatID: chatID,
                        name: name,
                        author: author,
                        creationString: creationString,
                        participant: participant,
                        lastMessage: lastMessage)
                }
                return nil
            })
            return chats.filter({$0 != nil}).map({$0!})
        }
        return [Chat]()
    }

    private class func chatFromJSON(json: AnyObject) -> Chat? {
        if let dict = json as? [String: AnyObject],
            let data = dict["data"],
            let chatID = data["id"] as? Int,
            let name = data["name"] as? String,
            let author = data["user"]??["name"] as? String,
            let creationString = data["created"] as? String {

            return Chat(chatID: chatID,
                        name: name,
                        author: author,
                        creationString: creationString)
        }
        return nil
    }
}
<commit_msg>add commented code to simulate search<commit_after>//
//  ChatClient.swift
//  OraChat
//
//  Created by <NAME> on 8/22/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Alamofire

private let endpoint = "https://private-d9e5b-oracodechallenge.apiary-mock.com/chats"

class ChatClient {
    class func getList(search search: String? = nil, success: ([Chat])->()) {
        var params = ["page": "1", "limit": "20"]
        if search != nil { params["q"] = search }

        Alamofire.request(.GET, endpoint, parameters: params)
            .responseJSON(completionHandler: { response in
                print(response.request)  // original URL request
                print(response.response) // URL response
                print(response.data)     // server data
                print(response.result)   // result of response serialization

                if let JSON = response.result.value {
                    print("JSON: \(JSON)")
                    success(chatsFromJSON(JSON))

                    //to simulate a smaller array in case of search,
                    //comment out the above line and uncomment the lines below

//                    let foo = chatsFromJSON(JSON)
//                    if search == nil {
//                        success([foo[0], foo[1], foo[0], foo[1]])
//                    } else {
//                        success(foo)
//                    }
                }
            })
    }

    class func createChat(name name: String, success: (Chat)->()) {
        Alamofire.request(.POST, endpoint, parameters: ["name": name], encoding: .JSON)
            .responseJSON(completionHandler: { response in
                print(response.request)  // original URL request
                print(response.response) // URL response
                print(response.data)     // server data
                print(response.result)   // result of response serialization

                if let JSON = response.result.value {
                    print("JSON: \(JSON)")
                    if let chatObject = chatFromJSON(JSON) {
                        success(chatObject)
                    }
                }
            })
    }

    private class func chatsFromJSON(json: AnyObject) -> [Chat] {
        if let data = json["data"] as? [AnyObject] {
            let chats = data.map({(chatData: AnyObject) -> Chat? in

                if let chatID = chatData["id"] as? Int,
                    let name = chatData["name"] as? String,
                    let author = chatData["user"]??["name"] as? String,
                    let participant = chatData["last_message"]??["user"]??["name"] as? String,
                    let creationString = chatData["created"] as? String,
                    let lastMessage = chatData["last_message"]??["message"] as? String {

                    return Chat(chatID: chatID,
                        name: name,
                        author: author,
                        creationString: creationString,
                        participant: participant,
                        lastMessage: lastMessage)
                }
                return nil
            })
            return chats.filter({$0 != nil}).map({$0!})
        }
        return [Chat]()
    }

    private class func chatFromJSON(json: AnyObject) -> Chat? {
        if let dict = json as? [String: AnyObject],
            let data = dict["data"],
            let chatID = data["id"] as? Int,
            let name = data["name"] as? String,
            let author = data["user"]??["name"] as? String,
            let creationString = data["created"] as? String {

            return Chat(chatID: chatID,
                        name: name,
                        author: author,
                        creationString: creationString)
        }
        return nil
    }
}

{'fraction_non_alphanumeric': 0.11648473124222744, 'fraction_numerical': 0.0049744369213762605, 'mean_word_length': 1.4779185210544334, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 17, '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': '6792651', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1763, 'n_words': 496}