Permalink
Browse files

Update js version

  • Loading branch information...
1 parent 16f39cd commit 2dd9067170f822a7e8828234dae49eba2d4efc7d @meritt committed Aug 16, 2011
Showing with 33 additions and 11 deletions.
  1. +33 −11 lib/gisty.js
View
@@ -1,28 +1,44 @@
(function() {
+ /*
+ node-gisty 0.0.1
+ (c) 2011 Alexey Simonenko, Serenity LLC.
+ For all details and documentation:
+ http://github.com/meritt/node-gisty
+ */
var Gisty, xhr;
xhr = require('request');
Gisty = (function() {
var api, request;
api = 'https://api.github.com';
- function Gisty(login, password) {
- this.login = login;
- this.password = password;
- this.username = this.login;
+ function Gisty(options) {
+ if (options == null) {
+ options = {};
+ }
+ this.setOptions(options);
}
- Gisty.prototype.setUsername = function(username) {
- this.username = username;
+ Gisty.prototype.setOptions = function(options) {
+ this.options = options;
+ if ((this.options.login != null) && !(this.options.username != null)) {
+ this.options.username = this.options.login;
+ }
return this;
};
Gisty.prototype.fetch = function(id, fn) {
+ if (id == null) {
+ throw new Error('You should specify gist id for fetch information');
+ }
this.scheme = '/gists/' + id;
return request(this, fn);
};
Gisty.prototype.comments = function(id, fn) {
+ if (id == null) {
+ throw new Error('You should specify gist id for fetch comments');
+ }
this.scheme = '/gists/' + id + '/comments';
return request(this, fn);
};
Gisty.prototype.all = function(fn) {
- this.scheme = '/users/' + this.username + '/gists';
+ this.scheme = this.options.username != null ? '/users/' + this.options.username + '/gists' : '/gists';
return request(this, fn);
};
Gisty.prototype.public = function(fn) {
@@ -39,11 +55,17 @@
fn = function() {};
}
options = {
- url: api + self.scheme,
- headers: {
- Authorization: 'Basic ' + new Buffer(self.login + ':' + self.password).toString('base64')
- }
+ url: api + self.scheme
};
+ if (self.options.token != null) {
+ options['headers'] = {
+ Authorization: "token " + self.options.token
+ };
+ } else if ((self.options.login != null) && (self.options.password != null)) {
+ options['headers'] = {
+ Authorization: "Basic " + new Buffer(self.options.login + ':' + self.options.password).toString('base64')
+ };
+ }
return xhr(options, function(error, request, body) {
var limit, _ref;
body = JSON.parse(body);

0 comments on commit 2dd9067

Please sign in to comment.