Permalink
Browse files

Add node-notifo to github.

  • Loading branch information...
mape committed Aug 24, 2010
0 parents commit 4fdc2a84833e78b1a107474f86e9a63c88f34972
Showing with 188 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +32 −0 README.md
  3. +41 −0 example.js
  4. +85 −0 lib/notifo.js
  5. +10 −0 package.json
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Mathias Pettersson, mape@mape.me
+
+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.
@@ -0,0 +1,32 @@
+# node-notifo
+
+Send push notifications to your iPhone for __free__ using node.js and [notifo.com](http://notifo.com/);
+
+## Installation
+
+Via [npm](http://github.com/isaacs/npm):
+
+ $ npm install notifo
+## Requires
+
+Account on [http://notifo.com/](http://notifo.com/) (__free__).
+
+## Usage
+ var Notifo = require('notifo');
+
+ var notification = new Notifo({
+ 'username': 'yourNotifoUsername'
+ , 'secret': 'yourNotifoApiSecret'
+ });
+
+ notification.send({
+ 'title': 'node-notifo test'
+ , 'uri': 'http://github.com/mape/node-notifo'
+ , 'msg': 'With URI and title.'
+ }, function(err, response) {
+ if (err) {
+ throw err
+ } else {
+ console.log(response);
+ }
+ });
@@ -0,0 +1,41 @@
+var Notifo = require('notifo');
+
+// Send uncaught exceptions to your iPhone.
+var notification = new Notifo({
+ 'username': 'yourNotifoUsername'
+ , 'secret': 'yourNotifoApiSecret'
+});
+
+notification.send({
+ 'uri': 'http://github.com/mape/node-notifo/'
+ , 'msg': 'With URI, no title.'
+}, function(err, response) {
+ if (err) {
+ throw err
+ } else {
+ console.log(response);
+ }
+});
+
+notification.send({
+ 'title': 'node-notifo test'
+ , 'msg': 'Without URI.'
+}, function(err, response) {
+ if (err) {
+ throw err
+ } else {
+ console.log(response);
+ }
+});
+
+notification.send({
+ 'title': 'node-notifo test'
+ , 'uri': 'http://github.com/mape/node-notifo/'
+ , 'msg': 'With URI and title.'
+}, function(err, response) {
+ if (err) {
+ throw err
+ } else {
+ console.log(response);
+ }
+});
@@ -0,0 +1,85 @@
+var sys = require('sys');
+var net = require('net');
+var qs = require('querystring');
+
+module.exports = function (args) {
+ return new Notifo(args);
+};
+
+function Notifo(args) {
+ if (!args.secret) {
+ throw new Error('Must supply a valid Notifo API secret');
+ return false;
+ }
+ if (!args.username) {
+ throw new Error('Must supply a username');
+ return false;
+ }
+ 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.');
+ 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);
+ });
+
+ if (!options.msg) {
+ throw new Error('Must supply a msg.');
+ 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 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);
+ }
+ });
+};
@@ -0,0 +1,10 @@
+{
+ "name" : "notifo",
+ "description" : "Send push notifications to your iPhone for free through http://notifo.com/.",
+ "version" : "0.0.1",
+ "author" : "Mathias Pettersson <mape@mape.me>",
+ "engines" : ["node"],
+ "directories" : { "lib" : "./lib" },
+ "main" : "./lib/notifo",
+ "repository" : { "type":"git", "url":"http://github.com/mape/node-notifo" }
+}

0 comments on commit 4fdc2a8

Please sign in to comment.