Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed Jan 4, 2016
0 parents commit 6043ec6
Show file tree
Hide file tree
Showing 14 changed files with 363 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
lib
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v5
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
src
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v5
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- "0.12"
- "4"
- "5"
notifications:
email: false
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Leonardo Gatica

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# codigo-postal

[![npm version](https://img.shields.io/npm/v/codigo-postal.svg?style=flat-square)](https://www.npmjs.com/package/codigo-postal)
[![npm downloads](https://img.shields.io/npm/dm/codigo-postal.svg?style=flat-square)](https://www.npmjs.com/package/codigo-postal)
[![Build Status](https://img.shields.io/travis/lgaticaq/codigo-postal.svg?style=flat-square)](https://travis-ci.org/lgaticaq/codigo-postal)
[![dependency Status](https://img.shields.io/david/lgaticaq/codigo-postal.svg?style=flat-square)](https://david-dm.org/lgaticaq/codigo-postal#info=dependencies)
[![devDependency Status](https://img.shields.io/david/dev/lgaticaq/codigo-postal.svg?style=flat-square)](https://david-dm.org/lgaticaq/codigo-postal#info=devDependencies)
[![Join the chat at https://gitter.im/lgaticaq/codigo-postal](https://img.shields.io/badge/gitter-join%20chat%20%E2%86%92-brightgreen.svg?style=flat-square)](https://gitter.im/lgaticaq/codigo-postal?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Get postal code from Correos de Chile

## Installation

```bash
npm i -S codigo-postal
```

## Use

[Try on Tonic](https://tonicdev.com/npm/codigo-postal)
```js
const codigoPostal = require('codigo-postal');

const data = {
address: 'avenida siempreviva',
number: 742,
commune: 'springfield'
};

// Promise
codigoPostal(data)
.then(data => console.log(data))
.catch(err => console.error(err));

// Callback
codigoPostal(data, (err, data) => {
if (err) return console.error(err);
console.log(data);
});
```

Result:

```js
{
zip: XXXXX, // a number
address: XXXXX, // a string
number: XXXXX, // a string
commune: XXXXX, // a string
}
```
9 changes: 9 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const codigoPostal = require('codigo-postal');

const data = {
address: 'avenida siempreviva',
number: 742,
commune: 'springfield'
};

const result = await codigoPostal(data)
84 changes: 84 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"name": "codigo-postal",
"version": "1.0.0",
"description": "Get postal code from Correos de Chile",
"main": "lib",
"scripts": {
"prepublish": "npm run build -s",
"prebuild": "npm run lint -s && npm run clean -s",
"build": "babel src --out-dir lib --source-maps",
"lint": "eslint src",
"clean": "rimraf lib",
"pretest": "npm run build -s",
"test": "mocha --compilers js:babel-core/register"
},
"engines": {
"node": ">=0.12"
},
"repository": {
"type": "git",
"url": "https://github.com/lgaticaq/codigo-postal.git"
},
"keywords": [
"codigo postal",
"postal code",
"zip",
"chile"
],
"author": "Leonardo Gatica <lgatica@protonmail.com> (https://about.me/lgatica)",
"license": "MIT",
"bugs": {
"url": "https://github.com/lgaticaq/codigo-postal/issues"
},
"homepage": "https://github.com/lgaticaq/codigo-postal#readme",
"dependencies": {
"cheerio": "^0.19.0",
"q": "^1.4.1",
"request-promise": "^1.0.2"
},
"devDependencies": {
"babel-cli": "^6.3.17",
"babel-core": "^6.3.26",
"babel-preset-es2015": "^6.3.13",
"chai": "^3.4.1",
"eslint": "^1.10.3",
"mocha": "^2.3.4",
"nock": "^3.6.0",
"rimraf": "^2.5.0"
},
"eslintConfig": {
"rules": {
"indent": [
2,
2
],
"quotes": [
2,
"single"
],
"linebreak-style": [
2,
"unix"
],
"semi": [
2,
"always"
]
},
"ecmaFeatures": {
"modules": true
},
"env": {
"es6": true,
"node": true,
"mocha": true
},
"extends": "eslint:recommended"
},
"babel": {
"presets": [
"es2015"
]
},
"tonicExampleFilename": "example.js"
}
35 changes: 35 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

import Q from 'q';
import cheerio from 'cheerio';
import rp from 'request-promise';

const getZip = (data, cb) => {
const deferred = Q.defer();
const options = {
url: 'http://www.correos.cl/SitePages/codigo_postal/codigo_postal.aspx',
qs: {
calle: data.address,
numero: data.number,
comuna: data.commune
},
transform: (body) => cheerio.load(body)
};
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) deferred.reject(new Error('Not found'));
deferred.resolve({
zip: parseInt(zip, 10),
address: address,
number: number,
commune: commune
});
}).catch((err) => deferred.reject(err));
deferred.promise.nodeify(cb);
return deferred.promise;
};

module.exports = getZip;
3 changes: 3 additions & 0 deletions test/invalid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div id="ctl00_PlaceHolderMain_g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f_ctl00_pnlError">
<img src="/Style%20Library/Images/cp/error-codigo-postal.jpg" alt="error" border="0">
</div>
101 changes: 101 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
'use strict';

import path from 'path';

import {expect} from 'chai';
import nock from 'nock';

import lib from '../lib';

describe('codigo-postal', () => {


describe('valid', () => {

const data = {
address: 'avenida siempreviva',
number: 742,
commune: 'springfield'
};

beforeEach(() => {
const query = {
calle: data.address,
numero: data.number,
comuna: data.commune
};
nock.disableNetConnect();
nock('http://www.correos.cl')
.get('/SitePages/codigo_postal/codigo_postal.aspx')
.query(query)
.replyWithFile(200, path.join(__dirname, 'valid.html'));
});

it('should return a valid result (callback)', (done) => {
lib(data, (err, result) => {
expect(err).to.be.null;
expect(result).to.be.a('object');
expect(result.zip).to.be.a('number');
expect(result.address).to.be.a('string');
expect(result.number).to.be.a('string');
expect(result.commune).to.be.a('string');
done();
});
});


it('should return a valid result (promise)', (done) => {
lib(data).then((result) => {
expect(result).to.be.a('object');
expect(result.zip).to.be.a('number');
expect(result.address).to.be.a('string');
expect(result.number).to.be.a('string');
expect(result.commune).to.be.a('string');
done();
}).fail((err) => {
expect(err).to.be.null;
done();
});
});
});

describe('invalid', () => {

const data = {
address: 'avenida siemprebiba',
number: 666,
commune: 'springfield'
};

beforeEach(() => {
const query = {
calle: data.address,
numero: data.number,
comuna: data.commune
};
nock.disableNetConnect();
nock('http://www.correos.cl')
.get('/SitePages/codigo_postal/codigo_postal.aspx')
.query(query)
.replyWithFile(200, path.join(__dirname, 'invalid.html'));
});

it('should return a empty result (callback)', (done) => {
lib(data, (err, result) => {
expect(err).to.eql(new Error('Not found'));
expect(result).to.be.undefined;
done();
});
});

it('should return a empty result (promise)', (done) => {
lib(data).then((result) => {
expect(result).to.be.undefined;
done();
}).fail((err) => {
expect(err).to.eql(new Error('Not found'));
done();
});
});
});
});
7 changes: 7 additions & 0 deletions test/valid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>Tu <span class="span-rojo">Código Postal</span> es: <span class="tu_codigo"><span id="ctl00_PlaceHolderMain_g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f_ctl00_lblCodigoPostal">1111111</span></span></h1>
<div id="datos">
Calle : <span id="ctl00_PlaceHolderMain_g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f_ctl00_lblCalle" class="dato-info">AVENIDA SIEMPREVIVA</span><br>
Número : <span id="ctl00_PlaceHolderMain_g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f_ctl00_lblNumero" class="dato-info">742</span><br>
Comuna : <span id="ctl00_PlaceHolderMain_g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f_ctl00_lblComuna" class="dato-info">SPRINGFIELD</span>
</div>
<input type="hidden" name="ctl00$PlaceHolderMain$g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f$ctl00$HiddenField1" id="ctl00_PlaceHolderMain_g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f_ctl00_HiddenField1" value="avenida siempreviva 742;springfield">

0 comments on commit 6043ec6

Please sign in to comment.