Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Now finds or creates gist to store the favorites in.
  • Loading branch information
jrunning committed Mar 30, 2011
1 parent b32abb7 commit 14a17f5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 41 deletions.
32 changes: 0 additions & 32 deletions background.html
Expand Up @@ -6,38 +6,6 @@
</head>
<body>
<script>
var getGistId = function getGistId(username, callback) {
if(!localStorage['gistId']) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var gists = JSON.parse(xhr.responseText).gists
, numGists = gists.length
;

for(var i = 0; i < numGists; i++) {
if(gists[i].files[0] == 'github-faves.json') {
callback(gists[i].repo);
return;
}
}

createGist(callback);
} else {
// TODO: TEH ERRORZ!
callback(null);
}
}
}

var url = 'http://gist.github.com/api/v1/json/gists/' + username;

xhr.open('GET', url, true);
xhr.send();
}
};

var faves = new GHFaves();

// big generic listener to all extension messages
Expand Down
48 changes: 42 additions & 6 deletions lib/GHFaves.js
@@ -1,8 +1,14 @@
function GHFaves() {
this.loadCredentials();

if(this.hasCredentials()) {
this.gist = new Gister(this.username, this.apiToken);
}
}

GHFaves.ACCOUNT_ADMIN_PATHNAME = '/account/admin';
GHFaves.ACCOUNT_ADMIN_URL = 'https://github.com/account/admin';
GHFaves.GIST_FILENAME = 'github-faves.json'

GHFaves.prototype.messageHandlers = {
'acquiredCredentials' : function msg_acquiredCredentials(request, _, callback) {
Expand All @@ -24,15 +30,23 @@ GHFaves.prototype.saveCredentials = function saveCredentials(username, apiToken,
};

GHFaves.prototype.hasCredentials = function hasCredentials(sendResponse) {
sendResponse( { 'message' : 'hasCredentials?'
, 'hasCredentials' : localStorage['github-username'] && localStorage['github-api-token']
}
);
var hasCredentials = !!(this.username && this.apiToken);

if(sendResponse) {
sendResponse( { 'message' : 'hasCredentials?'
, 'hasCredentials' : hasCredentials
}
);
}

return hasCredentials;
};

GHFaves.prototype.loadCredentials = function loadCredentials(sendResponse) {
if( this.username = localStorage['github-username']
&& this.apiToken = localStorage['github-api-token']
var sendResponse = sendResponse || function() {};

if( (this.username = localStorage['github-username'])
&& (this.apiToken = localStorage['github-api-token'])
) {
sendResponse({ message : 'credentials', username : this.username, apiToken : this.apiToken });
} else {
Expand Down Expand Up @@ -64,6 +78,28 @@ GHFaves.prototype.doSetup = function doSetup() {
);
};

GHFaves.prototype.getGistId = function getGistId(username, callback) {
var gist = this.gist;

this.gist.getGists(username, function(resp) {
console.log(resp);
var gists = resp.gists
, numGists = gists.length
;

for(var i = 0; i < numGists; i++) {
if(gists[i].files[0] == GHFaves.GIST_FILENAME) {
callback(gists[i].repo);
return;
}
}

// if we get this far, no such gist exists!
gist.createOne(GHFaves.GIST_FILENAME, '{}', {}, callback);
});
};


// a.k.a. poorMansParseURL
GHFaves.Util = { parseURL : function parseURL(url) {
var dummy = document.createElement('a');
Expand Down
16 changes: 13 additions & 3 deletions lib/Gister.js
Expand Up @@ -26,7 +26,17 @@ Gister.prototype.createOne = function createOne(filename, data, options, callbac
postData.action_button = 'private';
}

this.genericPost(this.CREATE_URI, postData, callback);
this.genericPost(this.CREATE_URI, postData, function(data) {
var pattern = /\sgist: (\d{6,})\s/ // unwilling to parse the entire returned doc, I turn to drugs (and regular expressions)
matches = pattern.exec(data)
;

if(matches) {
callback(matches[1]);
} else {
callback();
}
});
};


Expand All @@ -51,7 +61,7 @@ Gister.prototype.writeOne = function write(repoId, filename, data, callback) {

// get the JSON content of a gist as an object
Gister.prototype.getJSON = function getJSON(repoId, callback) {
this.get(repoId, jsonifyCallback(callback));
this.get(repoId, this.jsonifyCallback(callback));
};

// get the content of a gist
Expand All @@ -63,7 +73,7 @@ Gister.prototype.get = function get(repoId, callback) {

// get a list of a user's public gists
Gister.prototype.getGists = function getGists(username, callback) {
this.genericGet(this.BASE_URI + username, jsonifyCallback(callback));
this.genericGet(this.BASE_URI + 'gists/' + username, this.jsonifyCallback(callback));
};


Expand Down

0 comments on commit 14a17f5

Please sign in to comment.