This repository has been archived by the owner on Jan 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
188 lines (153 loc) · 3.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Deps.
*/
var request = require('request');
/**
* Expose.
*/
module.exports = exports = Pushover;
/**
* Pushover API endpoint.
*
* Note: While Pushover supports XML requests and responses, I don't because
* XML sucks ass. Submit a pull request if you want need it.
*/
var endpoint = 'https://api.pushover.net/1/messages.json';
/**
* Pushover.
*
* Bare bones message.
*
* Send message with options.
*
* Pipe messages in later.
*
* @api public
* @param {Object} [opts]
* @param {String} msg
* @param {Function} [callback]
* @return {Stream}
*/
function Pushover(opts, callback) {
if (!(this instanceof Pushover)) return new Pushover(opts, callback);
// required
this.user = opts.user;
this.token = opts.token;
// optional
this.device = opts.device;
this.title = opts.title;
this.url = opts.url;
this.url_title = opts.url_title;
this.priority = opts.priority;
this.timestamp = opts.timestamp;
this.timeout = opts.timeout;
// this is only for testing purposes
this.endpoint = opts.endpoint || endpoint;
// outgoing messages
this.outbox = [];
// if we have a message, push it on now
if (type(opts.message) == 'string') {
this.push(opts.message);
}
// if we have a callback, send it now
if (type(callback) == 'function') {
this.end(callback);
}
}
/**
* Pushes a new message in the `outbox`.
*
* @api public
* @param {String} msg
* @return {Pushover}
*/
Pushover.prototype.push = function(msg) {
this.outbox.push(msg);
return this;
};
/**
* Iterates through the `outbox` sending each message. If the `callback`
* is defined, it will be invoked when the last response is received.
*
* @api public
* @param {Function} [callback]
* @return {Pushover}
*
* TODO: enforce limitations of message, url and url_title char length.
*/
Pushover.prototype.end = function(callback) {
callback = callback || function(){};
var outbox = this.outbox;
var count = outbox.length;
if (count === 0) {
callback(new Error('no messages'));
return this;
}
var pending = outbox.splice(0, count);
var failed = null;
pending.forEach(send, this);
function send(msg) {
if (failed) return;
var defaults = {
message: msg,
user: this.user,
token: this.token,
device: this.device,
title: this.title,
url: this.url,
url_title: this.url_title,
priority: this.priority,
timestamp: this.timestamp
};
var opts = null;
if (type(msg) == 'object') {
opts = override(defaults, msg);
opts.message = msg.message;
} else {
opts = defaults;
}
var req = request({
uri: this.endpoint,
method: 'POST',
timeout: this.timeout,
form: opts
}, respond);
function respond(err, res, body) {
if (err) return callback((failed = err));
try {
body = JSON.parse(body);
} catch (e) {
err = e;
}
if (0 === body.status) err = body.errors[0];
--count || callback(err || null, body);
}
}
return this;
};
/**
* Type testing utility.
*
* @api private
* @return {String}
*/
function type(value) {
if (value === null) return 'null';
var toString = Object.prototype.toString;
return toString.call(value).toLowerCase().match(/\s([a-z]+)\]$/)[1];
}
/**
* Shallowly overrides an object's properties.
*
* @api private
* @param {Object} a
* @param {Object} b
* @return {Object}
*/
function override(a, b) {
var dupe = {};
for (var key in a) {
dupe[key] = 'undefined' == typeof b[key]? a[key]: b[key];
}
return dupe;
}