From 8ea959ceba8dc7a126144076475fbaf04adc992f Mon Sep 17 00:00:00 2001 From: Leonardo Gatica Date: Sun, 3 Jan 2016 20:10:14 -0300 Subject: [PATCH] first commit --- .editorconfig | 12 +++++++ .gitignore | 28 ++++++++++++++++ .node-version | 1 + .npmignore | 2 ++ .nvmrc | 1 + .travis.yml | 7 ++++ LICENSE | 22 +++++++++++++ README.md | 45 +++++++++++++++++++++++++ example.js | 5 +++ package.json | 83 +++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 25 ++++++++++++++ test/invalid.json | 10 ++++++ test/test.js | 77 +++++++++++++++++++++++++++++++++++++++++++ test/valid.json | 12 +++++++ 14 files changed, 330 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .node-version create mode 100644 .npmignore create mode 100644 .nvmrc create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 example.js create mode 100644 package.json create mode 100644 src/index.js create mode 100644 test/invalid.json create mode 100644 test/test.js create mode 100644 test/valid.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4a7ea30 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..215000b --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.node-version b/.node-version new file mode 100644 index 0000000..47e5d40 --- /dev/null +++ b/.node-version @@ -0,0 +1 @@ +v5 diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..cd3ca40 --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +node_modules +src diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..47e5d40 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +v5 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..22cacd0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: node_js +node_js: + - "0.12" + - "4" + - "5" +notifications: + email: false diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b5fea6b --- /dev/null +++ b/LICENSE @@ -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. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..3fcff52 --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +# bip + +[![npm version](https://img.shields.io/npm/v/bip.svg?style=flat-square)](https://www.npmjs.com/package/bip) +[![npm downloads](https://img.shields.io/npm/dm/bip.svg?style=flat-square)](https://www.npmjs.com/package/bip) +[![Build Status](https://img.shields.io/travis/lgaticaq/bip.svg?style=flat-square)](https://travis-ci.org/lgaticaq/bip) +[![dependency Status](https://img.shields.io/david/lgaticaq/bip.svg?style=flat-square)](https://david-dm.org/lgaticaq/bip#info=dependencies) +[![devDependency Status](https://img.shields.io/david/dev/lgaticaq/bip.svg?style=flat-square)](https://david-dm.org/lgaticaq/bip#info=devDependencies) +[![Join the chat at https://gitter.im/lgaticaq/bip](https://img.shields.io/badge/gitter-join%20chat%20%E2%86%92-brightgreen.svg?style=flat-square)](https://gitter.im/lgaticaq/bip?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +Check rut status in registro civil + +## Installation + +```bash +npm i -S bip +``` + +## Use + +[Try on Tonic](https://tonicdev.com/npm/bip) +```js +const bip = require('bip'); + +const number = 11111111; + +// Promise +bip(number) + .then(data => console.log(data)) + .fail(err => console.error(err)); + +// Callback +bip(number, (err, data) => { + if (err) return console.error(err); + console.log(data); +}); +``` + +Result: + +```js +{ + balance: XXXXX, // a number + date: XXXXX, // a date +} +``` diff --git a/example.js b/example.js new file mode 100644 index 0000000..2d09286 --- /dev/null +++ b/example.js @@ -0,0 +1,5 @@ +const bip = require('bip'); + +const number = 11111111; + +const status = await bip(number) diff --git a/package.json b/package.json new file mode 100644 index 0000000..47f1215 --- /dev/null +++ b/package.json @@ -0,0 +1,83 @@ +{ + "name": "bip", + "version": "1.0.0", + "description": "Get balance of bip card (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/bip.git" + }, + "keywords": [ + "bip", + "transantiago", + "metro" + ], + "author": "Leonardo Gatica (https://about.me/lgatica)", + "license": "MIT", + "bugs": { + "url": "https://github.com/lgaticaq/bip/issues" + }, + "homepage": "https://github.com/lgaticaq/bip#readme", + "dependencies": { + "moment": "^2.11.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" +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..cb07ade --- /dev/null +++ b/src/index.js @@ -0,0 +1,25 @@ +'use strict'; + +import Q from 'q'; +import moment from 'moment'; +import rp from 'request-promise'; + +const getBalance = (number, cb) => { + const deferred = Q.defer(); + const options = { + url: `http://www.metrosantiago.cl/contents/guia-viajero/includes/consultarTarjeta/${number}`, + json: true + }; + rp(options).then((results) => { + const data = results[1]; + if (!data.salida) deferred.reject(new Error('Not found')); + deferred.resolve({ + balance: parseInt(data.saldo, 10), + date: moment(data.fecha, 'DD\/MM\/YYYY HH:mm').toDate() + }); + }).catch((err) => deferred.reject(err)); + deferred.promise.nodeify(cb); + return deferred.promise; +}; + +module.exports = getBalance; diff --git a/test/invalid.json b/test/invalid.json new file mode 100644 index 0000000..a6ac1b3 --- /dev/null +++ b/test/invalid.json @@ -0,0 +1,10 @@ +[ + { + "estado": 1, + "mensaje": "Esta tarjeta no se puede cargar" + }, + { + "salida": false, + "tarjeta": "11111111" + } +] diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..d1e3a98 --- /dev/null +++ b/test/test.js @@ -0,0 +1,77 @@ +'use strict'; + +import path from 'path'; + +import {expect} from 'chai'; +import nock from 'nock'; + +import lib from '../lib'; + +describe('bip', () => { + + + describe('valid', () => { + + const number = 11111111; + + beforeEach(() => { + nock.disableNetConnect(); + nock('http://www.metrosantiago.cl') + .get(`/contents/guia-viajero/includes/consultarTarjeta/${number}`) + .replyWithFile(200, path.join(__dirname, 'valid.json')); + }); + + it('should return a balance data (callback)', (done) => { + lib(number, (err, data) => { + expect(err).to.be.null; + expect(data).to.be.a('object'); + expect(data.balance).to.be.a('number'); + expect(data.date).to.be.a('date'); + done(); + }); + }); + + + it('should return a balance data (promise)', (done) => { + lib(number).then((data) => { + expect(data).to.be.a('object'); + expect(data.balance).to.be.a('number'); + expect(data.date).to.be.a('date'); + done(); + }).fail((err) => { + expect(err).to.be.null; + done(); + }); + }); + }); + + describe('invalid', () => { + + const number = 11111111; + + beforeEach(() => { + nock.disableNetConnect(); + nock('http://www.metrosantiago.cl') + .get(`/contents/guia-viajero/includes/consultarTarjeta/${number}`) + .replyWithFile(200, path.join(__dirname, 'invalid.json')); + }); + + it('should return a empty data (callback)', (done) => { + lib(number, (err, data) => { + expect(err).to.eql(new Error('Not found')); + expect(data).to.be.undefined; + done(); + }); + }); + + it('should return a empty data (promise)', (done) => { + lib(number).then((data) => { + expect(data).to.be.undefined; + done(); + }).fail((err) => { + expect(err).to.eql(new Error('Not found')); + done(); + }); + }); + }); +}); diff --git a/test/valid.json b/test/valid.json new file mode 100644 index 0000000..0287641 --- /dev/null +++ b/test/valid.json @@ -0,0 +1,12 @@ +[ + { + "estado": 0, + "mensaje": "Tarjeta Valida" + }, + { + "salida": true, + "tarjeta": "11111111", + "saldo": "1180", + "fecha": "02\/01\/2016 20:59" + } +]