<commit_before>//
//  WeatherApi.swift
//  Weather
//
//  Created by <NAME> on 29.03.15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit

enum WeatherApiUnits {
    case Internal
    case Metric
    case Imperial
}

class WeatherApiCommunicator: NSObject {
    
    let manager = AFHTTPRequestOperationManager()
    let baseUrl = "http://api.openweathermap.org/data"
    let apiVersion = "2.5"
    let apiId: String
    
    var language: String?
    
    var units: WeatherApiUnits = .Internal
    
    var cache: [String: (date: NSDate, json: JSON)] = [:]
    var cacheExpirationInterval: NSTimeInterval = 10 * 60
    
    init(apiId: String) {
        self.apiId = apiId
        super.init()
    }
    
    var langUrlString: String {
        return language != nil ? "&lang=\(language)" : ""
    }
    
    var unitsUrlString: String {
        switch self.units {
        case .Metric:
            return "&units=metric"
        case .Imperial:
            return "&units=imperial"
        default:
            return ""
        }
    }
    
    var apiIdUrlString: String {
        return "&APPIID=\(apiId)"
    }
    
    func sendRequest(request: String, callback: (JSON?, NSError?) -> Void) {
        let url = "\(baseUrl)/\(apiVersion)/\(request)\(langUrlString)\(unitsUrlString)\(apiIdUrlString)"
        let asciiData = url.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)
        let nsString = NSString(data: asciiData!, encoding: NSASCIIStringEncoding);
        let escapedUrl = nsString!.stringByAddingPercentEscapesUsingEncoding(NSASCIIStringEncoding)!
        
        if let cachedResult = self.cache[url] {
            if cachedResult.date.timeIntervalSinceNow > -self.cacheExpirationInterval {
                callback(cachedResult.json, nil)
                return
            }
        }
        
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        println(url)
        
        manager.GET(escapedUrl, parameters: nil,
            success: {(operation: AFHTTPRequestOperation!, response: AnyObject?) in
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                println(response?.description)
                
                if let response: AnyObject = response {
                    let json = JSON(response)<commit_msg>commented debug logging of network communication<commit_after>//
//  WeatherApi.swift
//  Weather
//
//  Created by <NAME> on 29.03.15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit

enum WeatherApiUnits {
    case Internal
    case Metric
    case Imperial
}

class WeatherApiCommunicator: NSObject {
    
    let manager = AFHTTPRequestOperationManager()
    let baseUrl = "http://api.openweathermap.org/data"
    let apiVersion = "2.5"
    let apiId: String
    
    var language: String?
    
    var units: WeatherApiUnits = .Internal
    
    var cache: [String: (date: NSDate, json: JSON)] = [:]
    var cacheExpirationInterval: NSTimeInterval = 10 * 60
    
    init(apiId: String) {
        self.apiId = apiId
        super.init()
    }
    
    var langUrlString: String {
        return language != nil ? "&lang=\(language)" : ""
    }
    
    var unitsUrlString: String {
        switch self.units {
        case .Metric:
            return "&units=metric"
        case .Imperial:
            return "&units=imperial"
        default:
            return ""
        }
    }
    
    var apiIdUrlString: String {
        return "&APPIID=\(apiId)"
    }
    
    func sendRequest(request: String, callback: (JSON?, NSError?) -> Void) {
        let url = "\(baseUrl)/\(apiVersion)/\(request)\(langUrlString)\(unitsUrlString)\(apiIdUrlString)"
        let asciiData = url.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)
        let nsString = NSString(data: asciiData!, encoding: NSASCIIStringEncoding);
        let escapedUrl = nsString!.stringByAddingPercentEscapesUsingEncoding(NSASCIIStringEncoding)!
        
        if let cachedResult = self.cache[url] {
            if cachedResult.date.timeIntervalSinceNow > -self.cacheExpirationInterval {
                callback(cachedResult.json, nil)
                return
            }
        }
        
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        //println(url)
        
        manager.GET(escapedUrl, parameters: nil,
            success: {(operation: AFHTTPRequestOperation!, response: AnyObject?) in
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                //println(response?.description)
                
                if let response: AnyObject = response {
                    let json = JSON(response)
{'fraction_non_alphanumeric': 0.0999790838736666, 'fraction_numerical': 0.006693160426688978, 'mean_word_length': 2.1837549933422102, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '7571480', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1307, 'n_words': 300}