-
Notifications
You must be signed in to change notification settings - Fork 6
/
module.js
91 lines (73 loc) · 1.93 KB
/
module.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
// This script defines objects loaded into the context of every Gonk module
console = {
"log": function() { return _console_log.apply(null, arguments); }
}
gonk = {
"respond": function() { return _robot_respond.apply(null, arguments); },
"hear" : function() { return _robot_hear.apply(null, arguments); },
}
function HttpClient (url) {
this._url = url;
this._querystr = "";
this._headers = {};
}
HttpClient.prototype.post = function() {
uri = encodeURI(this._url);
body = _httpclient_post(uri, this._headers, this._querystr);
return function(cb) {
cb(null, null, body);
}
}
HttpClient.prototype.get = function() {
uri = encodeURI(this._url) + this._querystr;
body = _httpclient_get(uri, this._headers);
return function(cb) {
cb(null, null, body);
}
}
HttpClient.prototype.query = function(q) {
prefix = "?";
for (var prop in q) {
this._querystr += prefix;
this._querystr += prop + "=" + encodeURIComponent(q[prop]);
prefix = "&";
}
return this;
}
HttpClient.prototype.headers = function(headers) {
this._headers = headers;
return this;
}
HttpClient.prototype.header = function() {
this._headers[arguments[0]] = arguments[1];
return this;
}
response = {
"send" : function() {
var args = [].slice.call(arguments);
args.push(this.target);
_msg_send.apply(null, args);
},
"random" : function(items) { return items[Math.floor(Math.random()*items.length)] },
"http" : function(url) { return new HttpClient(url); }
}
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
function clone(obj){
if(obj == null || typeof(obj) != 'object')
return obj;
var temp = obj.constructor(); // changed
for(var key in obj)
temp[key] = clone(obj[key]);
return temp;
}
module = {}; // Module code is loaded into module.exports