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..802c141 100644 --- a/Readme.md +++ b/Readme.md @@ -1,58 +1,72 @@ -[![Build Status](https://secure.travis-ci.org/emerleite/node-gist.png)](http://travis-ci.org/emerleite/node-gist) +## Node.js Gist client -Node.js Gist client -=================== -Gist API client for Node.JS +Gist API v3 client for Node.JS -Dependencies ------------- +## Installation -### Runtime -* Node 0.4.x+ +For use in your modules (adds to package.json automatically) -### Development/Tests -* mocha - *should.js + npm install -S gist -Instalation ------------ -> npm install gist +For the commandline gist -Usuage ------- - var gist = require('gist'); + npm install -g gist - gist.create('your gist content', function (url) { - console.log(url); //prints created gist url - }); +## Usage -Running tests -------------- +### Commandline -### Unit -$ node_modules/mocha/bin/mocha test/gist.test.js + gist -### Integration -$ node_modules/mocha/bin/mocha test/integration.test.js + echo "Hello World!" > ./hello.txt + gist ./hello.txt -### All tests (3 ways) -$ npm test -$ mocha (installed global) -$ node_modules/mocha/bin/mocha +### API -Note: Integration test creates real gist. Please, be carreful with this test to not flood gist. + * `gist.gists([username], fn)` + * `gist.gist(id, fn)` + * `gist([validOauthToken]).create(newGist, fn)` -To-Do ------ - () +```javascript +var gist = require('gist')(validOAuthToken); -Author ------- +// get all your gists (or all public if you didnt specify a token) +gist.gists(function(err, resp, json) { + console.log(err, json) +}) -* Emerson Macedo () +// get all public gists for some user +gist.gists('maxogden', function(err, resp, json) { + console.log(err, json) +}) -License: --------- +// 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) +}) +``` + +## Author + +* Max Ogden (@maxogden) + +this library was forked from Emerson Macedo () and entirely rewritten + +## License (The MIT License) diff --git a/bin/gist.js b/bin/gist.js new file mode 100644 index 0000000..825dc19 --- /dev/null +++ b/bin/gist.js @@ -0,0 +1,101 @@ +#!/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') + , filepath = process.argv[2] + , desc = process.argv[3] + ; + + function usage() { + console.log('Usage: gist '); + } + + if (!filepath) { + usage(); + return; + } + + fs.readFile(filepath, 'utf8', function (err, data) { + var meta + , filename = filepath.replace(/.*\//, '') + ; + + //filename = 'index.js'; + + if (err) { + usage(); + return; + } + + meta = { + "description": desc || "" + , "public": true + , "files": {} + }; + meta.files[filename] = { content: data }; + + gist().create(meta, function (err, resp, json) { + 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[filename].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/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..0d8555c 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,27 @@ -{ - "name" : "gist", - "description" : "Gist api client for node.js", - "keywords" : [ "gist", "github", "api", "package.json" ], - "version" : "0.2.1", - "author" : "Emerson Macedo ", - "repository" : { "type" : "git", - "url" : "git://github.com/emerleite/node-gist.git" }, - "devDependencies" : { - "mocha" : "0.7.0", - "should" : "0.4.2" +{ + "name": "gist", + "description": "Gist api client for node.js", + "keywords": [ + "gist", + "github", + "api", + "package.json" + ], + "version": "1.0.2", + "author": "Max Ogden ", + "repository": { + "type": "git", + "url": "git://github.com/maxogden/node-gist.git" }, "main": "index", - "scripts" : {"test" : "node_modules/mocha/bin/mocha -R spec"}, - "engines" : { "node" : ">=0.4.0" } + "bin": { + "gist": "bin/gist.js" + }, + "engines": { + "node": ">=0.6.0" + }, + "dependencies": { + "request": "~2.10.0", + "underscore": "~1.3.3" + } } - 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(); - }); - }); - -});