1 | var request = require('micro-request')
|
2 | , assert = require('assert')
|
3 |
|
4 | function fetchGist (input, cb) {
|
5 | var pkg = require('../package.json')
|
6 | var headers = {
|
7 | 'User-Agent': pkg.name + '/' + pkg.version
|
8 | }
|
9 | var re = /^(?:https:\/\/gist\.github\.com\/)?(?:([^\/]+)\/)?([a-f0-9]+)/
|
10 | var inMatch = input.match(re)
|
11 | if (!inMatch) return cb(new Error('invalid gist spec'))
|
12 | var uri = 'https://api.github.com/gists/' + inMatch[2]
|
13 | request('https://api.github.com/gists/' + inMatch[2], {headers: headers}, function (err, resp, gist) {
|
14 | if (err) return cb(err)
|
15 | if (resp.statusCode !== 200) {
|
16 | return cb(new Error('non-200 status from github: ' + resp.statusCode))
|
17 | }
|
18 | if (!gist.files['salty.pem']) return cb(new Error('no salty.pem found in gist'))
|
19 | request(gist.files['salty.pem'].raw_url, function (err, resp, body) {
|
20 | if (err) return cb(err)
|
21 | if (resp.statusCode !== 200) return cb(new Error('non-200 status from github: ' + resp.statusCode))
|
22 | cb(null, body, gist)
|
23 | })
|
24 | })
|
25 | }
|
26 | module.exports = fetchGist |
\ | No newline at end of file |