diff --git a/app.coffee b/app.coffee index 0897108..6e2cdba 100644 --- a/app.coffee +++ b/app.coffee @@ -28,5 +28,13 @@ compileMethod = (str, path) -> app.get "/", (req, res) -> res.render "index", title: "Twitter Raffle", subtitle: "By Como Rich Web" +app.get '/disqualified', (req, res) -> + # load disqualified users from yaml file + fs = require 'fs' + yaml = require 'yaml' + fs.readFile 'config/settings.yml', (err, data) -> + contents = data.toString('utf-8') + res.json(yaml.eval(contents)) + app.listen 3000 console.log "Express server listening on port %d in %s mode", app.address().port, app.settings.env diff --git a/config/settings.yml b/config/settings.yml new file mode 100644 index 0000000..e99898b --- /dev/null +++ b/config/settings.yml @@ -0,0 +1 @@ +disqualified: ['CoMORichWeb'] \ No newline at end of file diff --git a/package.json b/package.json index 5562cb8..86a555d 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "express": "2.4.3", "jade": ">= 0.0.1", "coffee-script": ">=1.1.1", - "stylus": "*" + "stylus": "*", + "yaml": "0.2.2" }, "devDependencies": {}, "engines": { diff --git a/public/javascripts/application.js b/public/javascripts/application.js index f310b80..2733da4 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -8,7 +8,7 @@ return child; }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; $(function() { - var Tweet, TweetView, Tweets, TweetsView; + var DisqualifiedUser, DisqualifiedUsers, Tweet, TweetView, Tweets, TweetsView; Tweet = (function() { __extends(Tweet, Backbone.Model); function Tweet() { @@ -16,6 +16,37 @@ } return Tweet; })(); + DisqualifiedUser = (function() { + __extends(DisqualifiedUser, Backbone.Model); + function DisqualifiedUser() { + DisqualifiedUser.__super__.constructor.apply(this, arguments); + } + return DisqualifiedUser; + })(); + DisqualifiedUsers = (function() { + __extends(DisqualifiedUsers, Backbone.Collection); + function DisqualifiedUsers() { + DisqualifiedUsers.__super__.constructor.apply(this, arguments); + } + DisqualifiedUsers.prototype.model = DisqualifiedUser; + DisqualifiedUsers.prototype.initialize = function() { + return this.url = "http://localhost:3000/disqualified"; + }; + DisqualifiedUsers.prototype.parse = function(response) { + var name, results, _i, _len, _ref, _results; + results = response.disqualified; + _ref = response.disqualified; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + name = _ref[_i]; + _results.push({ + user: name + }); + } + return _results; + }; + return DisqualifiedUsers; + })(); Tweets = (function() { __extends(Tweets, Backbone.Collection); function Tweets() { @@ -46,6 +77,15 @@ Tweets.prototype.randomUpTo = function(ceiling) { return Math.floor(Math.random() * ceiling); }; + Tweets.prototype.disqualify = function(usernames) { + var raw_names; + raw_names = usernames.map(__bind(function(item) { + return item.get('user'); + }, this)); + return this.reset(this.reject(__bind(function(tweet) { + return $.inArray(tweet.get('from_user'), raw_names) > -1; + }, this))); + }; return Tweets; })(); TweetView = (function() { @@ -87,6 +127,7 @@ }; TweetsView.prototype.initialize = function() { this.tweets = new Tweets(); + this.banlist = new DisqualifiedUsers(); this.tweets.bind('reset', __bind(function(tweets) { $("#tweets").empty(); return tweets.each(__bind(function(tweet) { @@ -95,12 +136,16 @@ }).render().el); }, this)); }, this)); - return this.getTweets(); + this.getTweets(); + return this.getBanlist(); }; TweetsView.prototype.getTweets = function() { this.tweets.setURL(this.searchInput.val()); return this.tweets.fetch(); }; + TweetsView.prototype.getBanlist = function() { + return this.banlist.fetch(); + }; TweetsView.prototype.hilightRandom = function() { if (this.tweet) { this.tweet.view.deactive(); @@ -117,6 +162,7 @@ return this.$('#winner').text(tweet.get('from_user')); }; TweetsView.prototype.pickRandom = function() { + this.tweets.disqualify(this.banlist); this.interval = setInterval(this.hilightRandom, this.RANDOM_SELECTION_INTERVAL); return setTimeout(this.selectWinner, this.SELECTION_LENGTH); }; diff --git a/views/javascripts/application.coffee b/views/javascripts/application.coffee index 98723fe..f44a529 100644 --- a/views/javascripts/application.coffee +++ b/views/javascripts/application.coffee @@ -1,5 +1,14 @@ $ -> class Tweet extends Backbone.Model + class DisqualifiedUser extends Backbone.Model + + class DisqualifiedUsers extends Backbone.Collection + model: DisqualifiedUser + initialize: -> + @url = "http://localhost:3000/disqualified" + parse: (response)-> + results = response.disqualified + {user: name} for name in response.disqualified class Tweets extends Backbone.Collection model: Tweet @@ -21,6 +30,12 @@ $ -> randomUpTo: (ceiling) -> Math.floor(Math.random()*ceiling) + + disqualify: (usernames) -> + raw_names = usernames.map (item) => + item.get('user') + @reset @reject (tweet) => + $.inArray(tweet.get('from_user'), raw_names) > -1 class TweetView extends Backbone.View tagName:'li' @@ -45,14 +60,18 @@ $ -> 'change #search':'getTweets' initialize: -> @tweets = new Tweets(); + @banlist = new DisqualifiedUsers(); @tweets.bind 'reset', (tweets) => $("#tweets").empty() tweets.each (tweet) => @render(new TweetView(model:tweet).render().el) @getTweets() + @getBanlist() getTweets: -> @tweets.setURL(@searchInput.val()) @tweets.fetch() + getBanlist: -> + @banlist.fetch() hilightRandom: => if(@tweet) @@ -67,6 +86,7 @@ $ -> @$('#winner').text tweet.get('from_user') pickRandom: -> + @tweets.disqualify(@banlist) @interval = setInterval @hilightRandom, @RANDOM_SELECTION_INTERVAL setTimeout @selectWinner, @SELECTION_LENGTH