Skip to content

Instantly share code, notes, and snippets.

@jtrim
Created January 4, 2012 01:32
Show Gist options
  • Select an option

  • Save jtrim/1557973 to your computer and use it in GitHub Desktop.

Select an option

Save jtrim/1557973 to your computer and use it in GitHub Desktop.
# Gets the last gist for specified user
#
# last gist [for] <username> - Returns the last gist for specified user
module.exports = (robot) ->
robot.respond /last gist (?:for)? (.+)/i, (msg) ->
username = msg.match[1]
url = "https://api.github.com/users/#{username}/gists"
msg.http(url).get() (err, res, body) ->
gists = JSON.parse(body)
indexed = {}
dates = []
indexed[+new Date(gist.created_at)] = gist for gist in gists when gist.public == true
dates.push(+new Date(gist.created_at)) for gist in gists
gist = indexed[Math.max.apply(global, dates)]
msg.send "Last public gist by #{username}:"
msg.send gist.html_url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment