forked from DemocracyOS/notifier-client
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
131 lines (108 loc) · 2.79 KB
/
index.js
File metadata and controls
131 lines (108 loc) · 2.79 KB
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
/**
* Module dependencies.
*/
var object = require('object-component');
var request = require('superagent');
var log = require('debug')('notifier-client');
/**
* Expose `NotifierClient` constructor.
*/
module.exports = NotifierClient;
var defaults = {
host: 'localhost',
path: '/api/events',
port: 80,
protocol: 'http',
token: null
};
/**
* Creates a Client instance. Takes an object with options.
*
* @param {Object} options for configuring the client instance.
* - host {String} defaults to `localhost`.
* - port {String} defaults to '80'.
* - protocol {String} defautls to `http`.
* - path {String} api path - defaults to `/api`.
* - token {String} api token - defaults to `null`.
* @return {NotifierClient} `NotifierClient` instance
* @api public
*/
function NotifierClient (options) {
if (!(this instanceof NotifierClient)) {
return new NotifierClient(options);
}
this.options = object.merge({}, defaults);
this.options = object.merge(this.options, options || {});
}
/**
* Sends notification
*
* @param {Object} or {String} either event with data or just event name
* @param {Function} optional callback
* @return {NotifierClient} `NotifierClient` instance
* @api public
*/
NotifierClient.prototype.notify = function(event, callback) {
this.event = {};
if(typeof event === 'object') {
this.event = event;
this.send(callback);
} else {
this.event.event = event;
}
return this;
};
/**
* Initialize user field of event
*
* @param {String} recepient/user id
* @return {NotifierClient} `NotifierClient` instance
* @api public
*/
NotifierClient.prototype.to = function(recipient) {
this.event.user = recipient;
return this;
};
/**
* Initialize data field of event
*
* @param {Object} event data
* @return {NotifierClient} `NotifierClient` instance
* @api public
*/
NotifierClient.prototype.withData = function(data) {
this.event.data = data;
return this;
};
/**
* Sends notification request
*
* @param {Function} optional callback
* @api public
*/
NotifierClient.prototype.send = function(callback) {
callback = callback || function () {};
request
.post(this._buildUrl())
.set('Accept', 'application/json')
.send(this.event)
.end(function (err, res) {
if (err) {
log('Unexpected error when sending event %j', this.event);
return callback(err);
}
if (res.body.error || res.statusCode > 201) {
log('Error for event %j: %s', this.event, res.body.error);
return callback(res.body);
}
callback(null, res.body);
});
};
/**
* Builds request URL
*
* @api private
*/
NotifierClient.prototype._buildUrl = function() {
return this.options.protocol + '://' + this.options.host + ':' + this.options.port + this.options.path + '?access_token=' + this.options.token;
};