Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle remove by rule ids #38

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
lib/*.cache
.DS_Store
.idea/
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ The callback receives an object as the 2nd argument and contains the number of a
Clears cached rules.

### The following methods uses Gnip API directly and ignores the local cache. Avoid usage if you are working with too many rules!
- `live.update(rules: Array, callback)`
- `live.add(rules: Array, callback)`
- `live.remove(rules: Array, callback)`
- `live.update(rules: Array<(string|{value:string, tag:string})>, callback)`
- `live.add(rules: Array<(string|{value:string, tag:string})>, callback)`
- `live.remove(rules: Array<(string|{value:string, tag:string})>, callback)`
- `live.removeByIds(ids: Array<string>, cb)`
- `live.getAll(callback)`
- `live.getByIds(ids: Array, callback)`
- `live.getByIds(ids: Array<string>, callback)`
- `live.removeAll(callback)`

# Gnip.Search
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ GnipStream.prototype.start = function () {
self.end();
});
if (res.statusCode < 200 || res.statusCode > 299) {
self.emit('error', new Error('Response error. HTTP status code: ' + res.statusCode));
self.emit('error', new Error('Response error. HTTP status code: ' + res.statusCode + 'body: '+ JSON.stringify(res && res.body)));
self.end();
}
else {
Expand Down
27 changes: 23 additions & 4 deletions lib/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function formatFailedRules (body) {

/**
* Add rules
* @param rules Array of rules as strings or objects in the form {value: String, tag: String}
* @param {Array<(string|{value:string, tag:string})>} rules Array of rules as strings or objects in the form {value: String, tag: String}
* @param cb Function callback
*/
LiveRules.prototype.add = function (rules, cb) {
Expand Down Expand Up @@ -54,7 +54,7 @@ LiveRules.prototype.add = function (rules, cb) {

/**
* Delete rules
* @param rules Array of rules as strings or objects in the form {value: String, tag: String}
* @param {Array<(string|{value:string, tag:string})>}rules Array of rules as strings or objects in the form {value: String, tag: String}
* @param cb Function callback
*/
LiveRules.prototype.remove = function (rules, cb) {
Expand All @@ -77,10 +77,29 @@ LiveRules.prototype.remove = function (rules, cb) {
}
});
};
/**
* delete rules by ids
* @param {Array<string>} rules_ids
* @param cb
*/
LiveRules.prototype.removeByIds = function (rules_ids, cb) {
var json = {
rule_ids: rules_ids
};
request.post({
url: this._api + '?_method=delete',
json: json,
headers: { 'Authorization': this._auth }
}, function (err, response, body) {
if (err) cb(err);
else if (response.statusCode >= 200 && response.statusCode < 300) cb(null, body);
else cb(new Error('Unable to delete rules. Request failed with status code: ' + response.statusCode + 'body: '+ JSON.stringify(body)));
});
};
Comment on lines +89 to +98
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you extract this logic so we can reuse it in the method above? It is the same, except the payload is different. After this change, I can merge and release a new minor version later today,.


/**
* Replace existing tracking rules
* @param rules Array of rules as strings or objects in the form {value: String, tag: String}
* @param {Array<(string|{value:string, tag:string})>} rules Array of rules as strings or objects in the form {value: String, tag: String}
* @param cb Function callback
*/
LiveRules.prototype.update = function (rules, cb) {
Expand Down Expand Up @@ -118,7 +137,7 @@ LiveRules.prototype.getAll = function (cb) {

/**
* Get rules matching GNIP IDs
* @param ids List of strings
* @param {Array<string>} ids List of strings
* @param cb Function callback
*/
LiveRules.prototype.getByIds = function (ids, cb) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.