node-pushover
Advanced tools
Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "node-pushover", | ||
"description": "node.js pushover module for https://pushover.net/api", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": { | ||
@@ -22,3 +22,6 @@ "name": "Sam Decrock", | ||
} | ||
] | ||
], | ||
"dependencies": { | ||
"httpreq": "~0.1.1" | ||
} | ||
} |
136
pushover.js
@@ -1,39 +0,115 @@ | ||
var querystring = require('querystring'); | ||
var https = require('https'); | ||
function send(options, callback) { | ||
var params = querystring.stringify(options); | ||
/* | ||
Copyright (c) 2013 Sam Decrock <sam.decrock@gmail.com> | ||
var post_options = { | ||
host: "api.pushover.net", | ||
port: 443, | ||
path: "/1/messages.json ", | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Content-Length': params.length | ||
MIT License | ||
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. | ||
*/ | ||
var httpreq = require('httpreq'); | ||
function Pushover(options) { | ||
if(!options.token) { | ||
throw new Error('No app token defined'); | ||
return; | ||
} | ||
this.token = options.token; | ||
if(options.user) | ||
this.user = options.user; | ||
return this; | ||
} | ||
/** | ||
* send([user,] title, message, [callback]) | ||
*/ | ||
Pushover.prototype.send = function(arg1, arg2, arg3, arg4) { | ||
if( arguments.length == 2 ){ | ||
// (title, message) | ||
if(!this.user) { | ||
throw new Error('No user token defined'); | ||
return; | ||
} | ||
}; | ||
var post_req = https.request(post_options, function(res) { | ||
res.setEncoding('utf8'); | ||
send({ | ||
token: this.token, | ||
user: this.user, | ||
title: arg1, | ||
message: arg2 | ||
}); | ||
} | ||
res.on('data', function (chunk) { | ||
var response = JSON.parse(chunk); | ||
if(response.status != 1) { | ||
if (callback && typeof callback === "function") callback(response); | ||
} else { | ||
if (callback && typeof callback === "function") callback(null, response); | ||
if( arguments.length == 3 ){ | ||
if(typeof(arg3)==="function"){ | ||
// (title, message, callback) | ||
if(!this.user) { | ||
throw new Error('No user token defined'); | ||
return; | ||
} | ||
}); | ||
send({ | ||
token: this.token, | ||
user: this.user, | ||
title: arg1, | ||
message: arg2 | ||
}, arg3); | ||
res.on('close', function (err) { | ||
callback(err); | ||
}); | ||
}else{ | ||
// (user, title, message) | ||
send({ | ||
token: this.token, | ||
user: arg1, | ||
title: arg2, | ||
message: arg3 | ||
}); | ||
} | ||
} | ||
if( arguments.length == 4 ){ | ||
// (user, title, message, callback) | ||
send({ | ||
token: this.token, | ||
user: arg1, | ||
title: arg2, | ||
message: arg3 | ||
}, arg4); | ||
} | ||
}; | ||
function send(parameters, callback){ | ||
httpreq.post("https://api.pushover.net/1/messages.json", { parameters: parameters}, function (err, res){ | ||
if (callback && typeof callback === "function"){ | ||
if(err){ | ||
callback(err); | ||
}else{ | ||
var response = JSON.parse(res.body); | ||
if(response.status != 1) { | ||
callback(response); | ||
} else { | ||
callback(null, response); | ||
} | ||
} | ||
} | ||
}); | ||
post_req.write(params); | ||
post_req.end(); | ||
} | ||
exports.send = send; | ||
module.exports = Pushover; | ||
@@ -12,21 +12,66 @@ node-pushover | ||
## Example | ||
## Initialization | ||
### new Pushover({ token: "APPTOKEN" [, user: "USERKEY"]}); | ||
Initializes a Pushover object with the __APPTOKEN__ and optionally a __USERKEY__ . The __USERKEY__ can also be given when sending the messages. | ||
## Sending messages | ||
### push.send([USERKEY,] title, message [,callback]) | ||
__Arguments__ | ||
- USERKEY: (optional) The __USERKEY__ as given to you by the Pushover API. | ||
- title: The title for your message | ||
- message: The content for your message | ||
- callback(err, res): (optional) A callback function which is called when the message is send. | ||
__Example with the USERKEY given at initialisation__ | ||
```js | ||
var pushover = require('node-pushover'); | ||
var Pushover = require('./pushover'); | ||
var push = new Pushover({ | ||
token: "APPTOKEN", | ||
user: "USERKEY" | ||
}); | ||
pushover.send({ | ||
token: "YOUR APP TOKEN", | ||
user: "USER KEY", | ||
title: "Node", | ||
message: "Node.js is so cool!" | ||
}, function(err, response){ | ||
if (err){ | ||
console.error("Error sending pushover"); | ||
console.error(err); | ||
// No callback function defined: | ||
push.send("Some title", "Node.js is Cool!! - no callback"); | ||
// A callback function is defined: | ||
push.send("Some title", "Node.js is Cool!!", function (err, res){ | ||
if(err){ | ||
console.log("We have an error:"); | ||
console.log(err); | ||
console.log(err.stack); | ||
}else{ | ||
console.log("Sending pushover complete"); | ||
console.log(response); | ||
console.log("Message send successfully"); | ||
console.log(res); | ||
} | ||
}); | ||
``` | ||
__Example with the USERKEY given when the message is send__ | ||
```js | ||
var Pushover = require('./pushover'); | ||
var push = new Pushover({ | ||
token: "APPTOKEN" | ||
}); | ||
// No callback function defined: | ||
push.send("USERKEY", "Some title", "Node.js is Cool!! - no callback"); | ||
// A callback function is defined: | ||
push.send("USERKEY", "Some title", "Node.js is Cool!!", function (err, res){ | ||
if(err){ | ||
console.log("We have an error:"); | ||
console.log(err); | ||
console.log(err.stack); | ||
}else{ | ||
console.log("Message send successfully"); | ||
console.log(res); | ||
} | ||
}); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
6165
6
130
77
1
2
+ Addedhttpreq@~0.1.1
+ Addedhttpreq@0.1.1(transitive)