From 7d051613810d34ed99ee6989d16667a1f4826b7a Mon Sep 17 00:00:00 2001 From: jywarren Date: Tue, 2 Jun 2015 17:09:39 -0700 Subject: [PATCH 1/3] adapted for stream input --- package.json | 3 ++- push | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 7a9a962..316043b 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "test": "push" }, "dependencies": { - "commander": "2.6.0" + "commander": "2.6.0", + "split": "1.0.0" }, "repository": { "type": "git", diff --git a/push b/push index 6a3ed0b..b1bd929 100755 --- a/push +++ b/push @@ -1,6 +1,7 @@ #!/usr/bin/env node var http = require('http') +var split = require('split'); var options = parseArgv(process.argv) @@ -8,6 +9,13 @@ var options = parseArgv(process.argv) if (options.hasOwnProperty('help')) { help() } +else if (options.hasOwnProperty('stream')) { + process.stdin.pipe(split()) + .on('data', function (line) { + options.data = line; + go() + }); +} else if (options.data) { go() } @@ -26,14 +34,15 @@ function go() { var path = '/input/' + options.public_key + '?private_key=' + options.private_key + '&' + options.field_name + '=' + options.data; - log(path) + log(options.url+path) http.get({ host: options.url, path: path}, function(response) { response.on('error', function() { console.log('error!') process.exit(1) }) - response.on('data', function() { + response.on('data', function(chunk) { console.log(response.statusCode) // 200 + console.log((chunk+"").replace(/<(?:.|\n)*?>/gm, '')) process.exit(0) }) }) @@ -47,6 +56,8 @@ function help() { console.log(' --field_name Example: temp') console.log(' --data [data] This is optional because you can also pipe data to this command.') console.log('') + console.log('Example: push --verbose --url data.sparkfun.com --public_key lzELagraWDiMMRra0V6Q --private_key XXXXXXXXXX --field_name temp') + console.log('') } // This is the standard function for parsing process.argv. It will return // an object that where `--argumentName` is a property name and the From 7b3efb2c04e2fb26bac355237f8b126b98d2471d Mon Sep 17 00:00:00 2001 From: jywarren Date: Wed, 3 Jun 2015 12:50:50 -0700 Subject: [PATCH 2/3] small style tweaks --- push | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/push b/push index b1bd929..bcd999d 100755 --- a/push +++ b/push @@ -1,10 +1,13 @@ #!/usr/bin/env node var http = require('http') -var split = require('split'); +var split = require('split') var options = parseArgv(process.argv) +process.stdin.on('error', function(e) { + console.log("error: "+e) +}) if (options.hasOwnProperty('help')) { help() @@ -12,9 +15,9 @@ if (options.hasOwnProperty('help')) { else if (options.hasOwnProperty('stream')) { process.stdin.pipe(split()) .on('data', function (line) { - options.data = line; + options.data = line go() - }); + }) } else if (options.data) { go() @@ -33,7 +36,7 @@ else { function go() { var path = '/input/' + options.public_key + '?private_key=' + options.private_key + - '&' + options.field_name + '=' + options.data; + '&' + options.field_name + '=' + options.data log(options.url+path) http.get({ host: options.url, path: path}, function(response) { response.on('error', function() { @@ -43,7 +46,7 @@ function go() { response.on('data', function(chunk) { console.log(response.statusCode) // 200 console.log((chunk+"").replace(/<(?:.|\n)*?>/gm, '')) - process.exit(0) + if (!options.hasOwnProperty('stream')) process.exit(0) }) }) } @@ -55,10 +58,12 @@ function help() { console.log(' --private_key Example: lz6d0j7KxPH1VVryqMw5') console.log(' --field_name Example: temp') console.log(' --data [data] This is optional because you can also pipe data to this command.') + console.log(' --stream Accepts streaming piped data, i.e. dosomething.sh | ./push --stream') console.log('') console.log('Example: push --verbose --url data.sparkfun.com --public_key lzELagraWDiMMRra0V6Q --private_key XXXXXXXXXX --field_name temp') console.log('') } + // This is the standard function for parsing process.argv. It will return // an object that where `--argumentName` is a property name and the // proceding argument is the value. If there is no proceeding value then From dd31f478b238253b218b5aa21884bb81c3ba44dc Mon Sep 17 00:00:00 2001 From: jywarren Date: Wed, 3 Jun 2015 15:23:36 -0700 Subject: [PATCH 3/3] json input; multiple measurements at a time --- push | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/push b/push index bcd999d..aeb9526 100755 --- a/push +++ b/push @@ -35,8 +35,16 @@ else { function go() { var path = '/input/' + options.public_key + - '?private_key=' + options.private_key + - '&' + options.field_name + '=' + options.data + '?private_key=' + options.private_key + if (params.hasOwnProperty('json')) { + options.data = JSON.parse(options.data) + log(options.data) + for (var i = 0;i< Object.keys(options.data).length;i++) { + log(Object.keys(options.data)[i]) + path = path + '&' + Object.keys(options.data)[i] + + '=' + options.data[Object.keys(options.data)[i]] + } + } else path += '&' + options.field_name + '=' + options.data log(options.url+path) http.get({ host: options.url, path: path}, function(response) { response.on('error', function() { @@ -58,7 +66,8 @@ function help() { console.log(' --private_key Example: lz6d0j7KxPH1VVryqMw5') console.log(' --field_name Example: temp') console.log(' --data [data] This is optional because you can also pipe data to this command.') - console.log(' --stream Accepts streaming piped data, i.e. dosomething.sh | ./push --stream') + console.log(' --stream Accept streaming piped data, i.e. dosomething.sh | ./push --stream') + console.log(' --json Accept json data, i.e. {first:1,second:2}') console.log('') console.log('Example: push --verbose --url data.sparkfun.com --public_key lzELagraWDiMMRra0V6Q --private_key XXXXXXXXXX --field_name temp') console.log('')