Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
66 lines (58 sloc) 2.04 KB
fs = require 'fs'
path = require 'path'
https = require 'https'
querystring = require 'querystring'
exports.send = (files, options, callback) ->
gist = {}
if typeof options is 'function'
callback = options
options = null
return callback new Error "#{files.length} files is more than the maximum of 10 files" if files.length > 10
files.forEach (file, i) ->
gist["file_ext[gistfile#{i+1}]"] = path.extname
gist["file_name[gistfile#{i+1}]"] =
gist["file_contents[gistfile#{i+1}]"] = file.contents
unless options is null
if (options.user and not options.token) or (options.token and not options.user)
return callback new Error 'To login both options.user and options.token must be given'
gist['login'] = options.user if options.user
gist['token'] = options.token if options.token
gist['description'] = options.description if options.description
gist['action_button'] = 'private' if options.private is true
return post gist, callback
exports.files = (files, callback) ->
tmp = []
serial files, (file, i, next) ->
path.exists file, (exists) ->
return callback new Error "File not found: #{file}" unless exists
fs.readFile file, 'utf8', (err, data) ->
'name': path.basename file
'contents': data
, ->
callback null, tmp
post = (gist, callback) ->
gistData = querystring.stringify gist
options =
host: ''
port: 443
path: '/gists'
method: 'POST'
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Length': gistData.length
req = https.request options, (res) ->
if res.statusCode == 401
return callback new Error 'Username or token not authorized on'
res.on 'end', ->
callback null, res.headers.location
req.end gistData
serial = (array, iterator, next) ->
cycle = (i) ->
if i < array.length
iterator array[i], i, ->
cycle i+1
cycle 0