Skip to content

Commit

Permalink
Sources feature added (partial) (#13)
Browse files Browse the repository at this point in the history
* Basic Sources Added

* tests cleanup

* README updated

* Update README.md
  • Loading branch information
adasq committed Sep 1, 2018
1 parent 7b07d92 commit 4c5cb06
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 20 deletions.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ alerts.configure({

```js
const alerts = require('google-alerts-api');
const { HOW_OFTEN, DELIVER_TO, HOW_MANY } = alerts;
const { HOW_OFTEN, DELIVER_TO, HOW_MANY, SOURCE_TYPE } = alerts;

alerts.configure({
mail: 'your_mail@gmail.com',
Expand All @@ -62,7 +62,7 @@ alerts.sync((err) => {
alertList.forEach(alert => printAlertInfo);
});

function printAlertInfo(alert){
function printAlertInfo(alert) {
console.log('name:', alert.name);
//'How Many' property information:
if (alert.howMany === HOW_MANY.BEST) {
Expand All @@ -78,7 +78,7 @@ function printAlertInfo(alert){
name: '"Donald Trump * ISIS"',
id: '4f94515ec736ef62:ade5b03803caa237:com:en:PL:R',
howOften: 2, //use HOW_OFTEN enum to find out proper meaning
sources: [], // sources are not supported yet
sources: '...', // some of SOURCE_TYPE enum property, SOURCE_TYPE.AUTOMATIC by default
lang: 'en',
region: 'PL',
howMany: 3, //use HOW_MANY enum to find out proper meaning
Expand Down Expand Up @@ -115,13 +115,33 @@ function printAlertInfo(alert){
}
```

#### Available source types:

```
const SOURCE_TYPE = {
AUTOMATIC,
NEWS,
BLOGS,
WEB,
NEWS_AND_BLOGS,
NEWS_AND_WEB,
BLOGS_AND_WEB,
VIDEO,
BOOKS,
DISCUSSIONS,
FINANCE,
};
```

#### Create alert:

```js
alerts.sync(() => {
const alertToCreate = {
howOften: HOW_OFTEN.AT_MOST_ONCE_A_DAY,
sources: [],
sources: SOURCE_TYPE.AUTOMATIC, // default one
lang: 'en',
name: 'NodeJS AND "Chrome V8"',
region: 'PL', // or do not specify it at all, if you want "All Regions"
Expand Down
43 changes: 34 additions & 9 deletions src/alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ const DELIVER_TO = {
RSS: 2
};

const SOURCE_TYPE = {
AUTOMATIC: '[null,null,null]',
NEWS: '[null,null,[1,3]]',
BLOGS: '[null,null,[2,3]]',
WEB: '[null,null,[2,1]]',

NEWS_AND_BLOGS: '[null,null,[3]]',
NEWS_AND_WEB: '[null,null,[1]]',
BLOGS_AND_WEB: '[null,null,[2]]',

VIDEO: '[null,[5],[2,1,3]]',
BOOKS: '[null,[6],[2,1,3]]',
DISCUSSIONS: '[null,[7],[2,1,4]]',
FINANCE: '[null,[8],[2,1,4]]',
};

const ID_LOC = '1';
const HOW_OFTEN_LOC = '2.6.0.4';
const RSS_ID_LOC = '2.6.0.11';
Expand All @@ -38,7 +54,7 @@ function dataTypeToPropertyLocMap(dataType) {
howMany: HOW_MANY_LOC,
name: NAME_LOC,
deliverTo: DELIVER_TO_LOC,
deliverToData: DELIVER_TO_DATA_LOC
deliverToData: DELIVER_TO_DATA_LOC,
}[dataType];
}

Expand Down Expand Up @@ -103,18 +119,19 @@ function getRssFeedByCreateResponse(body) {

id: np.get(alert, ID_LOC),
howOften: np.get(alert, HOW_OFTEN_LOC),
sources: getSources(alert),
sources: JSON.stringify(getSources(alert)),
lang: np.get(alert, LANG_LOC),
region: np.get(alert, REGION_LOC),
howMany: np.get(alert, HOW_MANY_LOC),
deliverTo: np.get(alert, DELIVER_TO_LOC),
rss: getRss(alert),
deliverToData: np.get(alert, DELIVER_TO_DATA_LOC)
deliverToData: np.get(alert, DELIVER_TO_DATA_LOC)
};
}

function create(data, createId) {
const {howOften, sources, lang, name, region, howMany, deliverTo, deliverToData} = data;
let {howOften, sources, lang, name, region, howMany, deliverTo, deliverToData} = data;
sources = JSON.parse(sources || SOURCE_TYPE.AUTOMATIC);
const n = null;
const getHowOftenPadding = (howOften) => {
if(howOften === HOW_OFTEN.AT_MOST_ONCE_A_DAY) {
Expand All @@ -126,7 +143,7 @@ function getRssFeedByCreateResponse(body) {
return [];
}
const rssId = "0";
return [n,
const result = [n,
[
n,n,n,
[
Expand All @@ -143,6 +160,7 @@ function getRssFeedByCreateResponse(body) {
...sources
]
];
return result;
}


Expand Down Expand Up @@ -198,20 +216,27 @@ function getAlertCopy(alert) {
}

function modifyData(alert, newData) {

const alertCopy = getAlertCopy(alert);

Object.keys(newData).forEach(paramType => {
const paramValue = newData[paramType];
const propertyLoc = dataTypeToPropertyLocMap(paramType);
np.set(alertCopy, propertyLoc, paramValue);
if(paramType === 'sources') {
const parsed = JSON.parse(paramValue)
np.set(alertCopy, '2.7', parsed[0]);
np.set(alertCopy, '2.8', parsed[1]);
np.set(alertCopy, '2.9', parsed[2]);
} else {
const propertyLoc = dataTypeToPropertyLocMap(paramType);
np.set(alertCopy, propertyLoc, paramValue);
}

});

return alertCopy;
}

module.exports = {
HOW_OFTEN, DELIVER_TO, HOW_MANY,
HOW_OFTEN, DELIVER_TO, HOW_MANY, SOURCE_TYPE,
findAlertById, modifyData,
getCreateIdByState, getAlertsByState, getRequestXByState,
getStateByBody, create, parseAlertToData, printAlertData,
Expand Down
4 changes: 2 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const reqHandler = require('./request-handler.js');
const alerts = require('./alerts.js');

const { HOW_OFTEN, DELIVER_TO, HOW_MANY } = alerts;
const { HOW_OFTEN, DELIVER_TO, HOW_MANY, SOURCE_TYPE } = alerts;
const config = { mail: '', password: ''};

let isAuthenticated = false;
Expand Down Expand Up @@ -126,7 +126,7 @@ function create(createData, cb) {
}

module.exports = {
HOW_OFTEN, DELIVER_TO, HOW_MANY, ERROR,
HOW_OFTEN, DELIVER_TO, HOW_MANY, ERROR, SOURCE_TYPE,
configure,
generateCookies,
sync,
Expand Down
44 changes: 39 additions & 5 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const nconf = require('nconf');
const fs = require('fs');
const { parseAlertToData, printAlertData} = require('../src/alerts.js');

const { HOW_OFTEN, DELIVER_TO, HOW_MANY } = api;
const { HOW_OFTEN, DELIVER_TO, HOW_MANY, SOURCE_TYPE } = api;

const TIMEOUT_MS = 10 * 1000;

Expand Down Expand Up @@ -81,17 +81,17 @@ describe('google', function() {
api.sync(() => {
const alertToCreate = {
howOften: HOW_OFTEN.AT_MOST_ONCE_A_DAY,
sources: [],
lang: 'en',
name: NAME,
region: 'PL',
howMany: HOW_MANY.BEST,
deliverTo: DELIVER_TO.RSS,
deliverToData: ''
deliverToData: '',
};

api.create(alertToCreate, (err, alert) => {
expect(alert.name).to.be(alertToCreate.name);
expect(alert.sources).to.be(SOURCE_TYPE.AUTOMATIC);
done();
});
});
Expand All @@ -101,7 +101,6 @@ describe('google', function() {
api.sync(() => {
const alertToCreate = {
howOften: HOW_OFTEN.AT_MOST_ONCE_A_DAY,
sources: [],
lang: 'en',
name: NAME + 2,
region: 'PL',
Expand All @@ -125,6 +124,29 @@ describe('google', function() {
done();
});
});
it('modify:sources:BLOGS', (done) => {
const alert = findAlertByName(api.getAlerts(), NAME);

api.modify(alert.id, {sources: SOURCE_TYPE.BLOGS}, (err, resp, body) => {
api.sync(() => {
const alert = findAlertByName(api.getAlerts(), NAME);
expect(alert.sources).to.be(SOURCE_TYPE.BLOGS);
done();
});
});
});

it('modify:sources:NEWS_AND_WEB', (done) => {
const alert = findAlertByName(api.getAlerts(), NAME);

api.modify(alert.id, {sources: SOURCE_TYPE.NEWS_AND_WEB}, (err, resp, body) => {
api.sync(() => {
const alert = findAlertByName(api.getAlerts(), NAME);
expect(alert.sources).to.be(SOURCE_TYPE.NEWS_AND_WEB);
done();
});
});
});

it('modify:deliverTo', (done) => {
const alert = findAlertByName(api.getAlerts(), NAME);
Expand Down Expand Up @@ -192,7 +214,19 @@ describe('google', function() {
done();
});
});
});
});

it('remove NAME 2', (done) => {
const alert = findAlertByName(api.getAlerts(), NAME + 2);
api.remove(alert.id, (err, resp, body) => {
api.sync(() => {
const alert = findAlertByName(api.getAlerts(), NAME + 2);
expect(alert).to.be(null);
done();
});
});
});

});
});

Expand Down

0 comments on commit 4c5cb06

Please sign in to comment.