<commit_before>angular.module('pyro.service', ['firebase'])
// [TODO] Make this extensions of angularFire factories
// PyroArray Adds pyro to angular fire lists
.factory('PyroArrayFactory',function($FirebaseArray, pyro){
	return function(snap){
		return $FirebaseArray.$extendFactory({
			// Overide $createObject behavior to return pyro object
			// $$added:function(snap){
			// 	// [TODO] Add check for apps that are managed
			// 	if(snap.val().hasOwnProperty('dbUrl')){
			// 		var loadedObj = snap.val();
			// 		loadedObj.url = snap.val().dbUrl;

			// 		return loadedObj;
			// 	} else {
			// 		return snap.val();
			// 	}
			// },
			$$updated:function(snap){
				// var instance =this.$getRecord(snap.key())
				// return snap.getUserCount()
			}
		});
	}
})
.factory('PyroObjectFactory',function($FirebaseObject, pyro){
	return function(snap){
		return $FirebaseObject.$extendFactory({
			// Overide $createObject behavior to return pyro object
			$$added:function(snap){
				// [TODO] Add check for apps that are managed
				if(snap.val().hasOwnProperty('dbUrl')){
					var loadedObj = snap.val();
					loadedObj.url = snap.val().dbUrl;
					return pyro(loadedObj);
				} else {
					return snap.val();
				}
			}
		});
	}
})
.factory('PyroArray', function($firebase, PyroArrayFactory, pyroMaster){
	return function(list){
		// query for objects created by user
		var auth = pyroMaster.getAuth();
		console.log('pyroMaster:', pyroMaster);
		var query = pyroMaster.mainRef.child(list).orderByChild('author').equalTo(auth.uid);
		return $firebase(query, {arrayFactory:PyroArrayFactory()}).$asArray();
	}
})
.factory('PyroObject', function($firebase, PyroObjectFactory, pyroMaster){
	return function(list){
		// query for objects created by user
		var auth = pyroMaster.getAuth();
		console.log('pyroMaster:', pyroMaster);
		var query = pyroMaster.mainRef.child(list).orderByChild('author').equalTo(auth.uid);
		return $firebase(query, {arrayFactory:PyroObjectFactory()}).$asObject();
	}
})
.factory('pyro', ['$q', function($q ){
	return function (argPyroObj){
		var auth = null;
		var account = null;
		var pyro = new Pyro(argPyroObj);
			pyro.$signup =  function(argSignupData) {
				var deferred = $q.defer();
				console.log('[PyroService] $signup called', argSignupData);
				pyro.userSignup(argSignupData, function(userAccount) {
		      console.log('signup + login successful:', userAccount);
		      deferred.resolve(userAccount);
		    }, function(err) {
		      console.warn('pyroSignup returned:', err);
		      deferred.reject(err)
		    });
		    return deferred.promise;
			};
			pyro.$login =  function(argLoginData) {
				console.warn('[PyroService] $getUser called');
				var deferredLogin = $q.defer();
				pyro.login(argLoginData, function(returnedAccount){
					if(returnedAccount){
							account = returnedAccount;
							deferredLogin.resolve(account);
						} else {
							console.log('got null for returnedAccount:', returnedAccount);
							deferredLogin.reject();
						}
				});
				return deferredLogin.promise;
			};
			pyro.$getUser = function() {
				var deferred = $q.defer();
				console.warn('[PyroService] $getUser called');
				if(account != null) {
					deferred.resolve(account);
				}
				else {
					pyro.getUser(function(returnedAccount){
						if(returnedAccount){
							account = returnedAccount;
							deferred.resolve(account);
						} else {
							console.log('got null for returnedAccount:', returnedAccount);
							deferred.reject();
						}
					});
				}
				return deferred.promise;
			};
			pyro.$logout =  function() {
				var deferred = $q.defer();
				auth = null;
				account = null;
				pyro.logout(function(){
					deferred.resolve();
				});
				return deferred.promise;
			};
			pyro.$auth =  function(){
				var deferred = $q.defer();
				var auth = pyro.getAuth();
				if(auth != null){
					deferred.resolve(auth);
				} else {
					deferred.reject();
				}
				return deferred.promise;
			};
			pyro.$getUserCount =  function(){
				var deferred = $q.defer();
				pyro.getUserCount(function(userCount){
					deferred.resolve(userCount);
				});
				return deferred.promise;
			};
			pyro.$getListByAuthor =  function(argListName) {
				var deferredLoad = $q.defer();
				pyro.getListByAuthor(argListName, function(returnedList){
					deferredLoad.resolve(returnedList);
				});
				return deferredLoad.promise;
			};
			pyro.$createObject = function(argListName, argObject) {
				var deferredCreate = $q.defer();
				// [TODO] Do this correctly with the library
					pyro.createObject(argListName, argObject, function(newObject){
						deferredCreate.resolve(newObject);
					});
				return deferredCreate.promise;
			};

			pyro.$loadObject = function(argListName, argObjectId){
				var deferredLoad = $q.defer();
				pyro.loadObject(argListName, argObjectId, function(loadedObject){
					deferredLoad.resolve(loadedObject);
				});
				return deferredLoad.promise;
			};
			pyro.$deleteObject = function(argListName, argObjectId){
				pyro.deleteObject(argListName, argObjectId);
				console.log(argObjectId + ' was removed from the ' + argListName + ' list');
			};
			pyro.$getObjectCount = function(argListName) {
				var deferred = $q.defer();
				pyro.getObjectCount(argListName,function(count){
					deferred.resolve(count);
				});
				return deferred.promise;
			};
			return pyro;
	}
}])
.factory('pyroMaster', ['pyro', '$http', '$q', function(pyro, $http, $q) {
	var pyro = pyro({url:'http://pyro.firebaseio.com'});
	var pyroBase = new Firebase('http://pyro.firebaseio.com');
	var pyroServerUrl = "https://pyro-server.herokuapp.com/";
	// var pyroServerUrl = "localhost:4000/"
	var auth = pyro.getAuth();
	pyro.$generatePyro = function(argInstanceName){
		//request server for new instance. Create
		console.log('generatePyro called with:', argInstanceName);
		var deferred = $q.defer();
		if(argInstanceName) {
			//Instance name exists
			var newInstanceRef = pyroBase.child('instances').child(argInstanceName);
			if(auth) {
				//Auth exists
				var dbName = "pyro-"+ argInstanceName;
				var dbUrl = "https://"+dbName+".firebaseio.com"
				var instanceObj = {name: argInstanceName, bucketName:dbName, author:auth.uid, dbName:dbName, createdAt:Firebase.ServerValue.TIMESTAMP, dbUrl:dbUrl, url:dbUrl};
				var endpointLocation = pyroServerUrl + 'api/generate';
				var postObj = {name: argInstanceName, uid:auth.uid};
				$http.post(endpointLocation, postObj).success(function(data, status, headers){
					console.log('[$generatePyro] Call to :' + endpointLocation + ' returned:', data, status);
					var instanceData = data;
					delete instanceData.status;
					console.log('Setting new app data to Firebase:', instanceData);
					newInstanceRef.set(instanceObj, function(err){
						if(!err) {
							// resolve with newInstanceRef data
							console.log('new app generated successfully');
							deferred.resolve(newInstanceRef);
						} else {
							console.error('Error setting new pyro:', err);
							deferred.reject(err);
						}
					}); //---newInstanceRef.set()
				}).error(function(data, status, headers){
					var errorObj = {data:data, status:status, headers:headers}
					console.error('error creating new instance:', errorObj);
					deferred.reject(errorObj);
				});
			} else {
				var errObj = {message:'You must be logged in to create an instance'};
				deferred.reject(errObj);<commit_msg>Added writing of appUrl to firebase when app generation is complete.<commit_after>angular.module('pyro.service', ['firebase'])
// [TODO] Make this extensions of angularFire factories
// PyroArray Adds pyro to angular fire lists
.factory('PyroArrayFactory',function($FirebaseArray, pyro){
	return function(snap){
		return $FirebaseArray.$extendFactory({
			// Overide $createObject behavior to return pyro object
			// $$added:function(snap){
			// 	// [TODO] Add check for apps that are managed
			// 	if(snap.val().hasOwnProperty('dbUrl')){
			// 		var loadedObj = snap.val();
			// 		loadedObj.url = snap.val().dbUrl;

			// 		return loadedObj;
			// 	} else {
			// 		return snap.val();
			// 	}
			// },
			$$updated:function(snap){
				// var instance =this.$getRecord(snap.key())
				// return snap.getUserCount()
			}
		});
	}
})
.factory('PyroObjectFactory',function($FirebaseObject, pyro){
	return function(snap){
		return $FirebaseObject.$extendFactory({
			// Overide $createObject behavior to return pyro object
			$$added:function(snap){
				// [TODO] Add check for apps that are managed
				if(snap.val().hasOwnProperty('dbUrl')){
					var loadedObj = snap.val();
					loadedObj.url = snap.val().dbUrl;
					return pyro(loadedObj);
				} else {
					return snap.val();
				}
			}
		});
	}
})
.factory('PyroArray', function($firebase, PyroArrayFactory, pyroMaster){
	return function(list){
		// query for objects created by user
		var auth = pyroMaster.getAuth();
		console.log('pyroMaster:', pyroMaster);
		var query = pyroMaster.mainRef.child(list).orderByChild('author').equalTo(auth.uid);
		return $firebase(query, {arrayFactory:PyroArrayFactory()}).$asArray();
	}
})
.factory('PyroObject', function($firebase, PyroObjectFactory, pyroMaster){
	return function(list){
		// query for objects created by user
		var auth = pyroMaster.getAuth();
		console.log('pyroMaster:', pyroMaster);
		var query = pyroMaster.mainRef.child(list).orderByChild('author').equalTo(auth.uid);
		return $firebase(query, {arrayFactory:PyroObjectFactory()}).$asObject();
	}
})
.factory('pyro', ['$q', function($q ){
	return function (argPyroObj){
		var auth = null;
		var account = null;
		var pyro = new Pyro(argPyroObj);
			pyro.$signup =  function(argSignupData) {
				var deferred = $q.defer();
				console.log('[PyroService] $signup called', argSignupData);
				pyro.userSignup(argSignupData, function(userAccount) {
		      console.log('signup + login successful:', userAccount);
		      deferred.resolve(userAccount);
		    }, function(err) {
		      console.warn('pyroSignup returned:', err);
		      deferred.reject(err)
		    });
		    return deferred.promise;
			};
			pyro.$login =  function(argLoginData) {
				console.warn('[PyroService] $getUser called');
				var deferredLogin = $q.defer();
				pyro.login(argLoginData, function(returnedAccount){
					if(returnedAccount){
							account = returnedAccount;
							deferredLogin.resolve(account);
						} else {
							console.log('got null for returnedAccount:', returnedAccount);
							deferredLogin.reject();
						}
				});
				return deferredLogin.promise;
			};
			pyro.$getUser = function() {
				var deferred = $q.defer();
				console.warn('[PyroService] $getUser called');
				if(account != null) {
					deferred.resolve(account);
				}
				else {
					pyro.getUser(function(returnedAccount){
						if(returnedAccount){
							account = returnedAccount;
							deferred.resolve(account);
						} else {
							console.log('got null for returnedAccount:', returnedAccount);
							deferred.reject();
						}
					});
				}
				return deferred.promise;
			};
			pyro.$logout =  function() {
				var deferred = $q.defer();
				auth = null;
				account = null;
				pyro.logout(function(){
					deferred.resolve();
				});
				return deferred.promise;
			};
			pyro.$auth =  function(){
				var deferred = $q.defer();
				var auth = pyro.getAuth();
				if(auth != null){
					deferred.resolve(auth);
				} else {
					deferred.reject();
				}
				return deferred.promise;
			};
			pyro.$getUserCount =  function(){
				var deferred = $q.defer();
				pyro.getUserCount(function(userCount){
					deferred.resolve(userCount);
				});
				return deferred.promise;
			};
			pyro.$getListByAuthor =  function(argListName) {
				var deferredLoad = $q.defer();
				pyro.getListByAuthor(argListName, function(returnedList){
					deferredLoad.resolve(returnedList);
				});
				return deferredLoad.promise;
			};
			pyro.$createObject = function(argListName, argObject) {
				var deferredCreate = $q.defer();
				// [TODO] Do this correctly with the library
					pyro.createObject(argListName, argObject, function(newObject){
						deferredCreate.resolve(newObject);
					});
				return deferredCreate.promise;
			};

			pyro.$loadObject = function(argListName, argObjectId){
				var deferredLoad = $q.defer();
				pyro.loadObject(argListName, argObjectId, function(loadedObject){
					deferredLoad.resolve(loadedObject);
				});
				return deferredLoad.promise;
			};
			pyro.$deleteObject = function(argListName, argObjectId){
				pyro.deleteObject(argListName, argObjectId);
				console.log(argObjectId + ' was removed from the ' + argListName + ' list');
			};
			pyro.$getObjectCount = function(argListName) {
				var deferred = $q.defer();
				pyro.getObjectCount(argListName,function(count){
					deferred.resolve(count);
				});
				return deferred.promise;
			};
			return pyro;
	}
}])
.factory('pyroMaster', ['pyro', '$http', '$q', function(pyro, $http, $q) {
	var pyro = pyro({url:'http://pyro.firebaseio.com'});
	var pyroBase = new Firebase('http://pyro.firebaseio.com');
	var pyroServerUrl = "https://pyro-server.herokuapp.com/";
	// var pyroServerUrl = "localhost:4000/"
	var auth = pyro.getAuth();
	pyro.$generatePyro = function(argInstanceName){
		//request server for new instance. Create
		console.log('generatePyro called with:', argInstanceName);
		var deferred = $q.defer();
		if(argInstanceName) {
			//Instance name exists
			var newInstanceRef = pyroBase.child('instances').child(argInstanceName);
			if(auth) {
				//Auth exists
				var dbName = "pyro-"+ argInstanceName;
				var dbUrl = "https://"+dbName+".firebaseio.com"
				var appUrl = "pyro-"+ argInstanceName + ".s3-website-us-east-1.amazonaws.com"
				var instanceObj = {name: argInstanceName, bucketName:dbName, author:auth.uid, dbName:dbName, createdAt:Firebase.ServerValue.TIMESTAMP, dbUrl:dbUrl, url:dbUrl, appUrl:appUrl};
				var endpointLocation = pyroServerUrl + 'api/generate';
				var postObj = {name: argInstanceName, uid:auth.uid};
				$http.post(endpointLocation, postObj).success(function(data, status, headers){
					console.log('[$generatePyro] Call to :' + endpointLocation + ' returned:', data, status);
					var instanceData = data;
					delete instanceData.status;
					console.log('Setting new app data to Firebase:', instanceData);
					newInstanceRef.set(instanceObj, function(err){
						if(!err) {
							// resolve with newInstanceRef data
							console.log('new app generated successfully');
							deferred.resolve(newInstanceRef);
						} else {
							console.error('Error setting new pyro:', err);
							deferred.reject(err);
						}
					}); //---newInstanceRef.set()
				}).error(function(data, status, headers){
					var errorObj = {data:data, status:status, headers:headers}
					console.error('error creating new instance:', errorObj);
					deferred.reject(errorObj);
				});
			} else {
				var errObj = {message:'You must be logged in to create an instance'};
				deferred.reject(errObj);
{'fraction_non_alphanumeric': 0.14722778891115565, 'fraction_numerical': 0.0006680026720106881, 'mean_word_length': 3.531174334140436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '5430176', 'n_tokens_mistral': 5842, 'n_tokens_neox': 4642, 'n_words': 949}