Skip to content

Commit

Permalink
chore(package): Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed Dec 18, 2016
1 parent 0d7b906 commit 0ff3390
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
23 changes: 14 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
"description": "Get postal code from Correos de Chile",
"main": "src",
"scripts": {
"pretest": "eslint src",
"pretest": "eslint .",
"test": "istanbul cover _mocha",
"coveralls": "coveralls < coverage/lcov.info"
"coveralls": "coveralls < coverage/lcov.info",
"codeclimate": "codeclimate-test-reporter < coverage/lcov.info",
"release:major": "changelog -M && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version major && git push origin && git push origin --tags && npm publish",
"release:minor": "changelog -m && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version minor && git push origin && git push origin --tags && npm publish",
"release:patch": "changelog -p && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version patch && git push origin && git push origin --tags && npm publish"
},
"engines": {
"node": ">=4"
Expand All @@ -28,16 +32,17 @@
},
"homepage": "https://github.com/lgaticaq/codigo-postal#readme",
"dependencies": {
"cheerio": "^0.22.0",
"request-promise": "^4.0.0"
"cheerio": "^0.22.0"
},
"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"eslint": "^3.0.0",
"istanbul": "^0.4.3",
"mocha": "^3.0.0",
"nock": "^9.0.0"
"codeclimate-test-reporter": "^0.4.0",
"coveralls": "^2.11.15",
"eslint": "^3.12.2",
"generate-changelog": "^1.1.0",
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"nock": "^9.0.2"
},
"eslintConfig": {
"env": {
Expand Down
53 changes: 34 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
'use strict';

const cheerio = require('cheerio');
const rp = require('request-promise');
const http = require('http');
const querystring = require('querystring');

const getZip = data => {
const options = {
url: 'http://www.correos.cl/SitePages/codigo_postal/codigo_postal.aspx',
qs: {
return new Promise((resolve, reject) => {
const qs = querystring.stringify({
calle: data.address,
numero: data.number,
comuna: data.commune
},
transform: cheerio.load
};
return rp(options).then($ => {
const zip = $('span[id$="CodigoPostal"]').text();
const address = $('span[id$="Calle"]').text();
const number = $('span[id$="Numero"]').text();
const commune = $('span[id$="Comuna"]').text();
if (!zip) throw new Error('Not found');
return {
zip: parseInt(zip, 10),
address: address,
number: number,
commune: commune
};
});
const url = `http://www.correos.cl/SitePages/codigo_postal/codigo_postal.aspx?${qs}`;
http.get(url, res => {
if (res.statusCode !== 200) {
res.resume();
reject(new Error(`Request Failed. Status Code: ${res.statusCode}`));
} else {
res.setEncoding('utf8');
let rawData = '';
res.on('data', chunk => rawData += chunk);
res.on('end', () => {
try {
const $ = cheerio.load(rawData, {decodeEntities: false});
const zip = $('span[id$="CodigoPostal"]').text();
const address = $('span[id$="Calle"]').text();
const number = $('span[id$="Numero"]').text();
const commune = $('span[id$="Comuna"]').text();
if (!zip) throw new Error('Not found');
resolve({
zip: parseInt(zip, 10),
address: address,
number: number,
commune: commune
});
} catch (err) {
reject(err);
}
});
}
}).on('error', err => reject(err));
});
};

Expand Down

0 comments on commit 0ff3390

Please sign in to comment.