<commit_before>angular.module('bitcoincalc', ['calc', 'app']);

angular.module('calc', []).factory('apiService', function($http) {
	var api = {};
	//api.url = "http://blockchain.info/ticker?format=json&callback=JSON_CALLBACK";
	//api.url = "http://bitcoincharts.com/t/markets.json";
	api.url = "ticker.json";
	api.getRates = function(callback, error) {
		console.log('get rates');
		$http.get(api.url).success(callback).error(error);
	};

	return api;
});

angular.module('app', []).controller('uiController', function($scope, apiService) {
	$scope.rates = [];
	$scope.bitcoinAmount = 1;
	$scope.cur = null;
	$scope.curAmount = 0;
	$scope.init = function(param) {
		console.log('app init');
		$scope.refreshRates();
	};

	$scope.refreshRates = function() {
		console.log('refresh rates');
		apiService.getRates(function(rates) {
			for(var i=0;i<Object.keys(rates).length;i++) {
				var rate = {};
				rate.name = Object.keys(rates)[i];
				rate.last = rates[Object.keys(rates)[i]].last;
				rate.symbol = rates[Object.keys(rates)[i]].symbol;
				$scope.rates.push(rate);
			}
			if($scope.cur == null)
				$scope.cur = $scope.rates[0];
			$scope.btcAmountChanged();
		},
		function(data){
			console.log("error");
			console.log(data);
		});
	};

	$scope.convertToCurrency = function(amount) {
		$scope.bitcoinAmount = parseFloat($scope.bitcoinAmount);
		if($scope.bitcoinAmount >= 0 && typeof($scope.bitcoinAmount) == "number" && !isNaN($scope.bitcoinAmount) && $scope.bitcoinAmount != "")
			return amount * $scope.bitcoinAmount;
		return 0;
	};

	$scope.convertToBitcoin = function(amount) {
		$scope.curAmount = parseFloat($scope.curAmount);
		if($scope.curAmount >= 0 && typeof($scope.curAmount) == "number" && !isNaN($scope.curAmount) && $scope.bitcoinAmount != "")
			return amount / $scope.curAmount;
		return 0;
	};

	$scope.setCur = function(cur) {
		$scope.cur = cur.rate;
		$scope.curAmountChanged();
	};

	$scope.btcAmountChanged = function() {
		$scope.curAmount = $scope.convertToCurrency($scope.cur.last);
	};
<commit_msg>Api module now broadcasts new currency rates to controller in every 15 mins, instead of using callbacks.<commit_after>angular.module('bitcoincalc', ['calc', 'app']);

angular.module('calc', []).factory('apiService', function($rootScope, $http) {
	var api = {};
	api.url = "ticker.json";
	api.refresh = 900000; // 15mins

	api.refreshRates = function() {
		$http.get(api.url).success(function(rates) {
			$rootScope.$broadcast('api.rates', rates);
		});
	};

	setInterval(function() {
		api.refreshRates();
	}, api.refresh);

	api.refreshRates();

	return api;
});

angular.module('app', []).controller('uiController', function($scope, apiService) {
	$scope.rates = [];
	$scope.bitcoinAmount = 1;
	$scope.cur = null;
	$scope.curAmount = 0;

	$scope.$on('api.rates', function(e, rates) {
		for(var i=0;i<Object.keys(rates).length;i++) {
			var rate = {};
			rate.name = Object.keys(rates)[i];
			rate.last = rates[Object.keys(rates)[i]].last;
			rate.symbol = rates[Object.keys(rates)[i]].symbol;
			$scope.rates.push(rate);
		}
		if($scope.cur == null)
			$scope.cur = $scope.rates[0];
		$scope.btcAmountChanged();
	});

	$scope.convertToCurrency = function(amount) {
		$scope.bitcoinAmount = parseFloat($scope.bitcoinAmount);
		if($scope.bitcoinAmount >= 0 && typeof($scope.bitcoinAmount) == "number" && !isNaN($scope.bitcoinAmount) && $scope.bitcoinAmount != "")
			return amount * $scope.bitcoinAmount;
		return 0;
	};

	$scope.convertToBitcoin = function(amount) {
		$scope.curAmount = parseFloat($scope.curAmount);
		if($scope.curAmount >= 0 && typeof($scope.curAmount) == "number" && !isNaN($scope.curAmount) && $scope.bitcoinAmount != "")
			return amount / $scope.curAmount;
		return 0;
	};

	$scope.setCur = function(cur) {
		$scope.cur = cur.rate;
		$scope.curAmountChanged();
	};

	$scope.btcAmountChanged = function() {
		$scope.curAmount = $scope.convertToCurrency($scope.cur.last);
	};

{'fraction_non_alphanumeric': 0.19781615033011682, 'fraction_numerical': 0.006602336211274758, 'mean_word_length': 4.627142857142857, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '2522361', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1371, 'n_words': 211}