-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5990f0
commit 5780625
Showing
6 changed files
with
145 additions
and
74 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,6 @@ | |
console.log('Magic happens on port: ' + app.get('port')); | ||
}); | ||
|
||
module.exports = app; | ||
|
||
})(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
(function (){ | ||
var cheerio = require('cheerio'); | ||
|
||
var Helper = { | ||
json: [], | ||
data: null, | ||
$ : null, | ||
dams : {} | ||
}; | ||
|
||
Helper.getDamName = function (str) { | ||
var name = str.split(/[./]+/)[1]; | ||
return Helper.dams[name]; | ||
}; | ||
|
||
Helper.buildJSON = function (cssSelector, key) { | ||
Helper.data.find(cssSelector).each(function (i, elem) { | ||
Helper.json[i].data.push({ | ||
key : key, | ||
value : Helper.$(elem).next().text() | ||
}); | ||
}); | ||
}; | ||
|
||
Helper.parserHTML = function (html) { | ||
Helper.$ = cheerio.load(html); | ||
|
||
Helper.$('#tabDados').filter(function () { | ||
Helper.data = Helper.$(this); | ||
|
||
// Fetch each images on context | ||
Helper.data.find('img').each(function (i, elem) { | ||
Helper.json[i] = { | ||
name : Helper.getDamName(elem.attribs.src), | ||
data : [] | ||
}; | ||
}); | ||
|
||
// Fetch each td with content "volume armazenado" | ||
Helper.buildJSON('td:contains(volume armazenado)', 'volume armazenado'); | ||
|
||
// Fetch each td with content "pluviometria do dia" | ||
Helper.buildJSON('td:contains(pluviometria do dia)', 'pluviometria do dia'); | ||
|
||
// Fetch each td with content "pluviometria acumulada no mês" | ||
Helper.buildJSON('td:contains(pluviometria acumulada no mês)', 'pluviometria acumulada no mês'); | ||
|
||
// Fetch each td with content "média histórica do mês" | ||
Helper.buildJSON('td:contains(média histórica do mês)', 'média histórica do mês'); | ||
}); | ||
|
||
return Helper.json; | ||
}; | ||
|
||
Helper.buildData = function(date, token) { | ||
date = date.split('-'); | ||
|
||
return { | ||
"__VIEWSTATE": token.state, | ||
"__EVENTVALIDATION": token.validation, | ||
"Imagebutton1.x": 8, | ||
"Imagebutton1.y": 6, | ||
"cmbDia": parseInt(date[2], 10), | ||
"cmbMes": parseInt(date[1], 10), | ||
"cmbAno": parseInt(date[0], 10) | ||
}; | ||
}; | ||
|
||
module.exports = Helper; | ||
})(); |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
var expect = require('chai').expect, | ||
Helper = require('../lib/Helper'); | ||
|
||
describe('Helper', function () { | ||
it('Translate image name from water system name', function(done) { | ||
var odd = Helper.dams; | ||
Helper.dams = { | ||
'dummySystem' : 'Dummy' | ||
}; | ||
|
||
expect(Helper.getDamName('imagens/dummySystem.jpg')).to.be.equal('Dummy'); | ||
|
||
Helper.dams = odd; | ||
done(); | ||
}); | ||
|
||
it("Expect the data be the same of mock", function(done) { | ||
var token = { | ||
state : 'xpto', | ||
validation : 'lorem' | ||
}, | ||
mock = { | ||
"__VIEWSTATE": token.state, | ||
"__EVENTVALIDATION": token.validation, | ||
"Imagebutton1.x": 8, | ||
"Imagebutton1.y": 6, | ||
"cmbDia": 1, | ||
"cmbMes": 1, | ||
"cmbAno": 2003 | ||
}; | ||
|
||
expect(Helper.buildData('2003-01-01', token)).to.eql(mock); | ||
done(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var request = require('supertest'), | ||
app = require('../index'), | ||
expect = require('chai').expect; | ||
|
||
describe('Sabesp', function () { | ||
it('returns today data', function(done) { | ||
request(app) | ||
.get('/') | ||
.set('Accept', 'application/json') | ||
.expect(200) | ||
.end(function(err, res) { | ||
expect(res.body.length).to.be.equal(6); | ||
expect(res.body[0].name).to.be.equal('Cantareira'); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); |