<commit_before>//
//  router.swift
//  TaylorTest
//
//  Created by <NAME> on 19/06/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

public class Router {
    
    private var routes: [Route] = [Route]()
    
    public func addRoute(route: Route) -> Bool {
        
        routes.append(route)
        return true
    }
    
    public func handler() -> Handler {
        
        return {
            
            request, response -> Callback in
            
            if let route = self.detectRouteForRequest(request){
                
                let handlerExecutor = HandlerExecutor(handlers: route.handlers)
                
                var didContinue = false
                handlerExecutor.onContinueWithNoHandlersLeft = { req, res -> Callback? in
                    didContinue = true
                    return nil
                }
                
                let (req, res) = handlerExecutor.execute(request, response)
                
                if didContinue {
                    return .Continue(req, res)
                } else {
                    return .Send(req, res)
                }
                
                // on .Continue (in there), if run out of handlers
                // return .Continue (in here)
                
                
                // on .Send (in there)
                // return .Send (in here)
                
//                callbackHandler.onContinueWithNoHandlersLeft = { req, res, _ in
//                    // outer callback (kind of confusing)
//                    callback(.Continue(req, res))
//                }
//                callbackHandler.onSend = { req, res, _ in
//                    // outer callback (kind of confusing)
//                    callback(.Send(req, res))
//                }
//                
//                callbackHandler.dispatch(request, response)
//                callbackHandler.start(request, response)

                
//                handlerExecutor.onContinueWithNoHandlersLeft = { req, res -> Callback? in
//                    
//                }
                
//                let (req, res) = handlerExecutor.execute(request, response)
                
                
            } else {
                return .Continue(request, response)
            }
        }
    }
    
    private func detectRouteForRequest(request: Request) -> Route? {
        
        for route in routes {
            
            request.parameters = Dictionary<String, String>()

            let compatibleMethods = (route.method == request.method) || (route.method == .GET && request.method == .HEAD)

            let componentCount = route.pathComponents.count<commit_msg>Cleanup<commit_after>//
//  router.swift
//  TaylorTest
//
//  Created by <NAME> on 19/06/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

public class Router {
    
    private var routes: [Route] = [Route]()
    
    public func addRoute(route: Route) -> Bool {
        
        routes.append(route)
        return true
    }
    
    public func handler() -> Handler {
        
        return {
            
            request, response -> Callback in
            
            if let route = self.detectRouteForRequest(request){
                
                let handlerExecutor = HandlerExecutor(handlers: route.handlers)
                
                var didContinue = false
                handlerExecutor.onContinueWithNoHandlersLeft = { req, res -> Callback? in
                    didContinue = true
                    return nil
                }
                
                let (req, res) = handlerExecutor.execute(request, response)
                
                if didContinue {
                    return .Continue(req, res)
                } else {
                    return .Send(req, res)
                }
                
            } else {
                return .Continue(request, response)
            }
        }
    }
    
    private func detectRouteForRequest(request: Request) -> Route? {
        
        for route in routes {
            
            request.parameters = Dictionary<String, String>()

            let compatibleMethods = (route.method == request.method) || (route.method == .GET && request.method == .HEAD)

            let componentCount = route.pathComponents.count
{'fraction_non_alphanumeric': 0.09103416647557899, 'fraction_numerical': 0.004586104104563174, 'mean_word_length': 1.1278048780487804, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 20, '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': '1025953', 'n_tokens_mistral': 1089, 'n_tokens_neox': 997, 'n_words': 258}