Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update module to work with the new API

  • Loading branch information...
commit 29ac4dc6b8fff8fea0a87629e4f82108f339ed59 1 parent 4fdc2a8
@mape authored
Showing with 34 additions and 50 deletions.
  1. +33 −49 lib/notifo.js
  2. +1 −1  package.json
View
82 lib/notifo.js
@@ -1,6 +1,5 @@
-var sys = require('sys');
-var net = require('net');
var qs = require('querystring');
+var https = require('https');
module.exports = function (args) {
return new Notifo(args);
@@ -15,71 +14,56 @@ function Notifo(args) {
throw new Error('Must supply a username');
return false;
}
- this.secret = args.secret || '';
- this.username = args.username || '';
+ this.secret = args.secret;
+ this.username = args.username;
};
Notifo.prototype.send = function (options, callback) {
- var self = this;
// Setup default values and light validation.
-
if (!options.title && !options.uri && !options.msg) {
- throw new Error('Tried to send notification without any information.');
+ callback(new Error('Tried to send notification without any information.'), null);
return false;
}
// Make sure the messages don't exceed the APIs max length.
Object.keys(options).forEach(function (key) {
- options[key] = options[key].substr(0,1024);
+ options[key] = options[key].substr(0,1024)+'';
});
if (!options.msg) {
- throw new Error('Must supply a msg.');
+ callback(new Error('Must supply a msg.'), null);
return false;
}
-
- var client = net.createConnection (443, 'api.notifo.com');
-
- client.setEncoding('UTF8');
- client.addListener('connect', function () {
- // Make sure we are able to TLS
- try {
- client.setSecure();
- } catch (e) {
- throw new Error('SSL is not supported in your version of node JS. This is needed for this module to function.');
- }
- });
// Send the request containing notification data.
- var auth = new Buffer(this.username+':'+this.secret).toString('base64');
- var postData = qs.stringify(options);
-
- client.addListener('secure', function () {
- client.write('POST /v1/send_notification HTTP/1.1\r\n');
- client.write('Host: api.notifo.com\r\n');
- client.write('User-Agent: node-notifo\r\n');
- client.write('Authorization: Basic '+auth+'\r\n');
- client.write('Content-Type: application/x-www-form-urlencoded\r\n');
- client.write('Content-Length:'+ postData.length+'\r\n');
- client.write('\r\n');
- client.write(postData+'\r\n');
- client.write('\r\n');
+ var req = https.request({
+ 'host': 'api.notifo.com'
+ , 'port': 443
+ , 'path': '/v1/send_notification'
+ , 'method': 'POST'
+ , 'headers': {
+ 'Authorization': 'Basic '+new Buffer(this.username+':'+this.secret).toString('base64')
+ , 'Content-Type': 'application/x-www-form-urlencoded'
+ }
+ }, function(res) {
+ res.setEncoding('utf8');
+ res.on('data', function(body) {
+ try {
+ var responce = JSON.parse(body.match(/{"status.*/)[0].trim());
+ if (responce['response_code'] !== 2201) {
+ callback(new Error('notifo code '+responce['response_code']+': '+responce['response_message']), responce);
+ } else {
+ callback(null, responce);
+ }
+ } catch(e) {
+ callback(e, null);
+ }
+ });
});
+ req.write(qs.stringify(options));
+ req.end();
- var body = '';
- client.on('data', function (chunk) {
- body += chunk;
- });
- client.on('end', function (chunk) {
- try {
- var responce = JSON.parse(body.match(/{"status.*/)[0].trim());
- if (responce['response_code'] !== 2201) {
- callback(new Error(responce['response_code']+': '+responce['response_message']), null);
- } else {
- callback(null, responce);
- }
- } catch(e) {
- callback(e, null);
- }
+ req.on('error', function(e) {
+ callback(e, null);
});
};
View
2  package.json
@@ -1,7 +1,7 @@
{
"name" : "notifo",
"description" : "Send push notifications to your iPhone for free through http://notifo.com/.",
- "version" : "0.0.1",
+ "version" : "0.0.2",
"author" : "Mathias Pettersson <mape@mape.me>",
"engines" : ["node"],
"directories" : { "lib" : "./lib" },
Please sign in to comment.
Something went wrong with that request. Please try again.