Skip to content

Commit

Permalink
[bug fix] + reset hours array when not in columns mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanley committed Sep 27, 2011
1 parent 2cbc1c5 commit 075f353
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/pigeons.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ Pigeons.prototype.getTimetable = function(uri, cb) {
var doc = self.parseTimetable($);
doc.source = log.uri;

if(doc.tables == {}) { if(cb) cb(); return };
if(log.response_time) log.response_time.db = new Date();

self.put(doc, html, function(err, resp, body){
Expand Down Expand Up @@ -363,12 +364,14 @@ Pigeons.prototype.parseTimetable = function($) {
var d = -1, day, hour, hours = [];

$(opts.context).forEach(function(el){
// Day
if($.matchesSelector(el, opts.days)){
doc['tables'][el.textContent] = {};
were_minutes = false;
hours = []; hour = undefined;
cols = false;
d += 1;
// Hour
} else if($.matchesSelector(el, opts.hours)){
var days = Object.keys(doc['tables']);
var h = parseInt(el.textContent); // hour must be integer
Expand All @@ -385,8 +388,7 @@ Pigeons.prototype.parseTimetable = function($) {
// Example: http://www.zditm.szczecin.pl/rozklady/0___1007.11/0___1_2.htm
if(!day){
day = 'Undefined';
doc['tables'][day] = {};
}
doc['tables'][day] = {} }

if(were_minutes){
if(cols){ // At least two hour-nodes in a row
Expand All @@ -409,9 +411,11 @@ Pigeons.prototype.parseTimetable = function($) {
if(hours.length > 0) cols = true;
hours.unshift(h);
}
// Minutes
} else if($.matchesSelector(el, opts.minutes)){
if(hours.length)
hour = hours.pop();
if(!cols) hours = []

var minutes = (el.children.map ?
el.children.map(function(e){ return e.textContent }) :
Expand Down
18 changes: 17 additions & 1 deletion spec/ParserSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('parser', function(){
<div class='minute'>04</div> \
<div class='hour'>7</div> \
";

var body2 = " \
<div class='day'>first</div> \
<div class='hour'>4</div> \
Expand All @@ -138,13 +138,29 @@ describe('parser', function(){
<div class='hour'>8</div> \
";

var body3 = " \
<div class='day'>first</div> \
<div class='hour'>4</div> \
<div class='minute'>02</div> \
<div class='minute'>03</div> \
<div class='hour'>5</div> \
<div class='hour'>6</div> \
<div class='hour'>7</div> \
<div class='minute'>04</div> \
<div class='minute'>05</div> \
";

expect((new Pigeons(config)).parseTimetable(Sizzle(body1)).tables).toEqual({
first: {5: ['02','03'], 6:['04']}
});

expect((new Pigeons(config)).parseTimetable(Sizzle(body2)).tables).toEqual({
first: {6: ['02','03'], 7:['04']}
});

expect((new Pigeons(config)).parseTimetable(Sizzle(body3)).tables).toEqual({
first: {4: ['02','03'], 7:['04','05']}
});
});

it('should return to default values after each table', function(){
Expand Down

0 comments on commit 075f353

Please sign in to comment.