forked from siddii/angular-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
108 lines (94 loc) · 2.8 KB
/
utils.js
File metadata and controls
108 lines (94 loc) · 2.8 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
var config = require('./config');
var http = require('http');
var url = require('url');
var querystring = require('querystring');
var titleCase = function toTitleCase(str) {
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
var uuid = function uuidGenerator () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
};
var browserString = function browserString (config) {
return config.os + ' ' + config.os_version + ', ' +
titleCase(config.browser || config.device) + ' ' +
config.browser_version;
}
var objectSize = function objectSize (obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
var alertBrowserStack = function alertBrowserStack (subject, content, params, fn) {
var endpoint = config.alert_endpoint || "http://www.browserstack.com/automate/alert";
var urlObject = url.parse(endpoint);
var context = config.alert_context || "Runner alert";
console.log("[%s] %s", context, subject);
if (typeof fn !== 'function') {
if (typeof params === 'function') {
} else {
fn = function() {
process.exit(1);
};
}
}
if (typeof params !== 'object') {
params = {};
}
params.subject = subject;
params.content = content;
params.context = context;
var body = querystring.stringify(params);
var options = {
hostname: urlObject.hostname,
port: urlObject.port,
path: urlObject.path,
method: 'POST',
auth: config.username + ":" + config.key,
headers: {
'Content-Length': body.length
}
};
var callback = function (res) {
var response = "";
res.setEncoding( "utf8" );
res.on( "data", function( chunk ) {
response += chunk;
});
res.on( "end", function() {
if ( res.statusCode !== 200 ) {
var message;
if ( res.headers[ "content-type" ].indexOf( "json" ) !== -1 ) {
var resp = JSON.parse( response );
message = resp.message;
message += ' - ' + resp.errors.map(function(err){
return '`' + err.field + '`' + ' ' + err.code
}).join(', ')
} else {
message = response;
}
if ( !message && res.statusCode === 403 ) {
message = "Forbidden";
}
fn( new Error( message ) );
} else {
fn( null, JSON.parse( response ) );
}
});
};
var request = http.request(options, callback);
request.write(body);
request.end();
return request;
}
exports.titleCase = titleCase;
exports.uuid = uuid;
exports.browserString = browserString;
exports.objectSize = objectSize;
exports.alertBrowserStack = alertBrowserStack;