Skip to content

Commit

Permalink
Added gist scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
Damon Oehlman committed Sep 19, 2012
1 parent f67a453 commit 51af335
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -45,6 +45,22 @@ getit('github://DamonOehlman/getit/index.js', function(err, data) {
});
```

### Github Gists (gist://)

To get the default file (first file) from a particular gist:

```js
getit('gist://3344823', function(err, content) {
});
```

To get a specific file from a particular gist:

```js
getit('gist://3344823/gistfile1.md', function(err, content) {
});
```

## Contributing URL Schemes

I haven't as yet ported the schemes from interleave across yet, but the process is incredibly simple, and the content of the [github scheme translator](/DamonOehlman/getit/blob/master/lib/schemes/github.js) is shown below:
Expand Down
17 changes: 17 additions & 0 deletions lib/schemes/gist.js
@@ -0,0 +1,17 @@
var debug = require('debug')('getit-gist');

module.exports = function(parts, original) {
var endpoint;

debug('running gist scheme remapper on: ' + original, parts);

// map the endpoint to the gist first of all
endpoint = 'https://raw.github.com/gist/' + parts.host;

// if a pathname has been extracted from the original url, then append that to the request
if (parts.pathname) {
endpoint += '/' + parts.pathname.replace(/^\//, '');
}

return endpoint;
};
30 changes: 30 additions & 0 deletions test/local.js
@@ -0,0 +1,30 @@
var assert = require('assert'),
fs = require('fs'),
path = require('path'),
getit = require('../'),
testfile = path.resolve(__dirname, 'test.txt'),
testContent,
opts = {
cwd: __dirname
};

describe('streamed download test', function() {
before(function(done) {
fs.readFile(path.resolve(__dirname, 'files/test.txt'), 'utf8', function(err, data) {
if (! err) {
testContent = data;
}

done(err);
});
});

it('should be able to get a local file', function(done) {
getit('files/test.txt', opts, function(err, data) {
assert.ifError(err);
assert.equal(data, testContent);

done();
});
});
});
53 changes: 53 additions & 0 deletions test/scheme-gist.js
@@ -0,0 +1,53 @@
var assert = require('assert'),
getit = require('../'),
snippet1, snippet2;

describe('gist scheme test', function() {
before(function(done) {
getit('https://raw.github.com/gist/3344823/gistfile1.md', function(err, content) {
snippet1 = content;
done(err);
});
});

before(function(done) {
getit('https://raw.github.com/gist/1261033/bridge-server.js', function(err, content) {
snippet2 = content;
done(err);
});
});

it('should get the first file by gist id only', function(done) {
getit('gist://3344823', function(err, content) {
assert.ifError(err);
assert.equal(content, snippet1);

done(err);
});
});

it('should get a specified file when specified', function(done) {
getit('gist://3344823/gistfile1.md', function(err, content) {
assert.ifError(err);
assert.equal(content, snippet1);

done(err);
});
});

it('should error when a non-existant file is requested', function(done) {
getit('gist://3344823/gistfile1.error', function(err, content) {
assert(err);
done();
});
});

it('should get a specified file when the gist has more than one file', function(done) {
getit('gist://1261033/bridge-server.js', function(err, content) {
assert.ifError(err);
assert.equal(content, snippet2);

done();
});
});
});

0 comments on commit 51af335

Please sign in to comment.