-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodevore.js
165 lines (128 loc) · 3.25 KB
/
nodevore.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
var connector = require('./lib/connector');
var methods = require('./lib/convoreapi').api;
var Connector = connector.Connector;
exports.setCredentials = connector.setCredentials;
exports.setHost = connector.setHost;
/**
* Determines if a string result contains an error
* @param {String} data string to check
* @private
*/
function _hasError(data) {
if ( data.indexOf('error') > -1 ) {
return true;
}
return false;
}
/**
* Tries to parse a string to JSON
* @param {String} data string to parse
* @returns {Object, String} JSON object or unparsed string if fail
* @private
*/
function _tryParse(data) {
var ret = null;
try {
ret = JSON.parse(data);
} catch (err){
ret = data;
}
return ret;
}
/**
* Returns a new connector
* @private
*/
function _conn() {
return new Connector();
}
/**
* Wraps a string into an error object
* @param {String} str error string to wrap
* @returns {Object} Error object
* @private
*/
function _err(str) {
return { error : str };
}
/**
* Calls a callback function with either error or data
* parameters.
* @param {Object} err Error object
* @param {Object} data Data object
* @param {Function} callback callback function
* @private
*/
function _errorOrData(err, data, callback) {
if(err) {
return callback(err);
}
if(_hasError(data)) {
return callback(_tryParse(data));
}
return callback(null, _tryParse(data));
}
/**
* Nodevore
* @param {String} user Convore username
* @param {String} pass Convore password
* @constructor
*/
function Nodevore(creds) {
if(creds.username && creds.password) {
connector.setCredentials(creds.username, creds.password);
}
};
function _replacePath(path, data) {
if(!data) {
return path;
}
var p = path;
for(var d in data) {
p = p.replace(':' + d, data[d])
}
return p;
}
//Populate methods
for(var m in methods) {
if(methods.hasOwnProperty(m)) {
var method = methods[m];
//Attach to proto
Nodevore.prototype[m] = (function(innermethod, innerm) {
return function(data, callback) {
var _callback = callback ? callback : data;
var queryparams = [];
var verb = innermethod.method || 'get';
if(!_callback) return;
//Validate input
if(innermethod.params) {
var imp = innermethod.params;
for(var p in imp) {
var thisp = imp[p];
if(thisp.required) {
if(!data[p]) {
return _callback(_err(innerm + ' requires parameter: ' + p));
}
}
if(thisp.postdata) {
var d = data[p] ? data[p] : '';
queryparams.push(p + '=' + d );
}
}
};
//Update path
var path = innermethod.params ? _replacePath(innermethod.path, data) : innermethod.path;
//Build query
var query = queryparams.length > 0 ? queryparams.join('&') : '';
//Call remote
_conn()[verb](path, query , function(err,data) {
_errorOrData( err, data, _callback);
});
}
})(method, m);
}
}
Nodevore.prototype.hangup = function() {
connector.liveStream(false);
}
exports.Nodevore = Nodevore;