<commit_before>//
//  HitboxChatAPI.swift
//  GamingStreamsTVApp
//
//  Created by <NAME> on 10/17/15.
//  Copyright © 2015 Rivus Media Inc. All rights reserved.
//

import UIKit
import Alamofire

class HitboxChatAPI {
    
    static func getChatServers(completionHandler: (serverURLS: [String]?, error: ServiceError?) -> ()) {
        let urlString = "https://www.hitbox.tv/api/chat/servers"
        Alamofire.request(.GET, urlString).responseJSON { response in
            if response.result.isSuccess {
                if let baseArray = response.result.value as? [[String : String]] {
                    var urls = [String]()
                    for dict in baseArray {
                        if let url = dict["server_ip"] {
                            urls.append(url)
                        }
                    }
                    completionHandler(serverURLS: urls, error: nil)
                    return
                }
                completionHandler(serverURLS: nil, error: .JSONError)
            } else {
                completionHandler(serverURLS: nil, error: .URLError)
            }
        }
    }
    
    static func compileWebSocket(serverURL: String, completionHandler: (result: String?, error: ServiceError?) -> ()) {
        Alamofire.request(.GET, "http://\(serverURL)/socket.io/1/").responseString { response in
            if response.result.isSuccess {
                if let responseSocket = response.result.value {
                    if let colonRange = responseSocket.rangeOfString(":") {
                        let connectionID = responseSocket.substringToIndex(colonRange.startIndex)
                        let compiledString = "ws://\(serverURL)/socket.io/1/websocket/\(connectionID)"
                        completionHandler(result: compiledString, error: nil)
                        return
                    }
                }
            }
            completionHandler(result: nil, error: .URLError)
        }
    }
    
    static func getFirstAvailableWebSocket(completionHandler : (result: String?, error: ServiceError?) -> ()) {
        HitboxChatAPI.getChatServers(){ serverURLs, error in
            guard error == nil else {
                completionHandler(result: nil, error: error!)
                return
            }
            
            guard let serverURLs = serverURLs where serverURLs.count > 0 else {
                completionHandler(result: nil, error: .OtherError("Invalid url array"))
                return
            }
            
            HitboxChatAPI.compileWebSocket(serverURLs[0]){ socketURL, error in
                guard error == nil else {
                    completionHandler(result: nil, error: error!)
                    return
                }
                
                guard let socketURL = socketURL else {
                    completionHandler(result: nil, error: .OtherError("Invalid socket URL"))
                    return
                }
                
                completionHandler(result: socketURL, error: nil)
            }
        }
    }

}<commit_msg>Logging on hitbox chat api<commit_after>//
//  HitboxChatAPI.swift
//  GamingStreamsTVApp
//
//  Created by <NAME> on 10/17/15.
//  Copyright © 2015 Rivus Media Inc. All rights reserved.
//

import UIKit
import Alamofire

class HitboxChatAPI {
    
    static func getChatServers(completionHandler: (serverURLS: [String]?, error: ServiceError?) -> ()) {
        let urlString = "https://www.hitbox.tv/api/chat/servers"
        Alamofire.request(.GET, urlString).responseJSON { response in
            if response.result.isSuccess {
                if let baseArray = response.result.value as? [[String : String]] {
                    var urls = [String]()
                    for dict in baseArray {
                        if let url = dict["server_ip"] {
                            urls.append(url)
                        }
                    }
                    Logger.Debug("Returned \(urls.count) results")
                    completionHandler(serverURLS: urls, error: nil)
                    return
                }
                Logger.Error("Could not parse server list as JSON")
                completionHandler(serverURLS: nil, error: .JSONError)
            }
            Logger.Error("Could not request chat server list")
            completionHandler(serverURLS: nil, error: .URLError)
            
        }
    }
    
    static func compileWebSocket(serverURL: String, completionHandler: (result: String?, error: ServiceError?) -> ()) {
        Alamofire.request(.GET, "http://\(serverURL)/socket.io/1/").responseString { response in
            if response.result.isSuccess {
                if let responseSocket = response.result.value {
                    if let colonRange = responseSocket.rangeOfString(":") {
                        let connectionID = responseSocket.substringToIndex(colonRange.startIndex)
                        let compiledString = "ws://\(serverURL)/socket.io/1/websocket/\(connectionID)"
                        Logger.Debug("Compiled websocket url: \(compiledString)")
                        completionHandler(result: compiledString, error: nil)
                        return
                    }
                }
            }
            Logger.Error("Could not request a websocket URL")
            completionHandler(result: nil, error: .URLError)
        }
    }
    
    static func getFirstAvailableWebSocket(completionHandler : (result: String?, error: ServiceError?) -> ()) {
        HitboxChatAPI.getChatServers(){ serverURLs, error in
            guard error == nil else {
                completionHandler(result: nil, error: error!)
                return
            }
            
            guard let serverURLs = serverURLs where serverURLs.count > 0 else {
                Logger.Error("Invalid server list")
                completionHandler(result: nil, error: .OtherError("Invalid url array"))
                return
            }
            
            HitboxChatAPI.compileWebSocket(serverURLs[0]){ socketURL, error in
                guard error == nil else {
                    completionHandler(result: nil, error: error!)
                    return
                }
                
                guard let socketURL = socketURL else {
                    Logger.Error("Invalid socket URL");
                    completionHandler(result: nil, error: .OtherError("Invalid socket URL"))
                    return
                }
                Logger.Debug("Returning websocket url: \(socketURL)")
                completionHandler(result: socketURL, error: nil)
            }
        }
    }

}
{'fraction_non_alphanumeric': 0.09383418138281951, 'fraction_numerical': 0.004190362167015863, 'mean_word_length': 1.510518407212622, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1994730', 'n_tokens_mistral': 1694, 'n_tokens_neox': 1585, 'n_words': 387}