Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added gist scheme

  • Loading branch information...
commit 51af33564b51b7a99eb2e1e0e9c2a6d4a3dcde0b 1 parent f67a453
Damon Oehlman authored
16 README.md
View
@@ -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:
17 lib/schemes/gist.js
View
@@ -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 test/local.js
View
@@ -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 test/scheme-gist.js
View
@@ -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();
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.