Skip to content

Commit

Permalink
Can now get contents via sync method
Browse files Browse the repository at this point in the history
Uses most of pull request from @dhcole and closes github-tools#46
To avoid code duplication though, sync has been added as an extra param,
instead of new function
  • Loading branch information
mattpass committed Oct 2, 2013
1 parent c875027 commit 5a8526c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ Show repository information
repo.show(function(err, repo) {});
```

Get contents at a particular path in a particular branch.
Get contents at a particular path in a particular branch. Set sync to true to get contents via sync method.

```js
repo.contents("master", "path/to/dir", function(err, contents) {});
repo.contents(branch, "path/to/dir", function(err, contents) {}, sync);
```

Fork repository. This operation runs asynchronously. You may want to poll for `repo.contents` until the forked repo is ready.
Expand Down
23 changes: 13 additions & 10 deletions github.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
//
// I'm not proud of this and neither should you be if you were responsible for the XMLHttpRequest spec.

function _request(method, path, data, cb, raw) {
function _request(method, path, data, cb, raw, sync) {
function getURL() {
var url = API_URL + path;
return url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime();
Expand All @@ -42,13 +42,15 @@
var xhr = new XMLHttpRequest();
if (!raw) {xhr.dataType = "json";}

xhr.open(method, getURL());
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status >= 200 && this.status < 300 || this.status === 304) {
cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true);
} else {
cb({request: this, error: this.status});
xhr.open(method, getURL(), !sync);
if (!sync) {
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status >= 200 && this.status < 300 || this.status === 304) {
cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true);
} else {
cb({request: this, error: this.status});
}
}
}
};
Expand All @@ -61,6 +63,7 @@
);
}
data ? xhr.send(JSON.stringify(data)) : xhr.send();
if (sync) return xhr.response;
}


Expand Down Expand Up @@ -343,8 +346,8 @@
// Get contents
// --------

this.contents = function(branch, path, cb) {
_request("GET", repoPath + "/contents?ref=" + branch + (path ? "&path=" + path : ""), null, cb, 'raw');
this.contents = function(branch, path, cb, sync) {
return _request("GET", repoPath + "/contents?ref=" + branch + (path ? "&path=" + path : ""), null, cb, 'raw', sync);
};

// Fork repository
Expand Down

0 comments on commit 5a8526c

Please sign in to comment.