Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed Jan 3, 2016
0 parents commit 8ea959c
Show file tree
Hide file tree
Showing 14 changed files with 330 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.

45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
}
```
5 changes: 5 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const bip = require('bip');

const number = 11111111;

const status = await bip(number)
83 changes: 83 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <lgatica@protonmail.com> (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"
}
25 changes: 25 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 10 additions & 0 deletions test/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"estado": 1,
"mensaje": "Esta tarjeta no se puede cargar"
},
{
"salida": false,
"tarjeta": "11111111"
}
]
77 changes: 77 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});
12 changes: 12 additions & 0 deletions test/valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"estado": 0,
"mensaje": "Tarjeta Valida"
},
{
"salida": true,
"tarjeta": "11111111",
"saldo": "1180",
"fecha": "02\/01\/2016 20:59"
}
]

0 comments on commit 8ea959c

Please sign in to comment.