Skip to content
Browse files

fix get post

  • Loading branch information...
1 parent 13262bd commit 4eae54ae7e10c1f16c660353c401d4e5148ac8a1 @twilson63 committed Mar 18, 2012
Showing with 110 additions and 72 deletions.
  1. +48 −7 Cakefile
  2. +4 −4 bin/get
  3. +4 −4 bin/post
  4. +8 −13 index.coffee
  5. +17 −39 index.js
  6. +6 −5 package.json
  7. +8 −0 test/getTest.coffee
  8. +14 −0 test/getTest.js
  9. +1 −0 test/mocha.opts
View
55 Cakefile
@@ -1,9 +1,50 @@
-{ exec } = require 'child_process'
+fs = require 'fs'
+{print} = require 'util'
+{spawn, exec} = require 'child_process'
+
+# ANSI Terminal Colors
+bold = '\033[0;1m'
+green = '\033[0;32m'
+reset = '\033[0m'
+red = '\033[0;31m'
+
+log = (message, color, explanation) ->
+ console.log color + message + reset + ' ' + (explanation or '')
+
+build = (watch, callback) ->
+ if typeof watch is 'function'
+ callback = watch
+ watch = false
+ options = ['-c', '-b', '-o', '.', '.']
+ options.unshift '-w' if watch
+
+ coffee = spawn 'coffee', options
+ coffee.stdout.on 'data', (data) -> print data.toString()
+ coffee.stderr.on 'data', (data) -> log data.toString(), red
+ coffee.on 'exit', (status) -> callback?() if status is 0
+
+test = (callback) ->
+ options = []
+ test = spawn 'mocha', options
+ test.stdout.on 'data', (data) -> print data.toString()
+ test.stderr.on 'data', (data) -> log data.toString(), red
+ test.on 'exit', (status) -> callback?() if status is 0
+
+
+task 'docs', 'Generate annotated source code with Docco', ->
+ fs.readdir 'src', (err, contents) ->
+ files = ("src/#{file}" for file in contents when /\.coffee$/.test file)
+ docco = spawn 'docco', files
+ docco.stdout.on 'data', (data) -> print data.toString()
+ docco.stderr.on 'data', (data) -> log data.toString(), red
+ docco.on 'exit', (status) -> callback?() if status is 0
+
task 'build', ->
- exec 'coffee -c index.coffee', (err, stdout, stderr) ->
- console.log stderr.trim() if err
- console.log ':)'
-# task 'spec', ->
-# exec 'jasmine-node spec --coffee', (err, stdout, stderr) ->
-# console.log stdout.trim()
+ build -> log ":)", green
+
+task 'watch', ->
+ build true, -> log ":-)", green
+
+task 'spec', 'Mocha Tests', ->
+ build -> test -> log ":)", green
View
8 bin/get
@@ -1,7 +1,7 @@
#!/usr/bin/env node
-var path = require('path');
-var fs = require('fs');
-var root = path.join(path.dirname(fs.realpathSync(__filename)), '..');
+var path = require('path')
+, fs = require('fs')
+, gp = require(path.join(path.dirname(fs.realpathSync(__filename)), '..'))
-require(root + '/index').get();
+gp.get(process.argv[2]).pipe(process.stdout);
View
8 bin/post
@@ -1,7 +1,7 @@
#!/usr/bin/env node
-var path = require('path');
-var fs = require('fs');
-var root = path.join(path.dirname(fs.realpathSync(__filename)), '..');
+var path = require('path')
+, fs = require('fs')
+, gp = require(path.join(path.dirname(fs.realpathSync(__filename)), '..'))
-require(root + '/index').post();
+process.openStdin().pipe(gp.post(process.argv[2]).pipe(process.stdout))
View
21 index.coffee
@@ -1,14 +1,9 @@
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
+
+exports.get = (uri) ->
+ return throw(new Error('URL is Required')) unless uri?
+ request(uri, json: true)
+
+exports.post = (uri) ->
+ return thow(new Error('URL is Required')) unless uri?
+ request.post(uri, json: true)
View
56 index.js
@@ -1,39 +1,17 @@
-(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!');
- }
- return request.post({
- uri: process.argv[2],
- json: json
- }, function(err, resp, body) {
- return console.log(body);
- });
- }
- };
-}).call(this);
+var request;
+
+request = require('request');
+
+exports.get = function(uri) {
+ if (uri == null) throw new Error('URL is Required');
+ return request(uri, {
+ json: true
+ });
+};
+
+exports.post = function(uri) {
+ if (uri == null) return thow(new Error('URL is Required'));
+ return request.post(uri, {
+ json: true
+ });
+};
View
11 package.json
@@ -1,8 +1,8 @@
{
"name": "get-post",
- "description": "Simple Wrapper around Request to provide cli get and post",
- "tags" : ["http", "simple", "get", "post","cli"],
- "version": "0.0.4444",
+ "description": "Command Line Tools for transfering data via http with url syntax",
+ "tags" : ["http", "get", "post", "url"],
+ "version": "0.2.0",
"homepage": "http://github.com/twilson63/get-post",
"author": "Tom Wilson",
"private": false,
@@ -11,11 +11,12 @@
"url" : "git://github.com/twilson63/get-post.git"
},
"dependencies": {
- "request": ">= 2.1.x"
+ "request": "*"
},
"devDependencies": {
"coffee-script": ">= 1.x.x",
- "jasmine-node": ">= 0"
+ "mocha": "*",
+ "should": "*"
},
"bin": {
"get": "./bin/get",
View
8 test/getTest.coffee
@@ -0,0 +1,8 @@
+lib = require '../'
+
+describe 'get cmd', ->
+ it 'with no uri should return error', ->
+ try
+ stream = lib.get()
+ catch err
+ err.message.should.equal 'URL is Required'
View
14 test/getTest.js
@@ -0,0 +1,14 @@
+var lib;
+
+lib = require('../');
+
+describe('get cmd', function() {
+ return it('with no uri should return error', function() {
+ var stream;
+ try {
+ return stream = lib.get();
+ } catch (err) {
+ return err.message.should.equal('URL is Required');
+ }
+ });
+});
View
1 test/mocha.opts
@@ -0,0 +1 @@
+--require should

0 comments on commit 4eae54a

Please sign in to comment.
Something went wrong with that request. Please try again.