Browse files

indent code, return correct error messages

  • Loading branch information...
1 parent 1d41973 commit fc03b7336dc8baf8eb5ff87e887e3058db68a7d2 @arnklint committed Feb 15, 2013
Showing with 44 additions and 40 deletions.
  1. +44 −40 lib/prowl.js
View
84 lib/prowl.js
@@ -1,16 +1,17 @@
var request = require('request'),
- querystring = require('querystring'),
- helpers = require('./helpers'),
- xml2js = require('xml2js'),
- parser = new xml2js.Parser();
+querystring = require('querystring'),
+helpers = require('./helpers'),
+xml2js = require('xml2js'),
+parser = new xml2js.Parser();
/**
* Prowl - send PUSH notification to you iDevice (iPhone, iPad)
*/
function Prowl( apiKey ){
- this.apiKey = apiKey || undefined;
+ this.apiKey = apiKey || undefined;
- this.endpoint = "https://api.prowlapp.com/publicapi/";
+ this.endpoint = "https://api.prowlapp.com/publicapi/";
+ this.timeout = 5000;
}
@@ -37,50 +38,53 @@ function Prowl( apiKey ){
*/
var push = function( event, application, options, callback ){
- if( !application ) throw new Error("Second parameter \'application\' is missing");
+ if( !application ) throw new Error("Second parameter \'application\' is missing");
- var me = this;
- if( typeof options == 'function' ){
- callback = options;
- options = {};
- }
+ var me = this;
+ if( typeof options == 'function' ){
+ callback = options;
+ options = {};
+ }
- var defaults = {
- description: null,
- providerkey: null,
- priority: 0,
- url: null
- }
+ var defaults = {
+ description: null,
+ providerkey: null,
+ priority: 0,
+ url: null
+ }
- var options = helpers.merge( defaults, options );
+ var options = helpers.merge( defaults, options );
- var query = { event: event,
- application: application,
- description: options.description,
- apikey: me.apiKey,
- providerkey: options.providerkey,
- priority: options.priority,
- url: options.url
- }
+ var query = { event: event,
+ application: application,
+ description: options.description,
+ apikey: me.apiKey,
+ providerkey: options.providerkey,
+ priority: options.priority,
+ url: options.url
+ }
- var url = me.endpoint + 'add?' + querystring.stringify( query );
+ var url = me.endpoint + 'add?' + querystring.stringify( query );
- request({
- method: 'POST',
- uri: url
- }, function( err, res ){
- parser.parseString(res.body, function (err, result) {
- var error = null;
- if( err ) error = err;
- if( !result['success'] ) error = err;
+ request({
+ method: 'POST',
+ uri: url,
+ timeout: me.timeout
+ }, function( err, res ){
+ parser.parseString(res.body, function (err, result) {
+ var error = !result['error'] ? null : {
+ code: result['error']['@']['code'],
+ message: result['error']['#']
+ }
+ var remaining = !result['success'] ? 0 : result['success']['@']['remaining']*1;
- callback( error, result['success']['@']['remaining']*1 );
- });
- })
+ callback( error, remaining );
+ });
+ })
}
Prowl.prototype = {
- push: push
+ push: push
}
module.exports = Prowl;

1 comment on commit fc03b73

@arnklint
Owner

This fixes #1

Please sign in to comment.