<commit_before>//
//  NTFNetwork.swift
//  Pods
//
//  Created by <NAME>, jr. on 17/03/16.
//
//

import UIKit
import Foundation

class NTFNetwork: NSObject {
	static let delegate = UIApplication.sharedApplication().delegate
	class var api: NTFNetwork {
		struct Static {
			static let instance: NTFNetwork = NTFNetwork()
		}
		return Static.instance
	}
	
	func updateNotificationToken(notificationToken: NSData?){
		addObservers()
		var parameters  = [
			NTFConstants.value.applicationToken : NTFDefaults.getApplicationToken(),
			NTFConstants.value.projectToken: NTFDefaults.getProjectToken()
			] as Dictionary<String, AnyObject>
		
		if notificationToken != nil {
			let characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
			let deviceTokenString: String = ( notificationToken!.description as NSString )
				.stringByTrimmingCharactersInSet( characterSet )
				.stringByReplacingOccurrencesOfString( " ", withString: "" ) as String
			
			NTFLOG_I(deviceTokenString)
			parameters.updateValue(deviceTokenString, forKey: "NotificationToken")
		}
		
		parameters += self.gatherDeviceInformation()
		self.sendRequest(.PUT, url: NTFConstants.api.router.register, parameters: parameters)
	}
	
	func registerDeviceForProject(projectToken: String, forApplication applicationToken: String){
		addObservers()

		var parameters  = [
			"ProjectToken": projectToken,
			"ApplicationToken": applicationToken,
			"NotificationToken" : "<PASSWORD>"
			] as Dictionary<String, AnyObject>
		
		parameters += self.gatherDeviceInformation()
		
		if let _ = NTFDefaults.getCommunicationToken() {
			self.sendRequest(.PUT, url: NTFConstants.api.router.register, parameters: parameters)
		}else{
			self.sendRequest(.POST, url: NTFConstants.api.router.register, parameters: parameters)
		}
	}
	
	private func addObservers(){
		NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NTFNetwork.didFinishLaunchingWithOptions), name: UIApplicationDidFinishLaunchingNotification, object: nil)
		NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NTFNetwork.applicationWillResignActive), name: UIApplicationWillResignActiveNotification, object: nil)
		NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NTFNetwork.applicationDidEnterBackground), name: UIApplicationDidEnterBackgroundNotification, object: nil)
		NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NTFNetwork.applicationWillEnterForeground), name: UIApplicationWillEnterForegroundNotification, object: nil)
		NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NTFNetwork.applicationDidBecomeActive), name: UIApplicationDidBecomeActiveNotification, object: nil)
	}
	
	
	func sendNotificationToken(notificationToken: String, projectToken: String, applicationToken: String){
		
		
		var parameters  = [
			"NotificationToken" : notificationToken,
			"ProjectToken": projectToken,
			"ApplicationToken": applicationToken
			] as Dictionary<String, AnyObject>
		parameters += self.gatherDeviceInformation()
		
		
		if let _ = NTFDefaults.getCommunicationToken() {
			self.sendRequest(.PUT, url: NTFConstants.api.router.register, parameters: parameters)
		}else{
			self.sendRequest(.POST, url: NTFConstants.api.router.register, parameters: parameters)
		}
	}
	
	
	func sendKeyValue(value: String, key: String){
		let parameters = [
			"Value" : value,
			"Key" : key
		]
		
		self.sendRequest(.POST, url: NTFConstants.api.router.keyValue, parameters: parameters)
	}
	
	func updateDeviceInformation(){
		var parameters  = [
			NTFConstants.value.applicationToken : NTFDefaults.getApplicationToken(),
			NTFConstants.value.projectToken: NTFDefaults.getProjectToken()
			] as Dictionary<String, AnyObject>
		parameters += self.gatherDeviceInformation()
		debugPrint(parameters)
		self.sendRequest(.PUT, url: NTFConstants.api.router.register, parameters: parameters)
	}
	
	func logApplicationState(state: NTFAppState){
		let parameters = [
			NTFConstants.api.applicationState : state.rawValue
		]
		sendRequest(.POST, url: NTFConstants.api.router.logState ,parameters: parameters)
		
	}
	
	
	private func gatherDeviceInformation() -> Dictionary<String,AnyObject> {
		let device = UIDevice.currentDevice()
		return [
			"DeviceName":device.name,
			"DeviceSystemName" : device.systemName,
			"DeviceSystemVersion" : device.systemVersion,
			"DeviceModel" : device.model,
			"DeviceLocalizedModel" : device.localizedModel,
			"DeviceIdentifierForVendor" : device.identifierForVendor?.UUIDString ?? "",
			"DeviceTimeZone" : String(NSTimeZone.localTimeZone()),
			"DevicePreferedLanguage" : NSLocale.preferredLanguages().first ?? "",
			"DeviceCFBundleDisplayName" : (NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleDisplayName") ?? "") as! String,
			"DeviceCFBundleShortVersionString" : (NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") ?? "") as! String,
			"DeviceCFBundleVersion" : (NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") ?? "") as! String,
			"DeviceBundleIdentifier" : NSBundle.mainBundle().bundleIdentifier ?? "",
			"DeviceDifferenceToGMT" : NSTimeZone.localTimeZone().secondsFromGMT / 3600
		]
	}
	
	private func sendRequest(method: NTFMethod, url: String, parameters: NSDictionary){
		do {
			debugPrint(parameters)
			let jsonData = try NSJSONSerialization.dataWithJSONObject(parameters, options: .PrettyPrinted)
			let url = NSURL(string: url)!
			let request = NSMutableURLRequest(URL: url)
			request.HTTPMethod = method.rawValue
			request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
			if (NTFDefaults.getCommunicationToken() != nil) {
				request.setValue(NTFDefaults.getCommunicationToken()!, forHTTPHeaderField: NTFConstants.value.communicationToken)
			}
			
			request.HTTPBody = jsonData
			
			let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
				if let httpResponse = response as? NSHTTPURLResponse {
					
					if let url = httpResponse.URL?.absoluteString {
						NTFLOG_I("Status: \(httpResponse.statusCode) URL: \(url)")
						if method == NTFMethod.PUT && httpResponse.statusCode != 200 {
							NTFDefaults.deleteCommunicationToken()
							self.registerDeviceForProject(NTFDefaults.getProjectToken(), forApplication: NTFDefaults.getApplicationToken())
						}
						if url == NTFConstants.api.router.register{
							do {
								if let result = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as? [String:AnyObject] {
									if let communicationToken = result["CommunicationToken"] as? String{
										NTFDefaults.setCommunicationToken(communicationToken)
									}
								}
								
							} catch (let error as NSError) {
								NTFLOG_F(" \(error)")
							}
						}
					}
					
				}
				if error != nil{
					print("Error -> \(error)")
					return
				}
				
				
			}
			
			task.resume()<commit_msg>FIX: Cocoapods<commit_after>//
//  NTFNetwork.swift
//  Pods
//
//  Created by <NAME>, jr. on 17/03/16.
//
//

import UIKit
import Foundation

class NTFNetwork: NSObject {
	static let delegate = UIApplication.sharedApplication().delegate
	class var api: NTFNetwork {
		struct Static {
			static let instance: NTFNetwork = NTFNetwork()
		}
		return Static.instance
	}
	
	func updateNotificationToken(notificationToken: NSData?){
		addObservers()
		var parameters  = [
			NTFConstants.value.applicationToken : NTFDefaults.getApplicationToken(),
			NTFConstants.value.projectToken: NTFDefaults.getProjectToken()
			] as Dictionary<String, AnyObject>
		
		if notificationToken != nil {
			let characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
			let deviceTokenString: String = ( notificationToken!.description as NSString )
				.stringByTrimmingCharactersInSet( characterSet )
				.stringByReplacingOccurrencesOfString( " ", withString: "" ) as String
			
			NTFLOG_I(deviceTokenString)
			parameters.updateValue(deviceTokenString, forKey: "NotificationToken")
		}
		
		parameters += self.gatherDeviceInformation()
		self.sendRequest(.PUT, url: NTFConstants.api.router.register, parameters: parameters)
	}
	
	func registerDeviceForProject(projectToken: String, forApplication applicationToken: String){
		addObservers()

		var parameters  = [
			"ProjectToken": projectToken,
			"ApplicationToken": applicationToken,
			"NotificationToken" : "<PASSWORD>"
			] as Dictionary<String, AnyObject>
		
		parameters += self.gatherDeviceInformation()
		
		if let _ = NTFDefaults.getCommunicationToken() {
			self.sendRequest(.PUT, url: NTFConstants.api.router.register, parameters: parameters)
		}else{
			self.sendRequest(.POST, url: NTFConstants.api.router.register, parameters: parameters)
		}
	}
	
	private func addObservers(){
		NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NTFNetwork.didFinishLaunchingWithOptions), name: UIApplicationDidFinishLaunchingNotification, object: nil)
		NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NTFNetwork.applicationWillResignActive), name: UIApplicationWillResignActiveNotification, object: nil)
		NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NTFNetwork.applicationDidEnterBackground), name: UIApplicationDidEnterBackgroundNotification, object: nil)
		NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NTFNetwork.applicationWillEnterForeground), name: UIApplicationWillEnterForegroundNotification, object: nil)
		NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NTFNetwork.applicationDidBecomeActive), name: UIApplicationDidBecomeActiveNotification, object: nil)
	}
	
	
	func sendNotificationToken(notificationToken: String, projectToken: String, applicationToken: String){
		
		
		var parameters  = [
			"NotificationToken" : notificationToken,
			"ProjectToken": projectToken,
			"ApplicationToken": applicationToken
			] as Dictionary<String, AnyObject>
		parameters += self.gatherDeviceInformation()
		
		
		if let _ = NTFDefaults.getCommunicationToken() {
			self.sendRequest(.PUT, url: NTFConstants.api.router.register, parameters: parameters)
		}else{
			self.sendRequest(.POST, url: NTFConstants.api.router.register, parameters: parameters)
		}
	}
	
	
	func sendKeyValue(value: String, key: String){
		let parameters = [
			"Value" : value,
			"Key" : key
		]
		
		self.sendRequest(.POST, url: NTFConstants.api.router.keyValue, parameters: parameters)
	}
	
	func updateDeviceInformation(){
		var parameters  = [
			NTFConstants.value.applicationToken : NTFDefaults.getApplicationToken(),
			NTFConstants.value.projectToken: NTFDefaults.getProjectToken()
			] as Dictionary<String, AnyObject>
		parameters += self.gatherDeviceInformation()
		debugPrint(parameters)
		self.sendRequest(.PUT, url: NTFConstants.api.router.register, parameters: parameters)
	}
	
	func logApplicationState(state: NTFAppState){
		let parameters = [
			NTFConstants.api.applicationState : state.rawValue
		]
		sendRequest(.POST, url: NTFConstants.api.router.logState ,parameters: parameters)
		
	}
	
	
	private func gatherDeviceInformation() -> Dictionary<String,AnyObject> {
		let device = UIDevice.currentDevice()
		return [
			"DeviceName":device.name,
			"DeviceSystemName" : device.systemName,
			"DeviceSystemVersion" : device.systemVersion,
			"DeviceModel" : device.model,
			"DeviceLocalizedModel" : device.localizedModel,
			"DeviceIdentifierForVendor" : device.identifierForVendor?.UUIDString ?? "",
			"DeviceTimeZone" : String(NSTimeZone.localTimeZone()),
			"DevicePreferedLanguage" : NSLocale.preferredLanguages().first ?? "",
			"DeviceCFBundleDisplayName" : (NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleDisplayName") ?? "") as! String,
			"DeviceCFBundleShortVersionString" : (NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") ?? "") as! String,
			"DeviceCFBundleVersion" : (NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") ?? "") as! String,
			"DeviceBundleIdentifier" : NSBundle.mainBundle().bundleIdentifier ?? "",
			"DeviceDifferenceToGMT" : NSTimeZone.localTimeZone().secondsFromGMT / 3600
		]
	}
	
	private func sendRequest(method: NTFMethod, url: String, parameters: NSDictionary){
		do {
			debugPrint(parameters)
			let jsonData = try NSJSONSerialization.dataWithJSONObject(parameters, options: .PrettyPrinted)
			let url = NSURL(string: url)!
			let request = NSMutableURLRequest(URL: url)
			request.HTTPMethod = method.rawValue
			request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
			if (NTFDefaults.getCommunicationToken() != nil) {
				request.setValue(NTFDefaults.getCommunicationToken()!, forHTTPHeaderField: NTFConstants.value.communicationToken)
			}
			
			request.HTTPBody = jsonData
			
			let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
				if let httpResponse = response as? NSHTTPURLResponse {
					
					if let url = httpResponse.URL?.absoluteString {
						NTFLOG_I("Status: \(httpResponse.statusCode) URL: \(url)")
						if method == NTFMethod.PUT && httpResponse.statusCode != 200 {
							NTFDefaults.deleteCommunicationToken()
							self.registerDeviceForProject(NTFDefaults.getProjectToken(), forApplication: NTFDefaults.getApplicationToken())
						}
						if url == NTFConstants.api.router.register{
							do {
								if let result = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as? [String:AnyObject] {
									if let communicationToken = result["CommunicationToken"] as? String{
										NTFDefaults.setCommunicationToken(communicationToken)
									}
								}
							} catch (let error as NSError) {
								NTFLOG_F(" \(error)")
							}
						}
					}
					
				}
				if error != nil{
					print("Error -> \(error)")
					return
				}
				
				
			}
			
			task.resume()
{'fraction_non_alphanumeric': 0.10842600513259196, 'fraction_numerical': 0.001996007984031936, 'mean_word_length': 4.877251780477587, 'pattern_counts': {'":': 10, '<': 19, '<?xml version=': 0, '>': 23, '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': '6584565', 'n_tokens_mistral': 4722, 'n_tokens_neox': 3908, 'n_words': 744}