Skip to content

Commit

Permalink
Refactor and add coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed May 15, 2016
1 parent ae17c4e commit d27aebd
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 96 deletions.
1 change: 0 additions & 1 deletion .node-version

This file was deleted.

5 changes: 4 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
node_modules
src
test
coverage
.*
LICENSE
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v5
v6
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
language: node_js
node_js:
- "0.12"
- "4"
- "5"
- "6"
notifications:
email: false
after_success:
- npm run coveralls
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Leonardo Gatica
Copyright (c) 2016 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
Expand Down
12 changes: 2 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![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)
[![Coverage Status](https://img.shields.io/coveralls/lgaticaq/codigo-postal/master.svg?style=flat-square)](https://coveralls.io/github/lgaticaq/codigo-postal?branch=master)
[![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)
Expand All @@ -27,16 +28,7 @@ const data = {
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);
});
codigoPostal(data).then(console.log);
```

Result:
Expand Down
57 changes: 21 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
"name": "codigo-postal",
"version": "1.0.1",
"description": "Get postal code from Correos de Chile",
"main": "lib",
"main": "src",
"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"
"pretest": "eslint src",
"test": "istanbul cover _mocha",
"coveralls": "coveralls < coverage/lcov.info"
},
"engines": {
"node": ">=0.12"
"node": ">=4"
},
"repository": {
"type": "git",
Expand All @@ -33,52 +29,41 @@
"homepage": "https://github.com/lgaticaq/codigo-postal#readme",
"dependencies": {
"cheerio": "^0.20.0",
"q": "^1.4.1",
"request-promise": "^3.0.0"
},
"devDependencies": {
"babel-cli": "^6.4.0",
"babel-core": "^6.4.0",
"babel-preset-es2015": "^6.3.13",
"chai": "^3.4.1",
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"eslint": "^2.10.1",
"mocha": "^2.3.4",
"nock": "^8.0.0",
"rimraf": "^2.5.0"
"istanbul": "^0.4.3",
"mocha": "^2.4.5",
"nock": "^8.0.0"
},
"eslintConfig": {
"env": {
"es6": true,
"node": true,
"mocha": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [
2,
2
],
"quotes": [
2,
"single"
],
"linebreak-style": [
2,
"unix"
],
"quotes": [
2,
"single"
],
"semi": [
2,
"always"
]
},
"ecmaFeatures": {
"modules": true
},
"env": {
"es6": true,
"node": true,
"mocha": true
},
"extends": "eslint:recommended"
},
"babel": {
"presets": [
"es2015"
]
}
},
"tonicExampleFilename": "example.js"
}
22 changes: 9 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
'use strict';

import Q from 'q';
import cheerio from 'cheerio';
import rp from 'request-promise';
const cheerio = require('cheerio');
const rp = require('request-promise');

const getZip = (data, cb) => {
const deferred = Q.defer();
const getZip = data => {
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)
transform: cheerio.load
};
rp(options).then(($) => {
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) deferred.reject(new Error('Not found'));
deferred.resolve({
if (!zip) throw new Error('Not found');
return {
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;
42 changes: 10 additions & 32 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict';

import path from 'path';
const path = require('path');

import {expect} from 'chai';
import nock from 'nock';
const expect = require('chai').expect;
const nock = require('nock');

import lib from '../lib';
const lib = require('../src');

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


describe('valid', () => {

const data = {
Expand All @@ -31,28 +30,15 @@ describe('codigo-postal', () => {
.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) => {
it('should return a valid result', 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) => {
}).catch(err => {
expect(err).to.be.null;
done();
});
Expand Down Expand Up @@ -80,19 +66,11 @@ describe('codigo-postal', () => {
.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) => {
it('should return a empty result', done => {
lib(data).then(result => {
expect(result).to.be.undefined;
done();
}).fail((err) => {
}).catch(err => {
expect(err).to.eql(new Error('Not found'));
done();
});
Expand Down

0 comments on commit d27aebd

Please sign in to comment.