Last active
August 29, 2015 14:01
-
-
Save zxqx/fb88eb464d7a85f667c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $ = require('jquery'); | |
var Q = require('q'); | |
var moment = require('moment'); | |
// iCal date parser | |
// -------------------------------------- | |
function ICalDateParser(d) | |
{ | |
this._date = d; | |
this.parsedDate = null; | |
} | |
ICalDateParser.prototype.parse = function() | |
{ | |
var date = this._date; | |
var year = date.substr(0, 4); | |
var month = parseInt(date.substr(4, 2), 10) -1; | |
var day = date.substr(6, 2); | |
var hour = date.substr(9, 2); | |
var minute = date.substr(11, 2); | |
var second = date.substr(13, 2); | |
this.parsedDate = new Date(year, month, day, hour, minute, second); | |
return this.parsedDate; | |
}; | |
// Schedule class | |
// -------------------------------------- | |
function Schedule() | |
{ | |
this._games = []; | |
this.games = []; | |
this.isLoaded = Q.defer(); | |
} | |
Schedule.SCHEDULE_URL = './schedule.json'; | |
Schedule.TEAM = 'Mud Dohgs'; | |
Schedule.prototype.fetchData = function(url) | |
{ | |
var _this = this; | |
$.get(url) | |
.then(function(resp) { | |
_this.isLoaded.resolve(resp); | |
}); | |
}; | |
Schedule.prototype.filterByTeam = function(team) | |
{ | |
var f = []; | |
var _this = this; | |
this._games.forEach(function(g) { | |
if (!~~g.teams.indexOf(team)) { | |
var game = $.extend(true, new Game(), g); | |
game.opponent = _this.parseOpponent(g, team); | |
f.push(game); | |
} | |
}); | |
this.games = f; | |
}; | |
Schedule.prototype.parseOpponent = function(game, team) | |
{ | |
var teamIndex = game.teams.indexOf(team); | |
var opponentIndex = (teamIndex) === 1 ? 0 : 1; | |
return game.teams[opponentIndex]; | |
}; | |
// Game class | |
// -------------------------------------- | |
function Game() | |
{ | |
} | |
// ScheduleParser class | |
// -------------------------------------- | |
function ScheduleParser(s) | |
{ | |
this._schedule = s; | |
this.games = []; | |
} | |
ScheduleParser.prototype.parse = function() | |
{ | |
var games = this._schedule.VCALENDAR[0].VEVENT; | |
var _this = this; | |
games.forEach(function(g) { | |
var game = new Game(); | |
game.date = moment(_this.parseDate(g)); | |
game.teams = _this.parseTeams(g); | |
game.park = _this.parsePark(g); | |
game.field = _this.parseField(g); | |
_this.games.push(game); | |
}); | |
return this; | |
}; | |
ScheduleParser.prototype.setupSchedule = function(s) { | |
s._games = this.games; | |
s.games = this.games; | |
}; | |
ScheduleParser.prototype.parseDate = function(g) | |
{ | |
return new ICalDateParser(g.DTSTART).parse(); | |
}; | |
ScheduleParser.prototype.parseTeams = function(g) | |
{ | |
return g.SUMMARY.split(' vs. '); | |
}; | |
ScheduleParser.prototype.parsePark = function(g) | |
{ | |
return g.DESCRIPTION.split(' - ')[0]; | |
}; | |
ScheduleParser.prototype.parseField = function(g) | |
{ | |
return parseInt(g.DESCRIPTION.split(' - ')[1], 10); | |
}; | |
// IMPLEMENTATION | |
// -------------------------------------- | |
var schedule = new Schedule(); | |
schedule.fetchData(Schedule.SCHEDULE_URL); | |
schedule.isLoaded.promise | |
.then(function(data) { | |
new ScheduleParser(data) | |
.parse() | |
.setupSchedule(schedule); | |
schedule.filterByTeam(Schedule.TEAM); | |
console.log(schedule); | |
}); | |
$(function() { | |
console.log('Dom ready'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment