summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-07-22 20:43:04 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-07-22 20:43:04 +0200
commit56edd9584cb964f75ccaeae4db9d0040b71ffe5f (patch)
tree7e333b514df3db8af9bbe65d4aee9d7f8ba70b78 /lib
parentc677e79c1227eedb656ad1a6830c38c64a4c0e4b (diff)
downloadcollection-json-explorer-56edd9584cb964f75ccaeae4db9d0040b71ffe5f.tar.gz
collection-json-explorer-56edd9584cb964f75ccaeae4db9d0040b71ffe5f.tar.bz2
collection-json-explorer-56edd9584cb964f75ccaeae4db9d0040b71ffe5f.tar.xz
collection-json-explorer-56edd9584cb964f75ccaeae4db9d0040b71ffe5f.zip
o Better layout of the data table.
Using a <table> instead of <dl> as the dl headings was collapsed by at least Chrome. o Adding mail logging.
Diffstat (limited to 'lib')
-rw-r--r--lib/explorer.js93
1 files changed, 93 insertions, 0 deletions
diff --git a/lib/explorer.js b/lib/explorer.js
new file mode 100644
index 0000000..57f5f64
--- /dev/null
+++ b/lib/explorer.js
@@ -0,0 +1,93 @@
+var http = require('http');
+var nodemailer = require('nodemailer');
+var url = require('url')
+
+var mailTransport
+if(process.env.SMTP_HOST) {
+ mailTransport = nodemailer.createTransport('SMTP', {
+ host: process.env.SMTP_HOST,
+ name: process.env.SMTP_NAME,
+ debug: process.env.SMTP_DEBUG === 'true',
+ auth: {
+ user: process.env.SMTP_AUTH_USER,
+ pass: process.env.SMTP_AUTH_PASS,
+ }
+ });
+}
+
+function fetchCollection(u, cb) {
+ var options = url.parse(u);
+ options.headers = {
+ accept: 'application/vnd.collection+json'
+ };
+ var start = new Date();
+ var req = http.get(options, function(res) {
+ res.setEncoding('utf8');
+ var body = '';
+ res.on('data', function (chunk) {
+ body += chunk;
+ });
+ res.on('end', function () {
+ var result = {
+ req: {
+ uri: options.uri,
+ headers: options.headers,
+ },
+ res: {
+ status: '',
+ statusCode: res.statusCode,
+ time: (new Date().getTime() - start.getTime()),
+ headers: res.headers,
+ },
+ body: body
+ };
+ sendMail(undefined, result);
+ cb(undefined, result);
+ });
+ }).on('error', function() {
+ cb('Unable to fetch ' + u);
+ sendMail('Unable to fetch ' + u);
+ });
+}
+
+function sendMail(err, result) {
+ if(!mailTransport) {
+ return;
+ }
+
+ if(err) {
+ }
+ else {
+ mailTransport.sendMail({
+ from: process.env.SMTP_FROM,
+ to: process.env.SMTP_TO,
+ subject: 'explorer',
+ attachments: [
+ {
+ fileName: 'request.json',
+ contents: JSON.stringify(result.req),
+ contentType: 'application/json',
+ },
+ {
+ fileName: 'response-meta.json',
+ contents: JSON.stringify(result.res),
+ contentType: 'application/json',
+ },
+ {
+ fileName: 'response-body',
+ contents: result.body,
+ contentType: result.res.headers['content-type']
+ }
+ ]
+ }, function(error, responseStatus) {
+ if(!error){
+ console.log('Message sent: ' + responseStatus.message);
+ }
+ else {
+ console.warn('Message not sent: ' + error);
+ }
+ });
+ }
+}
+
+module.exports.fetchCollection = fetchCollection;