Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update get-post cli to take pipes

  • Loading branch information...
commit 111182e9ea65cc06a062606426946652dd1167a0 1 parent 13262bd
@twilson63 authored
View
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Tom Wilson
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
13 bin/get
@@ -4,4 +4,15 @@ var path = require('path');
var fs = require('fs');
var root = path.join(path.dirname(fs.realpathSync(__filename)), '..');
-require(root + '/index').get();
+// # GET Command
+//
+// Usage:
+//
+// ``` sh
+// get "http://twitter.com/search.json?q=nodejs"
+// ```
+require(root + '/index').get(process.argv[2], function(err, data) {
+ if (err === null) {
+ console.log(data);
+ }
+});
View
18 bin/post
@@ -3,5 +3,21 @@
var path = require('path');
var fs = require('fs');
var root = path.join(path.dirname(fs.realpathSync(__filename)), '..');
+var post = require(root + '/index').post;
-require(root + '/index').post();
+// Get JSON Document via stdin pipe
+var buffer = "";
+
+var stdin = process.openStdin();
+stdin.setEncoding('utf8');
+stdin.on('data', function (chunk) {
+ buffer += chunk;
+});
+
+stdin.on('end', function () {
+ post(process.argv[2], buffer, function(err, data) {
+ if (err === null) {
+ console.log(data);
+ }
+ });
+});
View
121 index.coffee
@@ -1,14 +1,109 @@
request = require 'request'
-module.exports =
- get: ->
- return console.log 'get [uri] - uri is required ex. http://twitter.com/search.json?q=nodejs' unless process.argv[2]
- return console.log 'get [uri] - uri is not valid' unless process.argv[2].match /https?\:\/\/(.*)(\.|:)(.*)/
- request uri: process.argv[2], json: true, (err, resp, body) ->
- console.log body
- post: ->
- return console.log 'post [uri] [json] - required!' unless process.argv[2]
- return console.log 'post [*uri] [json] - required!' unless process.argv[3]
- json = JSON.stringify(JSON.parse(process.argv[3]))
- return console.log 'valid json required!' unless json?
- request.post uri: process.argv[2], json: json, (err, resp, body) ->
- console.log body
+module.exports = ( ->
+ # ANSI Terminal Colors
+ BOLD = '\033[0;1m'
+ GREEN = '\033[0;32m'
+ RESET = '\033[0m'
+ RED = '\033[0;31m'
+ ORANGE = '\033[33;40m'
+
+ VERSION = '0.1.0'
+
+ HEADER = """
+ ======================
+ #{GREEN}Get-Post#{RESET} Version #{ORANGE}#{VERSION}#{RESET}
+ ======================
+ """
+ GET_HELP = """
+ command: #{GREEN}get#{RESET}
+ params:
+ #{ORANGE}uri = The Resource Endpoint you are trying to access. #{RESET}
+
+ example:
+ #{GREEN}get#{RESET} #{ORANGE}http://twitter.com/search.json?q=nodejs#{RESET}
+
+ """
+ GET_INVALID = """
+ command: #{GREEN}get#{RESET}
+ #{RED}Invalid URI:#{RESET} #{RED}Unable to reconize => #{RESET}
+ """
+
+ POST_HELP = """
+ command: #{GREEN}post#{RESET}
+ params:
+ #{ORANGE}uri = The Resource Endpoint you are trying to access. #{RESET}
+
+ example:
+ echo '{"HELLO":"WORLD"}' | #{GREEN}post#{RESET} #{ORANGE}http://echo-json.heroku.com#{RESET}
+
+ """
+ POST_INVALID = """
+ command: #{GREEN}post#{RESET}
+ #{RED}Invalid URI:#{RESET} #{RED}Unable to reconize => #{RESET}
+ """
+
+ _parseJson = (json, cb) ->
+ try
+ cb null, JSON.stringify(json)
+ catch error
+ console.log error
+ cb(new Error("#{RED}Unable to parse JSON#{RESET}"), null)
+
+ _post = (uri, json, cb) ->
+ request.post { uri: uri, json: json }, cb
+
+
+ {
+ # ## get [uri]
+ #
+ # Description:
+ #
+ # performs a HTTP GET REQUEST on the uri passed as a parameter
+ # with a content-type of application/json and returns the response
+ # via stdout for piping.
+
+ # Usage:
+ #
+ # { get } = require "get-post"
+ # get "http://twitter.com/search.json?q=coffeescript", (err, data) ->
+ # console.log data
+ #
+ #
+ get: (uri, cb) ->
+ unless uri?
+ cb null, HEADER + GET_HELP
+ return true
+
+ unless /https?\:\/\/(.*)(\.|:)(.*)/.test uri
+ cb null, HEADER + GET_INVALID + "#{ORANGE}#{uri}#{RESET}"
+ return true
+
+ request uri: uri, json: true, (err, resp, body) ->
+ _parseJson body, (err, json) ->
+ if err?
+ cb null, HEADER + err
+ else
+ cb null, json
+
+ # # POST METHOD
+ #
+ # params
+ # uri: A HTTP Endpoint to post json data
+ #
+ post: (uri, json_data, cb) ->
+ unless uri?
+ cb null, HEADER + POST_HELP
+ return true
+
+ unless /https?\:\/\/(.*)(\.|:)(.*)/.test uri
+ cb null, HEADER + POST_INVALID + "#{ORANGE}#{uri}#{RESET}"
+ return true
+
+ _parseJson json_data, (err, json) ->
+ if err?
+ cb null, HEADER + err
+ else
+ _post uri, json, (err, status, body) -> cb null, body
+
+ }
+)()
View
103 index.js
@@ -1,39 +1,78 @@
(function() {
var request;
+
request = require('request');
- module.exports = {
- get: function() {
- if (!process.argv[2]) {
- return console.log('get [uri] - uri is required ex. http://twitter.com/search.json?q=nodejs');
- }
- if (!process.argv[2].match(/https?\:\/\/(.*)(\.|:)(.*)/)) {
- return console.log('get [uri] - uri is not valid');
- }
- return request({
- uri: process.argv[2],
- json: true
- }, function(err, resp, body) {
- return console.log(body);
- });
- },
- post: function() {
- var json;
- if (!process.argv[2]) {
- return console.log('post [uri] [json] - required!');
- }
- if (!process.argv[3]) {
- return console.log('post [*uri] [json] - required!');
- }
- json = JSON.stringify(JSON.parse(process.argv[3]));
- if (json == null) {
- return console.log('valid json required!');
+
+ module.exports = (function() {
+ var BOLD, GET_HELP, GET_INVALID, GREEN, HEADER, ORANGE, POST_HELP, POST_INVALID, RED, RESET, VERSION, _parseJson, _post;
+ BOLD = '\033[0;1m';
+ GREEN = '\033[0;32m';
+ RESET = '\033[0m';
+ RED = '\033[0;31m';
+ ORANGE = '\033[33;40m';
+ VERSION = '0.1.0';
+ HEADER = "======================\n" + GREEN + "Get-Post" + RESET + " Version " + ORANGE + VERSION + RESET + "\n======================";
+ GET_HELP = " command: " + GREEN + "get" + RESET + "\n params:\n " + ORANGE + "uri = The Resource Endpoint you are trying to access. " + RESET + "\n\n example:\n " + GREEN + "get" + RESET + " " + ORANGE + "http://twitter.com/search.json?q=nodejs" + RESET + "\n";
+ GET_INVALID = "command: " + GREEN + "get" + RESET + "\n" + RED + "Invalid URI:" + RESET + " " + RED + "Unable to reconize => " + RESET + " ";
+ POST_HELP = " command: " + GREEN + "post" + RESET + "\n params:\n " + ORANGE + "uri = The Resource Endpoint you are trying to access. " + RESET + "\n\n example:\n echo '{\"HELLO\":\"WORLD\"}' | " + GREEN + "post" + RESET + " " + ORANGE + "http://echo-json.heroku.com" + RESET + "\n";
+ POST_INVALID = "command: " + GREEN + "post" + RESET + "\n" + RED + "Invalid URI:" + RESET + " " + RED + "Unable to reconize => " + RESET + " ";
+ _parseJson = function(json, cb) {
+ try {
+ return cb(null, JSON.stringify(json));
+ } catch (error) {
+ console.log(error);
+ return cb(new Error("" + RED + "Unable to parse JSON" + RESET), null);
}
+ };
+ _post = function(uri, json, cb) {
return request.post({
- uri: process.argv[2],
+ uri: uri,
json: json
- }, function(err, resp, body) {
- return console.log(body);
- });
- }
- };
+ }, cb);
+ };
+ return {
+ get: function(uri, cb) {
+ if (uri == null) {
+ cb(null, HEADER + GET_HELP);
+ return true;
+ }
+ if (!/https?\:\/\/(.*)(\.|:)(.*)/.test(uri)) {
+ cb(null, HEADER + GET_INVALID + ("" + ORANGE + uri + RESET));
+ return true;
+ }
+ return request({
+ uri: uri,
+ json: true
+ }, function(err, resp, body) {
+ return _parseJson(body, function(err, json) {
+ if (err != null) {
+ return cb(null, HEADER + err);
+ } else {
+ return cb(null, json);
+ }
+ });
+ });
+ },
+ post: function(uri, json_data, cb) {
+ if (uri == null) {
+ cb(null, HEADER + POST_HELP);
+ return true;
+ }
+ if (!/https?\:\/\/(.*)(\.|:)(.*)/.test(uri)) {
+ cb(null, HEADER + POST_INVALID + ("" + ORANGE + uri + RESET));
+ return true;
+ }
+ return _parseJson(json_data, function(err, json) {
+ if (err != null) {
+ return cb(null, HEADER + err);
+ } else {
+ return _post(uri, json, function(err, status, body) {
+ return cb(null, body);
+ });
+ }
+ });
+ }
+ };
+ })();
+
}).call(this);
View
7 package.json
@@ -2,7 +2,7 @@
"name": "get-post",
"description": "Simple Wrapper around Request to provide cli get and post",
"tags" : ["http", "simple", "get", "post","cli"],
- "version": "0.0.4444",
+ "version": "0.1.0",
"homepage": "http://github.com/twilson63/get-post",
"author": "Tom Wilson",
"private": false,
@@ -14,8 +14,9 @@
"request": ">= 2.1.x"
},
"devDependencies": {
- "coffee-script": ">= 1.x.x",
- "jasmine-node": ">= 0"
+ "coffee-script": "~ 1.2.x",
+ "mocha": "*",
+ "should": "*"
},
"bin": {
"get": "./bin/get",
View
47 readme.md
@@ -1,30 +1,49 @@
-# get-post json cli
+GET-POST
+========
-Simple NodeJs json http request command-line client. A cli
-wrapper around Request by Mikeal Rogers. Everything gets
-or posts via content-type application/json.
+Two CLI applications that connect to http servers using json
-# Installation
+## Installation
-```
+``` sh
npm install get-post -g
```
-# Usage
+## GET
-Do a simple get as 'application/json' :
+### Usage
+
+``` sh
+get http://twitter.com/search.json?q=nodejs
-```
-get "http://twitter.com/search.json?q=nodejs"
```
-Do a simple json post:
+## POST
+
+### Usage
+
+
+``` sh
+echo '{"Hello": "World"} | post http://echo-json.heroku.com
```
-post http://echo-json.heroku.com '{"hello":"world"}'
-```
-# Thanks
+## FAQ
+
+* Why?
+
+Because with curl I just got tired of typeing -H 'Content-Type: application/json'
+
+## Thanks
* Mikeal Rogers for Request
* Ryan Dahl for Nodejs
+
+## License
+
+See LICENSE
+
+## Contribute
+
+Contributions Welcome!
+
View
7 test/get_spec.coffee
@@ -0,0 +1,7 @@
+{ get } = require '../index'
+describe 'GET CLI', ->
+ it 'should show name', (done) ->
+ # process.stdout.on "data", (chunked) ->
+ # chunked.should.eql("Hello World")
+ # done()
+ get("Hello World")
View
1  test/mocha.opts
@@ -0,0 +1 @@
+--require should
View
0  test/post_spec.coffee
No changes.
Please sign in to comment.
Something went wrong with that request. Please try again.