<commit_before>//
//  UsersEndpoint.swift
//  PerfectServerTest
//
//  Created by <NAME> on 13/06/2017.
//
//

import Foundation
import PerfectHTTP
import PerfectLib

public class UsersEndpoint : NSObject {
    
    static let Name = "users"
    static let StatusName = "status"
    static let UsersFilename = "Resources/Users.plist"
    
    private enum UserKeys {
        static let PaddockKey = "paddock"
        static let PressKey = "press"
        
        static let LastModifiedKey = "last_modified"
    }
    
    public func userStatusEndpointHandler(data: [String:Any]) throws -> RequestHandler {
        return {
            request, response in
            
            response.status = .ok
            response.setHeader(.contentType, value: ServerContentType.JSON)
            var jsonResponse: [String:String] = [:]
            
            do {
                let usersFile = File(UsersEndpoint.UsersFilename)
                jsonResponse[UserKeys.LastModifiedKey] = String(usersFile.modificationTime)
                try response.setBody(json: jsonResponse)
            } catch {
                response.status = .internalServerError
                response.appendBody(string: "Error.")
            }
            
            response.completed()
        }
    }
    
    public func usersEndpointHandler(data: [String:Any]) throws -> RequestHandler {
        return {
            request, response in
            
            response.status = .ok
            response.setHeader(.contentType, value: ServerContentType.JSON)
            
            var jsonResponse: [String:Any] = [:]
            do {
                let usersFile = File(UsersEndpoint.UsersFilename)
                let data = try Data(contentsOf:URL(fileURLWithPath:usersFile.realPath))
                let usersDictionary = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [String:[String:Any]]
                
                for category in usersDictionary {
                    var jsonCategory: [String:[[String:String]]] = [:]
                    
                    let paddockMembers = category.value[UserKeys.PaddockKey] as! [[String:String]]
                    jsonCategory[UserKeys.PaddockKey] = paddockMembers
                    
                    let pressMembers = category.value[UserKeys.PressKey] as! [[String:String]]
                    jsonCategory[UserKeys.PressKey] = pressMembers
                    
                    jsonResponse[category.key] = jsonCategory
                }
                
                try response.setBody(json: jsonResponse)
            } catch {
                response.status = .internalServerError
                response.appendBody(string: "Error.")
            }
            response.completed()
        }
    }
}
<commit_msg>We now filter our response depending on a "language" parameter.<commit_after>//
//  UsersEndpoint.swift
//  PerfectServerTest
//
//  Created by <NAME> on 13/06/2017.
//
//

import Foundation
import PerfectHTTP
import PerfectLib

public class UsersEndpoint : NSObject {
    
    static let Name = "users"
    static let StatusName = "status"
    static let UsersFilename = "Resources/Users.plist"
    
    private enum Language : String {
        case English = "en"
        case Spanish = "es"
        case Mexican = "es-MX"
        
        static func isValidLanguage(_ language: String) -> Bool {
            if language == Language.English.rawValue {
                return true
            } else if language == Language.Spanish.rawValue {
                return true
            } else if language == Language.Mexican.rawValue {
                return true
            }
            
            return false
        }
    }
    
    private enum UserKeys {
        static let PaddockKey = "paddock"
        static let PressKey = "press"
        
        static let LastModifiedKey = "last_modified"
    }
    
    public func userStatusEndpointHandler(data: [String:Any]) throws -> RequestHandler {
        return {
            request, response in
            
            response.status = .ok
            response.setHeader(.contentType, value: ServerContentType.JSON)
            var jsonResponse: [String:String] = [:]
            
            do {
                let usersFile = File(UsersEndpoint.UsersFilename)
                jsonResponse[UserKeys.LastModifiedKey] = String(usersFile.modificationTime)
                try response.setBody(json: jsonResponse)
            } catch {
                response.status = .internalServerError
                response.appendBody(string: "Error.")
            }
            
            response.completed()
        }
    }
    
    public func usersEndpointHandler(data: [String:Any]) throws -> RequestHandler {
        return {
            request, response in
            
            response.status = .ok
            response.setHeader(.contentType, value: ServerContentType.JSON)
            
            var jsonResponse: [String:Any] = [:]
            do {
                let usersFile = File(UsersEndpoint.UsersFilename)
                let data = try Data(contentsOf:URL(fileURLWithPath:usersFile.realPath))
                let usersDictionary = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [String:[String:Any]]
                
                let language = self.requestParameterLanguage(request)
                for category in usersDictionary {
                    var jsonCategory: [String:[[String:String]]] = [:]
                    
                    let paddockMembers = self.filter(users: category.value[UserKeys.PaddockKey] as! [[String:String]], language: language)
                    jsonCategory[UserKeys.PaddockKey] = paddockMembers
                    
                    let pressMembers = self.filter(users: category.value[UserKeys.PressKey] as! [[String:String]], language: language)
                    jsonCategory[UserKeys.PressKey] = pressMembers
                    
                    jsonResponse[category.key] = jsonCategory
                }
                
                try response.setBody(json: jsonResponse)
            } catch {
                response.status = .internalServerError
                response.appendBody(string: "Error.")
            }
            response.completed()
        }
    }
    
    public func filter(users: [[String : String]], language : String) -> [[String : String]] {
        var filteredUsers = [[String : String]]()
        for user in users {
            var filteredUser = user
            for (userKey, _) in filteredUser {
                if Language.isValidLanguage(userKey) && language != userKey {
                    filteredUser.removeValue(forKey: userKey)
                }
            }
            filteredUsers.append(filteredUser)
        }
        
        return filteredUsers
    }
    
    public func requestParameterLanguage(_ request: HTTPRequest) -> String {
        for (parameter, value) in request.queryParams {
            if parameter == "language" && Language.isValidLanguage(value) {
                return value
            }
        }
        
        return "en"
    }
}

{'fraction_non_alphanumeric': 0.08567863579647858, 'fraction_numerical': 0.0022182171079994455, 'mean_word_length': 1.6223191566703017, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, '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': '7559203', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1697, 'n_words': 400}