Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

google-stocks

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

google-stocks - npm Package Compare versions

Comparing version 0.1.6 to 0.2.0

.eslintrc

72

dist/google-stocks.js

@@ -6,9 +6,6 @@ 'use strict';

});
exports['default'] = googleStocks;
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _request = require('request');

@@ -18,50 +15,43 @@

var GoogleStocks = (function () {
function GoogleStocks() {
_classCallCheck(this, GoogleStocks);
var _promise = require('promise');
this.get = this.get.bind(this);
var HTTP_OK = 200;
var URL = 'https://www.google.com/finance/info?&q=';
this._url = 'https://www.google.com/finance/info?&q=';
}
function onParse(data) {
// remove first 3 characters which are "//"
return JSON.parse(data.substring(3));
}
_createClass(GoogleStocks, [{
key: 'get',
value: function get(stocks, callback) {
var _this = this;
function googleStocksCallback(stocks, callback) {
if (stocks === undefined) stocks = [];
if (stocks === undefined) stocks = [];
if (!stocks.length) {
return callback('No Stocks were passed');
}
if (!stocks.length) {
return callback('No Stocks were passed');
}
var url = URL + stocks.join(',');
var url = this._url + stocks.join(',');
(0, _request2['default'])(url, function (error, response, body) {
if (error) {
return callback(error);
} else if (parseInt(response.statusCode, 10) !== HTTP_OK) {
return callback('Webservice returned ' + response.statusCode);
}
(0, _request2['default'])(url, function (error, response, body) {
if (error) {
return callback(error);
} else if (parseInt(response.statusCode, 10) !== 200) {
return callback('Webservice returned ' + response.statusCode);
}
callback(error, onParse(body));
});
}
body = _this._onParse(body);
function googleStocks(stocks, callback) {
if (stocks === undefined) stocks = [];
callback(error, body);
});
}
}, {
key: '_onParse',
value: function _onParse(data) {
// remove first 3 characters which are "//"
data = data.substring(3);
if (!callback) {
var func = (0, _promise.denodeify)(googleStocksCallback);
return func(stocks);
}
return JSON.parse(data);
}
}]);
return googleStocksCallback(stocks, callback);
}
return GoogleStocks;
})();
exports['default'] = new GoogleStocks();
module.exports = exports['default'];

@@ -6,8 +6,10 @@ 'use strict';

googleStocks.get(['AAPL'], function(error, data) {
console.log(googleStocks);
googleStocks(['AAPL'], function(error, data) {
console.log(data);
});
googleStocks.get(['TSE:WJA', 'NASDAQ:GOOG', 'AAPL'], function(error, data) {
googleStocks(['TSE:WJA', 'NASDAQ:GOOG', 'AAPL'], function(error, data) {
console.log(data);
});
{
"name": "google-stocks",
"version": "0.1.6",
"version": "0.2.0",
"author": "anvk",

@@ -14,2 +14,7 @@ "main": "dist/google-stocks.js",

"scripts": {
"lint": "eslint src",
"build:source": "rimraf dist/ && babel src/ --out-dir dist/",
"build:tests": "rimraf test-dist/ && babel test/ --out-dir test-dist/",
"clean": "rimraf dist/ && rimraf test-dist/",
"build": "npm run build:source && npm run build:tests",
"test": "node node_modules/mocha/bin/mocha --reporter spec test-dist/*",

@@ -31,12 +36,13 @@ "example": "node example.js"

"dependencies": {
"request": "^2.55.0"
"promise": "7.1.1",
"request": "2.55.0"
},
"devDependencies": {
"babel": "5.6.5",
"babel-eslint": "5.0.0",
"chai": "3.0.0",
"gulp": "3.9.0",
"gulp-babel": "5.1.0",
"gulp-mocha": "2.1.2",
"gulp-jshint": "1.12.0",
"mocha": "2.2.5"
"eslint": "2.2.0",
"eslint-config-airbnb": "6.0.2",
"mocha": "2.2.5",
"rimraf": "2.5.2"
},

@@ -43,0 +49,0 @@ "engine": "node >= 0.10.x",

@@ -15,10 +15,12 @@ # google-stocks [![Build Status](https://travis-ci.org/anvk/google-stocks.svg?branch=master)](https://travis-ci.org/anvk/google-stocks)

### Callbacks
```js
var googleStocks = require('google-stocks');
googleStocks.get(['AAPL'], function(error, data) {
googleStocks(['AAPL'], function(error, data) {
console.log(data);
});
googleStocks.get(['TSE:WJA', 'NASDAQ:GOOG', 'AAPL'], function(error, data) {
googleStocks(['TSE:WJA', 'NASDAQ:GOOG', 'AAPL'], function(error, data) {
console.log(data);

@@ -28,2 +30,46 @@ });

### Promises
```js
var googleStocks = require('google-stocks');
googleStocks(['AAPL'])
.then(function(data) {
/* do something with data */
})
.catch(function(error) {
/* error logic */
});
googleStocks(['TSE:WJA', 'NASDAQ:GOOG', 'AAPL'])
.then(function(data) {
/* do something with data */
})
.catch(function(error) {
/* error logic */
});
```
### ES2015
```js
import googleStocks from 'google-stocks';
googleStocks(['AAPL'])
.then(data => {
/* do something with data */
})
.catch(error => {
/* error logic */
});
googleStocks(['TSE:WJA', 'NASDAQ:GOOG', 'AAPL'])
.then(data => {
/* do something with data */
})
.catch(error => {
/* error logic */
});
```
returned format looks like this:

@@ -118,3 +164,3 @@

```
node example.js
npm run example
```

@@ -125,3 +171,1 @@

MIT license; see [LICENSE](./LICENSE).
(c) 2015 by Alexey Novak

@@ -1,42 +0,37 @@

'use strict';
import request from 'request';
import { denodeify } from 'promise';
class GoogleStocks {
const HTTP_OK = 200;
const URL = 'https://www.google.com/finance/info?&q=';
constructor() {
this.get = this.get.bind(this);
function onParse(data) {
// remove first 3 characters which are "//"
return JSON.parse(data.substring(3));
}
this._url = 'https://www.google.com/finance/info?&q=';
function googleStocksCallback(stocks = [], callback) {
if (!stocks.length) {
return callback('No Stocks were passed');
}
get(stocks = [], callback) {
if (!stocks.length) {
return callback('No Stocks were passed');
const url = URL + stocks.join(',');
request(url, (error, response, body) => {
if (error) {
return callback(error);
} else if (parseInt(response.statusCode, 10) !== HTTP_OK) {
return callback(`Webservice returned ${response.statusCode}`);
}
var url = this._url + stocks.join(',');
callback(error, onParse(body));
});
}
request(url, (error, response, body) => {
if (error) {
return callback(error);
} else if (parseInt(response.statusCode, 10) !== 200) {
return callback('Webservice returned ' + response.statusCode);
}
body = this._onParse(body);
callback(error, body);
});
export default function googleStocks(stocks = [], callback) {
if (!callback) {
const func = denodeify(googleStocksCallback);
return func(stocks);
}
_onParse(data) {
// remove first 3 characters which are "//"
data = data.substring(3);
return JSON.parse(data);
}
return googleStocksCallback(stocks, callback);
}
export default new GoogleStocks();

@@ -13,67 +13,136 @@ 'use strict';

it('nothing was passed', function () {
_distGoogleStocksJs2['default'].get(undefined, function (error, data) {
(0, _chai.expect)(error).to.equal('No Stocks were passed');
describe('callback based', function () {
it('nothing was passed', function () {
(0, _distGoogleStocksJs2['default'])(undefined, function (error) {
(0, _chai.expect)(error).to.equal('No Stocks were passed');
});
});
});
it('empty array', function () {
_distGoogleStocksJs2['default'].get([], function (error, data) {
(0, _chai.expect)(error).to.equal('No Stocks were passed');
it('empty array', function () {
(0, _distGoogleStocksJs2['default'])([], function (error) {
(0, _chai.expect)(error).to.equal('No Stocks were passed');
});
});
});
it('with a non existing code', function () {
var codes = ['ZXY11_'];
it('with a non existing code', function () {
var codes = ['ZXY11_'];
_distGoogleStocksJs2['default'].get(codes, function (error, data) {
(0, _chai.expect)(error).to.equal('Webservice returned 400');
(0, _chai.expect)(data).to.be.undefined;
(0, _distGoogleStocksJs2['default'])(codes, function (error, data) {
(0, _chai.expect)(error).to.equal('Webservice returned 400');
(0, _chai.expect)(data).to.be.undefined;
});
});
});
it('proper stock code', function (done) {
var codes = ['AAPL'];
it('proper stock code', function (done) {
var codes = ['AAPL'];
_distGoogleStocksJs2['default'].get(codes, function (error, data) {
(0, _chai.expect)(error).to.be['null'];
(0, _chai.expect)(data.length).to.equal(1);
(0, _chai.expect)(data.shift().t).to.equal(codes.shift());
done();
(0, _distGoogleStocksJs2['default'])(codes, function (error, data) {
(0, _chai.expect)(error).to.be['null'];
(0, _chai.expect)(data.length).to.equal(1);
(0, _chai.expect)(data.shift().t).to.equal(codes.shift());
done();
});
});
});
it('with a different market code', function (done) {
var market = 'TSE',
code = 'WJA',
codes = [market + ':' + code];
it('with a different market code', function (done) {
var market = 'TSE';
var code = 'WJA';
var codes = [market + ':' + code];
_distGoogleStocksJs2['default'].get(codes, function (error, data) {
(0, _chai.expect)(error).to.be['null'];
(0, _chai.expect)(data.length).to.equal(1);
(0, _distGoogleStocksJs2['default'])(codes, function (error, data) {
(0, _chai.expect)(error).to.be['null'];
(0, _chai.expect)(data.length).to.equal(1);
var result = data.shift();
(0, _chai.expect)(result.t).to.equal(code);
(0, _chai.expect)(result.e).to.equal(market);
done();
var result = data.shift();
(0, _chai.expect)(result.t).to.equal(code);
(0, _chai.expect)(result.e).to.equal(market);
done();
});
});
});
it('with a an existing code and non existing code', function (done) {
var codes = ['ZXY11_', 'GOOG'];
it('with a an existing code and non existing code', function (done) {
var codes = ['ZXY11_', 'GOOG'];
_distGoogleStocksJs2['default'].get(codes, function (error, data) {
(0, _chai.expect)(data.length).to.equal(1);
done();
(0, _distGoogleStocksJs2['default'])(codes, function (error, data) {
(0, _chai.expect)(data.length).to.equal(1);
done();
});
});
it('with both existing codes and different markets', function (done) {
var codes = ['TSE:WJA', 'GOOG'];
(0, _distGoogleStocksJs2['default'])(codes, function (error, data) {
(0, _chai.expect)(data.length).to.equal(2);
done();
});
});
});
it('with both existing codes and different markets', function (done) {
var codes = ['TSE:WJA', 'GOOG'];
describe('promise based', function () {
it('nothing was passed', function () {
(0, _distGoogleStocksJs2['default'])(undefined)['catch'](function (error) {
return (0, _chai.expect)(error).to.equal('No Stocks were passed');
});
});
_distGoogleStocksJs2['default'].get(codes, function (error, data) {
(0, _chai.expect)(data.length).to.equal(2);
done();
it('empty array', function () {
(0, _distGoogleStocksJs2['default'])(undefined)['catch'](function (error) {
return (0, _chai.expect)(error).to.equal('No Stocks were passed');
});
});
it('with a non existing code', function () {
var codes = ['ZXY11_'];
(0, _distGoogleStocksJs2['default'])(codes).then(function (data) {
return (0, _chai.expect)(data).to.be.undefined;
})['catch'](function (error) {
return (0, _chai.expect)(error).to.equal('Webservice returned 400');
});
});
it('proper stock code', function (done) {
var codes = ['AAPL'];
(0, _distGoogleStocksJs2['default'])(codes).then(function (data) {
(0, _chai.expect)(data.length).to.equal(1);
(0, _chai.expect)(data.shift().t).to.equal(codes.shift());
done();
});
});
it('with a different market code', function (done) {
var market = 'TSE';
var code = 'WJA';
var codes = [market + ':' + code];
(0, _distGoogleStocksJs2['default'])(codes).then(function (data) {
(0, _chai.expect)(data.length).to.equal(1);
var result = data.shift();
(0, _chai.expect)(result.t).to.equal(code);
(0, _chai.expect)(result.e).to.equal(market);
done();
});
});
it('with a an existing code and non existing code', function (done) {
var codes = ['ZXY11_', 'GOOG'];
(0, _distGoogleStocksJs2['default'])(codes).then(function (data) {
(0, _chai.expect)(data.length).to.equal(1);
done();
});
});
it('with both existing codes and different markets', function (done) {
var codes = ['TSE:WJA', 'GOOG'];
(0, _distGoogleStocksJs2['default'])(codes).then(function (data) {
(0, _chai.expect)(data.length).to.equal(2);
done();
});
});
});
});

@@ -1,4 +0,2 @@

'use strict';
import {expect} from 'chai';
import { expect } from 'chai';
import googleStocks from '../dist/google-stocks.js';

@@ -8,68 +6,137 @@

it('nothing was passed', () => {
googleStocks.get(undefined, (error, data) => {
expect(error).to.equal('No Stocks were passed');
describe('callback based', () => {
it('nothing was passed', () => {
googleStocks(undefined, error => {
expect(error).to.equal('No Stocks were passed');
});
});
});
it('empty array', () => {
googleStocks.get([], (error, data) => {
expect(error).to.equal('No Stocks were passed');
it('empty array', () => {
googleStocks([], error => {
expect(error).to.equal('No Stocks were passed');
});
});
});
it('with a non existing code', () => {
var codes = ['ZXY11_'];
it('with a non existing code', () => {
const codes = ['ZXY11_'];
googleStocks.get(codes, (error, data) => {
expect(error).to.equal('Webservice returned 400');
expect(data).to.be.undefined;
googleStocks(codes, (error, data) => {
expect(error).to.equal('Webservice returned 400');
expect(data).to.be.undefined;
});
});
});
it('proper stock code', done => {
var codes = ['AAPL'];
it('proper stock code', done => {
const codes = ['AAPL'];
googleStocks.get(codes, (error, data) => {
expect(error).to.be.null;
expect(data.length).to.equal(1);
expect(data.shift().t).to.equal(codes.shift());
done();
googleStocks(codes, (error, data) => {
expect(error).to.be.null;
expect(data.length).to.equal(1);
expect(data.shift().t).to.equal(codes.shift());
done();
});
});
});
it('with a different market code', done => {
var market = 'TSE',
code = 'WJA',
codes = [market + ':' + code];
it('with a different market code', done => {
const market = 'TSE';
const code = 'WJA';
const codes = [`${market}:${code}`];
googleStocks.get(codes, (error, data) => {
expect(error).to.be.null;
expect(data.length).to.equal(1);
googleStocks(codes, (error, data) => {
expect(error).to.be.null;
expect(data.length).to.equal(1);
var result = data.shift();
expect(result.t).to.equal(code);
expect(result.e).to.equal(market);
done();
const result = data.shift();
expect(result.t).to.equal(code);
expect(result.e).to.equal(market);
done();
});
});
});
it('with a an existing code and non existing code', done => {
var codes = ['ZXY11_', 'GOOG'];
it('with a an existing code and non existing code', done => {
const codes = ['ZXY11_', 'GOOG'];
googleStocks.get(codes, (error, data) => {
expect(data.length).to.equal(1);
done();
googleStocks(codes, (error, data) => {
expect(data.length).to.equal(1);
done();
});
});
it('with both existing codes and different markets', done => {
const codes = ['TSE:WJA', 'GOOG'];
googleStocks(codes, (error, data) => {
expect(data.length).to.equal(2);
done();
});
});
});
it('with both existing codes and different markets', done => {
var codes = ['TSE:WJA', 'GOOG'];
describe('promise based', () => {
it('nothing was passed', () => {
googleStocks(undefined)
.catch(error => expect(error).to.equal('No Stocks were passed'));
});
googleStocks.get(codes, (error, data) => {
expect(data.length).to.equal(2);
done();
it('empty array', () => {
googleStocks(undefined)
.catch(error => expect(error).to.equal('No Stocks were passed'));
});
it('with a non existing code', () => {
const codes = ['ZXY11_'];
googleStocks(codes)
.then(data => expect(data).to.be.undefined)
.catch(error => expect(error).to.equal('Webservice returned 400'));
});
it('proper stock code', done => {
const codes = ['AAPL'];
googleStocks(codes)
.then(data => {
expect(data.length).to.equal(1);
expect(data.shift().t).to.equal(codes.shift());
done();
});
});
it('with a different market code', done => {
const market = 'TSE';
const code = 'WJA';
const codes = [`${market}:${code}`];
googleStocks(codes)
.then(data => {
expect(data.length).to.equal(1);
const result = data.shift();
expect(result.t).to.equal(code);
expect(result.e).to.equal(market);
done();
});
});
it('with a an existing code and non existing code', done => {
const codes = ['ZXY11_', 'GOOG'];
googleStocks(codes)
.then(data => {
expect(data.length).to.equal(1);
done();
});
});
it('with both existing codes and different markets', done => {
const codes = ['TSE:WJA', 'GOOG'];
googleStocks(codes)
.then(data => {
expect(data.length).to.equal(2);
done();
});
});
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc