From 78166bf449265e9b0e5021cc2d79d99e724dc37d Mon Sep 17 00:00:00 2001 From: Max Ogden Date: Fri, 1 Jun 2012 12:40:03 -0700 Subject: [PATCH 01/10] rewrite using request --- Changelog.md | 2 ++ Readme.md | 77 ++++++++++++++++++---------------------- index.js | 43 +++++++++++++++++++++- lib/gist.js | 36 ------------------- package.json | 13 +++---- test/gist.test.js | 42 ---------------------- test/integration.test.js | 13 ------- 7 files changed, 82 insertions(+), 144 deletions(-) delete mode 100644 lib/gist.js delete mode 100644 test/gist.test.js delete mode 100644 test/integration.test.js diff --git a/Changelog.md b/Changelog.md index 306c8e7..d676831 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,5 @@ +### 0.3.0 +- rewrite using the request module ### 0.2.0 - Created a test script to not depend on global nodeunit - Ensure Node 0.6.0 compatibility diff --git a/Readme.md b/Readme.md index 7eda6fc..7d25446 100644 --- a/Readme.md +++ b/Readme.md @@ -1,55 +1,46 @@ -[![Build Status](https://secure.travis-ci.org/emerleite/node-gist.png)](http://travis-ci.org/emerleite/node-gist) - Node.js Gist client =================== -Gist API client for Node.JS - -Dependencies ------------- - -### Runtime -* Node 0.4.x+ +Gist API v3 client for Node.JS -### Development/Tests -* mocha - *should.js - -Instalation ------------ -> npm install gist - -Usuage +Usage ------ - var gist = require('gist'); - - gist.create('your gist content', function (url) { - console.log(url); //prints created gist url - }); - -Running tests -------------- - -### Unit -$ node_modules/mocha/bin/mocha test/gist.test.js - -### Integration -$ node_modules/mocha/bin/mocha test/integration.test.js - -### All tests (3 ways) -$ npm test -$ mocha (installed global) -$ node_modules/mocha/bin/mocha - -Note: Integration test creates real gist. Please, be carreful with this test to not flood gist. + var gist = require('gist')(validOAuthToken); + + // get all your gists (or all public if you didnt specify a token) + gist.gists(function(err, resp, json) { + console.log(err, json) + }) + + // get all public gists for some user + gist.gists('maxogden', function(err, resp, json) { + console.log(err, json) + }) + + // get a gist by id + gist.gist('2698151', function(err, resp, json) { + console.log(err, json) + }) + + // creating a new gist + var newGist = { + "description": "the description for this gist", + "public": false, + "files": { + "file1.txt": { + "content": "String file contents" + } + } + } + gist(validOauthToken).create(newGist, function(err, resp, json) { + console.log(err, json) + }) -To-Do ------ - () Author ------ +* Max Ogden (@maxogden) -* Emerson Macedo () +this library was forked from Emerson Macedo () and entirely rewritten License: -------- diff --git a/index.js b/index.js index 52fcb7f..7468d65 100644 --- a/index.js +++ b/index.js @@ -1 +1,42 @@ -module.exports = require('./lib/gist'); +var request = require('request').defaults({json: true}) +var qs = require('querystring') +var _ = require('underscore') + +function Gist(token) { + this.api = "https://api.github.com/" + this.token = token +} + +Gist.prototype.create = function (content, cb) { + if (!content) return cb("must specify id") + var url = this.api + 'gists' + return this._request({url: url, json: content, method: "POST"}, cb) +}; + +Gist.prototype.gists = function (user, cb) { + if (typeof user === 'function') { + cb = user + user = false + } + var url = this.api + (user ? 'users/' + user + '/gists' : 'gists') + return this._request({url: url}, cb) +} + +Gist.prototype.gist = function (id, cb) { + if (!id) return cb("must specify id") + var url = this.api + 'gists/' + id + return this._request({url: url}, cb) +} + +Gist.prototype._request = function(options, cb) { + if (this.token) { + if (!options.headers) options.headers = {} + options.headers["Authorization"] = "token " + this.token + } + if (!cb) cb = function() {} + return request(options, cb) +} + +module.exports = function(options) { + return new Gist(options) +} \ No newline at end of file diff --git a/lib/gist.js b/lib/gist.js deleted file mode 100644 index b966531..0000000 --- a/lib/gist.js +++ /dev/null @@ -1,36 +0,0 @@ -var http = require('http'); - -var url = function (response) { - var gist = JSON.parse(response).gists[0]; - return 'http://gist.github.com/' + gist.repo; -}; - -exports.url = url; -exports.create = function (content, callback) { - var body = 'files[newfile]=' + content; - - var options = { - host: 'gist.github.com', - port: 80, - path: '/api/v1/json/new', - method: 'POST', - headers: { - 'host': 'gist.github.com', - 'Content-length': body.length, - 'Content-Type': 'application/x-www-form-urlencoded' - } - }; - - var req = http.request(options, function(res) { - res.body = ''; - res.setEncoding('utf8'); - res.on('data', function (chunk) { - res.body += chunk; - }); - res.on('end', function () { - callback.apply(null, [url(res.body)]); - }); - }); - - req.end(body); -}; diff --git a/package.json b/package.json index 9647ec2..b3da2ad 100644 --- a/package.json +++ b/package.json @@ -2,16 +2,11 @@ "name" : "gist", "description" : "Gist api client for node.js", "keywords" : [ "gist", "github", "api", "package.json" ], - "version" : "0.2.1", - "author" : "Emerson Macedo ", + "version" : "0.3.0", + "author" : "Max Ogden ", "repository" : { "type" : "git", - "url" : "git://github.com/emerleite/node-gist.git" }, - "devDependencies" : { - "mocha" : "0.7.0", - "should" : "0.4.2" - }, + "url" : "git://github.com/maxogden/node-gist.git" }, "main": "index", - "scripts" : {"test" : "node_modules/mocha/bin/mocha -R spec"}, - "engines" : { "node" : ">=0.4.0" } + "engines" : { "node" : ">=0.6.0" } } diff --git a/test/gist.test.js b/test/gist.test.js deleted file mode 100644 index be52b69..0000000 --- a/test/gist.test.js +++ /dev/null @@ -1,42 +0,0 @@ -var should = require('should') - , gist = require('../lib/gist') - , http = require('http') - , EventEmitter = require('events').EventEmitter; - -var orig_request = http.request; - -describe('unit', function() { - it('should return the new gist url', function() { - var gistJSONResponse = '{"gists":[{"public":true,"repo":"854033","created_at":"2011/03/03 17:57:22 -0800","files":["file1.ab"],"description":null}]}'; - var url = gist.url(gistJSONResponse); - url.should.be.equal('http://gist.github.com/854033'); - }); - - it('should create gist and return the correct url', function(done) { - var gistJSONResponse = '{"gists":[{"public":true,"repo":"123456","created_at":"2011/03/03 17:57:22 -0800","files":["file1.ab"],"description":null}]}'; - - stubHttpRequest(gistJSONResponse); - gist.create('teste 123456', function (url) { - url.should.be.equal('http://gist.github.com/123456'); - done(); - }); - }); - - afterEach(function() { - http.request = orig_request; - }); -}); - -var stubHttpRequest = function(data) { - http.request = function(options, callback) { - var requestStubEmitter = new EventEmitter(); - requestStubEmitter.setEncoding = function () {}; - return { - end: function(body) { - callback(requestStubEmitter); - requestStubEmitter.emit('data', data); - requestStubEmitter.emit('end'); - } - }; - }; -}; diff --git a/test/integration.test.js b/test/integration.test.js deleted file mode 100644 index 655c42e..0000000 --- a/test/integration.test.js +++ /dev/null @@ -1,13 +0,0 @@ -var should = require('should') - , gist = require('../lib/gist'); - -describe('integration', function() { - - it('should create gist and return the gist url', function(done) { - gist.create('teste new gist', function (url) { - url.should.match(/^http:\/\/gist\.github\.com\/\d+$/) - done(); - }); - }); - -}); From 82bb0f69b6e4e1b69dc2026401b58a85fc5c0fde Mon Sep 17 00:00:00 2001 From: Eric West Date: Fri, 17 Aug 2012 04:32:34 -0500 Subject: [PATCH 02/10] Adds request and underscore to list of dependencies --- package.json | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index b3da2ad..d646f9c 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,24 @@ -{ - "name" : "gist", - "description" : "Gist api client for node.js", - "keywords" : [ "gist", "github", "api", "package.json" ], - "version" : "0.3.0", - "author" : "Max Ogden ", - "repository" : { "type" : "git", - "url" : "git://github.com/maxogden/node-gist.git" }, +{ + "name": "gist", + "description": "Gist api client for node.js", + "keywords": [ + "gist", + "github", + "api", + "package.json" + ], + "version": "0.3.0", + "author": "Max Ogden ", + "repository": { + "type": "git", + "url": "git://github.com/maxogden/node-gist.git" + }, "main": "index", - "engines" : { "node" : ">=0.6.0" } + "engines": { + "node": ">=0.6.0" + }, + "dependencies": { + "request": "~2.10.0", + "underscore": "~1.3.3" + } } - From d5a3f14c413d1f4807a6fd1e8db352a781a05abf Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 26 Oct 2012 11:56:51 -0600 Subject: [PATCH 03/10] added a cli tool --- Readme.md | 97 ++++++++++++++++++++++++++++++++-------------------- bin/gist.js | 87 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 5 ++- 3 files changed, 151 insertions(+), 38 deletions(-) create mode 100644 bin/gist.js diff --git a/Readme.md b/Readme.md index 7d25446..802c141 100644 --- a/Readme.md +++ b/Readme.md @@ -1,49 +1,72 @@ -Node.js Gist client -=================== +## Node.js Gist client + Gist API v3 client for Node.JS -Usage ------- - var gist = require('gist')(validOAuthToken); - - // get all your gists (or all public if you didnt specify a token) - gist.gists(function(err, resp, json) { - console.log(err, json) - }) - - // get all public gists for some user - gist.gists('maxogden', function(err, resp, json) { - console.log(err, json) - }) - - // get a gist by id - gist.gist('2698151', function(err, resp, json) { - console.log(err, json) - }) - - // creating a new gist - var newGist = { - "description": "the description for this gist", - "public": false, - "files": { - "file1.txt": { - "content": "String file contents" - } - } +## Installation + +For use in your modules (adds to package.json automatically) + + npm install -S gist + +For the commandline gist + + npm install -g gist + +## Usage + +### Commandline + + gist + + echo "Hello World!" > ./hello.txt + gist ./hello.txt + +### API + + * `gist.gists([username], fn)` + * `gist.gist(id, fn)` + * `gist([validOauthToken]).create(newGist, fn)` + +```javascript +var gist = require('gist')(validOAuthToken); + +// get all your gists (or all public if you didnt specify a token) +gist.gists(function(err, resp, json) { + console.log(err, json) +}) + +// get all public gists for some user +gist.gists('maxogden', function(err, resp, json) { + console.log(err, json) +}) + +// get a gist by id +gist.gist('2698151', function(err, resp, json) { + console.log(err, json) +}) + +// creating a new gist +var newGist = { + "description": "the description for this gist", + "public": false, + "files": { + "file1.txt": { + "content": "String file contents" } - gist(validOauthToken).create(newGist, function(err, resp, json) { - console.log(err, json) - }) + } +} +gist(validOauthToken).create(newGist, function(err, resp, json) { + console.log(err, json) +}) +``` +## Author -Author ------- * Max Ogden (@maxogden) this library was forked from Emerson Macedo () and entirely rewritten -License: --------- +## License (The MIT License) diff --git a/bin/gist.js b/bin/gist.js new file mode 100644 index 0000000..c48618f --- /dev/null +++ b/bin/gist.js @@ -0,0 +1,87 @@ +#!/usr/bin/env node +/*jshint strict:true node:true es5:true onevar:true laxcomma:true laxbreak:true eqeqeq:true immed:true latedef:true*/ +(function () { + "use strict"; + + var gist = require('../') + , fs = require('fs') + , filename = process.argv[2] + , desc = process.argv[3] + ; + + function usage() { + console.log('Usage: gist '); + } + + if (!filename) { + usage(); + return; + } + + fs.readFile(filename, 'utf8', function (err, data) { + var meta + , name = filename.replace(/.*\//, '') + ; + + if (err) { + usage(); + return; + } + + meta = { + "description": desc + , "public": true + , "files": {} + }; + meta.files[name] = data; + + gist().create(meta, function (err, resp, json) { + //console.log(JSON.stringify(json, null, ' ')); + console.log('[gist]', json.html_url); + console.log('[raw]', json.files[name].raw_url); + console.log('[git]', json.git_push_url); + }); + }); + + /* +{ + "git_push_url": "git@gist.github.com:3960244.git", + "user": null, + "html_url": "https://gist.github.com/3960244", + "history": [ + { + "user": null, + "version": "10564662c34f251be04ac7936758e8ff6c6df4a6", + "committed_at": "2012-10-26T17:51:27Z", + "change_status": { + "additions": 1, + "total": 1, + "deletions": 0 + }, + "url": "https://api.github.com/gists/3960244/10564662c34f251be04ac7936758e8ff6c6df4a6" + } + ], + "comments": 0, + "created_at": "2012-10-26T17:51:27Z", + "description": null, + "public": true, + "forks": [], + "updated_at": "2012-10-26T17:51:27Z", + "id": "3960244", + "url": "https://api.github.com/gists/3960244", + "files": { + "index.js": { + "type": "application/javascript", + "filename": "index.js", + "raw_url": "https://gist.github.com/raw/3960244/6b584e8ece562ebffc15d38808cd6b98fc3d97ea/index.js", + "size": 7, + "content": "content", + "language": "JavaScript" + } + }, + "git_pull_url": "git://gist.github.com/3960244.git" +} +*/ + + +}()); diff --git a/package.json b/package.json index d646f9c..6354f28 100644 --- a/package.json +++ b/package.json @@ -7,13 +7,16 @@ "api", "package.json" ], - "version": "0.3.0", + "version": "1.0.0", "author": "Max Ogden ", "repository": { "type": "git", "url": "git://github.com/maxogden/node-gist.git" }, "main": "index", + "bin": { + "gist": "bin/gist.js" + }, "engines": { "node": ">=0.6.0" }, From 8128263716ad8a0f8a3d12970dadaeb259d289f9 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 26 Oct 2012 12:07:07 -0600 Subject: [PATCH 04/10] instructions on installing the new version --- Readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 802c141..c428e3a 100644 --- a/Readme.md +++ b/Readme.md @@ -6,7 +6,8 @@ Gist API v3 client for Node.JS For use in your modules (adds to package.json automatically) - npm install -S gist + #npm install -S gist # old version + npm install -g git://github.com/coolaj86/node-gist.git For the commandline gist From f4a968e4e200f977df489e753ace840f38e6e92c Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 26 Oct 2012 12:12:07 -0600 Subject: [PATCH 05/10] added alternate installation methods --- Readme.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index c428e3a..7402cf0 100644 --- a/Readme.md +++ b/Readme.md @@ -6,13 +6,23 @@ Gist API v3 client for Node.JS For use in your modules (adds to package.json automatically) - #npm install -S gist # old version - npm install -g git://github.com/coolaj86/node-gist.git + # Old 0.2 in npm + npm install -S gist + + # Bleeding Edge + npm install -S 'git://github.com/coolaj86/node-gist.git' + + # v1.0.0 + npm install -S 'git://github.com/coolaj86/node-gist.git#v1.0.0' For the commandline gist npm install -g gist + npm install -g 'git://github.com/coolaj86/node-gist.git' + + npm install -g 'git://github.com/coolaj86/node-gist.git#v1.0.0' + ## Usage ### Commandline From 477f168f6eb02ff20d55d48901cf740502d7af29 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 26 Oct 2012 12:30:46 -0600 Subject: [PATCH 06/10] was missing the field 'content', fixed --- bin/gist.js | 30 ++++++++++++++++++++++-------- package.json | 2 +- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/bin/gist.js b/bin/gist.js index c48618f..fa6b4fd 100644 --- a/bin/gist.js +++ b/bin/gist.js @@ -5,7 +5,7 @@ var gist = require('../') , fs = require('fs') - , filename = process.argv[2] + , filepath = process.argv[2] , desc = process.argv[3] ; @@ -13,32 +13,46 @@ console.log('Usage: gist '); } - if (!filename) { + if (!filepath) { usage(); return; } - fs.readFile(filename, 'utf8', function (err, data) { + fs.readFile(filepath, 'utf8', function (err, data) { var meta - , name = filename.replace(/.*\//, '') + , filename = filepath.replace(/.*\//, '') ; + filename = 'index.js'; + if (err) { usage(); return; } meta = { - "description": desc + "description": desc || "" , "public": true , "files": {} }; - meta.files[name] = data; + meta.files[filename] = { content: data }; gist().create(meta, function (err, resp, json) { - //console.log(JSON.stringify(json, null, ' ')); + if (err) { + console.error(err); + return; + } + + if (json.errors) { + console.log('[DEBUG]'); + console.log(JSON.stringify(meta, null, ' ')); + console.log('[GIST ERROR]'); + console.log(JSON.stringify(json, null, ' ')); + return; + } + console.log('[gist]', json.html_url); - console.log('[raw]', json.files[name].raw_url); + console.log('[raw]', json.files[filename].raw_url); console.log('[git]', json.git_push_url); }); }); diff --git a/package.json b/package.json index 6354f28..b0cf0d7 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "api", "package.json" ], - "version": "1.0.0", + "version": "1.0.1", "author": "Max Ogden ", "repository": { "type": "git", From be7bddd86708cc60118ac7249f0a8fe6ff455131 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 26 Oct 2012 13:19:08 -0600 Subject: [PATCH 07/10] v1.0.1 update --- Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index 7402cf0..c46e4bf 100644 --- a/Readme.md +++ b/Readme.md @@ -13,7 +13,7 @@ For use in your modules (adds to package.json automatically) npm install -S 'git://github.com/coolaj86/node-gist.git' # v1.0.0 - npm install -S 'git://github.com/coolaj86/node-gist.git#v1.0.0' + npm install -S 'git://github.com/coolaj86/node-gist.git#v1.0.1' For the commandline gist @@ -21,7 +21,7 @@ For the commandline gist npm install -g 'git://github.com/coolaj86/node-gist.git' - npm install -g 'git://github.com/coolaj86/node-gist.git#v1.0.0' + npm install -g 'git://github.com/coolaj86/node-gist.git#v1.0.1' ## Usage From 8d3f4e4bbfb026b7c13fa056f6a0b78fb070ac56 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 6 Nov 2012 13:58:12 -0700 Subject: [PATCH 08/10] don't name every file index.js duh --- bin/gist.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/gist.js b/bin/gist.js index fa6b4fd..825dc19 100644 --- a/bin/gist.js +++ b/bin/gist.js @@ -23,7 +23,7 @@ , filename = filepath.replace(/.*\//, '') ; - filename = 'index.js'; + //filename = 'index.js'; if (err) { usage(); From 987c165ba85e581fe3eaa291f97d53584765963b Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 6 Nov 2012 13:58:32 -0700 Subject: [PATCH 09/10] version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b0cf0d7..0d8555c 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "api", "package.json" ], - "version": "1.0.1", + "version": "1.0.2", "author": "Max Ogden ", "repository": { "type": "git", From b7b251b35c68d7ed3b6202ccd439eacc4e115bd1 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 6 Nov 2012 13:59:38 -0700 Subject: [PATCH 10/10] added newer version --- Readme.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index c46e4bf..7d38d6a 100644 --- a/Readme.md +++ b/Readme.md @@ -12,8 +12,8 @@ For use in your modules (adds to package.json automatically) # Bleeding Edge npm install -S 'git://github.com/coolaj86/node-gist.git' - # v1.0.0 - npm install -S 'git://github.com/coolaj86/node-gist.git#v1.0.1' + # v1.0.2 + npm install -S 'git://github.com/coolaj86/node-gist.git#v1.0.2' For the commandline gist @@ -21,7 +21,7 @@ For the commandline gist npm install -g 'git://github.com/coolaj86/node-gist.git' - npm install -g 'git://github.com/coolaj86/node-gist.git#v1.0.1' + npm install -g 'git://github.com/coolaj86/node-gist.git#v1.0.2' ## Usage