<commit_before>//
//  Networking.swift
//  Pods
//
//  Created by <NAME> on 8/23/15.
//
//

import Foundation
import Alamofire
import AlamofireImage
import SwiftyJSON

public typealias completion  = (response: ResponseHandler) -> Void

public class Networking : NSObject,Resolveable
{
    public static var entityName : String? = "Networking"
    
    public var done  : completion?
    public var error : completion?
    public var baseUrl : String?
    
    public var isConnected : Bool
    {
        return Reachability.isAvailable()
    }
    
    var manager : Alamofire.Manager
    
    public var headers : [String:String]
    
    var pendingRequest : Bool = false
    
    public var requestCap : Int      = 20
    public var currentRequests : Int = 0
    
    
    public required override init()
    {
        self.manager = Alamofire.Manager()
        headers = [String:String]()
    }
    
    public func setHeader(key:String,value:String)
    {
        headers[key] = value
    }
    
    public subscript(key : String) -> String
    {
        get{
            return self.headers[key] ?? ""
        }
        set(newValue)
        {
            self.headers[key] = newValue
        }
    }
    
    func fireAs(method: Alamofire.Method,url:String, type: RequestType? = nil, parameters : [String:String]? = nil, completion:(response: ResponseHandler) -> Void )
    {
        
        let requestType : RequestType = type ?? .ShouldSendUrlAndReturnJson

        // If the URL provided does not start with http or https
        // we want to use the previously set base url so that
        // we can build the url from the provided endpoint.
        var complete = baseUrl ?? ""
        if let _ = url.rangeOfString("^https?://", options: .RegularExpressionSearch) {
            complete = url
        } else {
            // Ensure we have a protocol
            if !complete.hasPrefix("https://") && !complete.hasPrefix("http://") {
                complete = "http://\(complete)"
            }
            
            // Ensure we don't get double slashes by stripping the last
            // in the base if there is already one provided with the
            // endpoint. This allows users to have slashes anyway.
            if complete.characters.last == "/" && url.characters.first == "/" {
                complete = "\(complete.substringToIndex(complete.endIndex.predecessor()))\(url)"
            }
        }

        var route = Route(method: method, baseUrl: complete, endpoint: nil, type: requestType)
        
        if let params = parameters<commit_msg>Vast improvements to allow much more cases<commit_after>//
//  Networking.swift
//  Pods
//
//  Created by <NAME> on 8/23/15.
//
//

import Foundation
import Alamofire
import AlamofireImage
import SwiftyJSON

public typealias completion  = (response: ResponseHandler) -> Void

public class Networking : NSObject,Resolveable
{
    public static var entityName : String? = "Networking"
    
    public var done  : completion?
    public var error : completion?
    public var baseUrl : String?
    
    public var isConnected : Bool
    {
        return Reachability.isAvailable()
    }
    
    var manager : Alamofire.Manager
    
    public var headers : [String:String]
    
    var pendingRequest : Bool = false
    
    public var requestCap : Int      = 20
    public var currentRequests : Int = 0
    
    
    public required override init()
    {
        self.manager = Alamofire.Manager()
        headers = [String:String]()
    }
    
    public func setHeader(key:String,value:String)
    {
        headers[key] = value
    }
    
    public subscript(key : String) -> String
    {
        get{
            return self.headers[key] ?? ""
        }
        set(newValue)
        {
            self.headers[key] = newValue
        }
    }
    
    func fireAs(method: Alamofire.Method,url:String, type: RequestType? = nil, parameters : [String:String]? = nil, completion:(response: ResponseHandler) -> Void )
    {
        
        let requestType : RequestType = type ?? .ShouldSendUrlAndReturnJson

        // If the URL provided does not start with http or https
        // we want to use the previously set base url so that
        // we can build the url from the provided endpoint.
        var complete = baseUrl ?? ""
        if let _ = url.rangeOfString("^https?://", options: .RegularExpressionSearch) {
            complete = url
        } else {
            // Ensure we have a protocol
            if !complete.hasPrefix("https://") && !complete.hasPrefix("http://") {
                complete = "http://\(complete)"
            }
            
            // Ensure we don't get double slashes by stripping the last
            // in the base if there is already one provided with the
            // endpoint. This allows users to have slashes anyway.
            if complete.characters.last == "/" {
                complete = complete.substringToIndex(complete.endIndex.predecessor())
            }
            
            var endpoint = url
            
            if endpoint.characters.first == "/" {
                endpoint = endpoint.substringFromIndex(endpoint.startIndex.successor())
            }
            
            complete = "\(complete)/\(endpoint)"
        }

        var route = Route(method: method, baseUrl: complete, endpoint: nil, type: requestType)
        
        if let params = parameters
{'fraction_non_alphanumeric': 0.09712495392554368, 'fraction_numerical': 0.002948765204570586, 'mean_word_length': 1.902139037433155, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '6424601', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1442, 'n_words': 435}