-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.js
207 lines (192 loc) · 5.79 KB
/
random.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
var request = require("request").defaults({ "jar": true })
var qs = require("querystring");
var statusCodes = require("http").STATUS_CODES;
var ua = require("random-ua");
var random = module.exports = {
"endpoint": "https://www.random.org",
"uaGenerator": ua.generate
};
// Copied and modified from underscore source
var defaults = function(obj) {
[].slice.call(arguments, 1).forEach(function(source) {
if (source) {
for (var prop in source) {
if (obj[prop] == null) obj[prop] = source[prop];
}
}
});
return obj;
};
// Copied and modified from underscore source
var extend = function(obj) {
[].slice.call(arguments, 1).forEach(function(source) {
if (source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
});
return obj;
};
var randomMappings = {
"number": "num",
"minimum": "min",
"maximum": "max",
"columns": "col",
"random": "rnd",
"length": "len",
"upper": "upperalpha",
"lower": "loweralpha"
};
var onOffMappings = {
"true": "on",
"false": "off"
};
var remapKeys = function(obj, mappings) {
var remapped = extend({}, obj);
for (var prop in mappings) {
if (obj[prop] != null) {
remapped[mappings[prop]] = obj[prop];
delete remapped[prop];
}
}
return remapped;
};
var remapValues = function(obj, mappings) {
var remapped = extend({}, obj);
for (var prop in remapped) {
key = JSON.stringify(obj[prop]);
if (mappings.hasOwnProperty(key)) {
remapped[prop] = mappings[key];
}
}
return remapped;
};
var parseNumbers = function(raw, opts) {
var data = raw.trim().split("\n").map(function(x) {
return x.trim().split("\t").map(function(y) {
return parseInt(y, opts.base);
});
});
if (opts.col === 1) data = data.map(function(x) { return x[0]; });
if (opts.num === 1) data = data[0];
return data;
};
var parseStrings = function(raw, opts) {
var data = raw.trim().split("\n");
return (opts.num === 1) ? data[0] : data;
};
var methods = {
"integers": {
"defaultOpts": {
"number": 1,
"minimum": 0,
"maximum": 10000,
"columns": 1,
"base": 10,
"random": "new"
},
"parse": parseNumbers
},
"numbers": "integers",
"sequences": {
"defaultOpts": {
"minimum": 1,
"maximum": 10,
"columns": 1,
"base": 10,
"random": "new"
},
"parse": parseNumbers
},
"sequence": "sequences",
"strings": {
"defaultOpts": {
"number": 1,
"length": 10,
"digits": true,
"upper": true,
"lower": true,
"unique": false,
"random": "new"
},
"parse": parseStrings
},
"string": "strings"
}
for (var method in methods) {
if (typeof methods[method] === "string") {
random[method] = random[methods[method]];
} else {
random[method] = (function(method) {
return function(opts, callback) {
if (typeof opts === "function" && callback == null) {
callback = opts;
opts = {};
} else if (opts == null) {
opts = {};
}
opts = defaults(opts, methods[method].defaultOpts);
opts = extend(opts, { "format": "plain" });
opts = remapKeys(opts, randomMappings);
opts = remapValues(opts, onOffMappings);
var url = random.endpoint + "/" + method + "/?" + qs.stringify(opts);
request.get({
"url": url,
"headers": {
"User-Agent": random.uaGenerator()
}
}, function(error, response, body) {
var data;
if (!error && response.statusCode !== 200) {
data = null;
if (response.statusCode === 503) {
errorMessage = "503: " + body.replace(/^Error: /g, "");;
} else {
errorMessage = response.statusCode + ": " + statusCodes[response.statusCode] + " (" + url + ")"
}
error = new Error(errorMessage);
} else {
data = methods[method].parse(body, opts);
}
callback && callback(error, data);
});
};
})(method);
}
}
random.quota = function(opts, callback) {
var method = "quota";
var defaultOpts = {
"ip": ""
};
if (typeof opts === "function" && callback == null) {
callback = opts;
opts = {};
} else if (opts == null) {
opts = {};
}
opts = defaults(opts, defaultOpts);
opts = extend(opts, { "format": "plain" });
var url = random.endpoint + "/" + method + "/?" + qs.stringify(opts);
request.get({
"url": url,
"headers": {
"User-Agent": random.uaGenerator()
}
}, function(error, response, body) {
var data;
if (!error && response.statusCode !== 200) {
data = null;
if (response.statusCode === 503) {
errorMessage = "503: " + body.replace(/^Error: /g, "");;
} else {
errorMessage = response.statusCode + ": " + statusCodes[response.statusCode] + " (" + url + ")"
}
error = new Error(errorMessage);
} else {
data = parseInt(body, 10);
}
callback && callback(error, data);
});
};