{"size": 2331, "ext": "js", "max_stars_repo_path": "poll/pollCtrl.js", "max_stars_repo_name": "rockiey/handson-frontend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "poll/pollCtrl.js", "max_issues_repo_name": "rockiey/handson-frontend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "poll/pollCtrl.js", "max_forks_repo_name": "rockiey/handson-frontend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.9315068493, "max_line_length": 76, "alphanum_fraction": 0.4834834835}
(function() {
    var app = angular.module('pollApp', []);

    var pollCtrlFun = function($scope, $http) {
        var dbUrl = 'https://api.mongolab.com/api/1/databases/rockie_test';
        var apiKey = 'Wq6LkbyEZjQJ5D-HY22jAH7XWM3T2M7z';
        var optionsUrl = dbUrl + '/collections/options?apiKey=' + apiKey;
        var votesUrl = dbUrl + '/collections/votes?apiKey=' + apiKey;
        var voteInterval = 10000; // 10s

        $scope.options = {};

        var onError = function(response) {
            console.log(response);
        }

        // get the client ip
        $http.get('http://freegeoip.net/json/').then(function(response) {
            $scope.ip = response.data.ip;
        }, function(response) {
            $scope.ip = "unspecified";
        })

        // get the options for the question
        var getOptions = function() {

            $http.get(optionsUrl).then(function(response) {
                var data = response.data
                for (var i in data) {
                    var option = data[i]
                    var id = option['option'];
                    $scope.options[id] = option
                }

                $scope.lastVoteTime = new Date() - 10000;

            }, onError);
        }
        

        // when click to vote one of the options
        $scope.vote = function(id) {
            var now = new Date();
            console.log('you vote ' + id + ' from ip ' + $scope.ip);

            // too frequent vote is ignored
            var elapsed = now - $scope.lastVoteTime;
            if (elapsed > voteInterval) {
                var data = {
                    'ip': $scope.ip,
                    'question': $scope.options[id].question,
                    'vote': id,
                    'time': new Date().toUTCString()
                }

                // only update the vote time, if data is successfully posted
                $http.post(votesUrl, data).then(function(response) {
                    console.log(response);
                    $scope.lastVoteTime = now
                    $scope.thanks = "Thanks for voting";
                })
            } else {
                console.log('ignore too frequent vote');
            }

        }

        getOptions();
    }

    app.controller('pollCtrl', ['$scope', '$http', pollCtrlFun]);
}());
