forked from heroku/node-js-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restlerTest.js
executable file
·55 lines (43 loc) · 1.7 KB
/
restlerTest.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
#!/usr/bin/env node
/*Pulls html from http://google.com and saves it to file temp.html
Utilizes restler library for get() call.
Utilizes commander library for .option functionality
*/
var sys = require('util'),
rest = require('restler'),
program = require('commander'),
fs = require('fs');
function saveHtmlToFile (url, filename) {
rest.get(url).on('complete', function(result) {
if (result instanceof Error) {
sys.puts('Error: ' + result.message);
this.retry(5000); // try again after 5 sec
} else {
fs.writeFileSync(filename, result);
}
});
}
/*program with the option -u or --url which takes a given URL and saves the file to the option -f or --file*/
if(require.main == module) {
program
//.option('-c, --checks <check_file>', 'Path to checks.json', clone(assertFileExists), CHECKSFILE_DEFAULT)
.option('-f, --file <html_file>', 'Path to index.html')//, clone(assertFileExists), HTMLFILE_DEFAULT)
.option('-u, --url <url>', 'URL')//todo:define url and add clone(assert) call and add URL_DEFAULT
.parse(process.argv);
//var checkJson = checkHtmlFile(program.file, program.checks);
//var outJson = JSON.stringify(checkJson, null, 4);
console.log(program.url);
if (program.url) saveHtmlToFile(program.url, program.file);
} else {
console.log("Else.");
//exports.checkHtmlFile = checkHtmlFile;
}
/*// 2013-07-17 This is the code from before I cut/pasted into the function.
rest.get('http://google.com').on('complete', function(result) {
if (result instanceof Error) {
sys.puts('Error: ' + result.message);
this.retry(5000); // try again after 5 sec
} else {
fs.writeFileSync("temp.html", result);
}
});*/