Permalink
Browse files

Tightened up the code and added more coffeescriptisms.

  • Loading branch information...
1 parent 1687e56 commit a61c764b0aee00827dac32d219d849fc9eba2a83 @aaronott aaronott committed Nov 7, 2012
Showing with 10 additions and 16 deletions.
  1. +10 −16 src/scripts/commandlinefu.coffee
@@ -8,36 +8,30 @@
# None
#
# Commands:
-# hubot show me some commandlinefu - returns random command
-# hubot show me some commandlinefu <command> - random entry for the comand passed
+# hubot commandlinefu me - returns random command
+# hubot commandlinefu me <command> - random entry for the comand passed
#
# Author:
# aaronott
module.exports = (robot) ->
- robot.respond /(show me some )?(commandlinefu)\s*(.*)?/i, (msg) ->
- uri = "http://www.commandlinefu.com/commands/"
- c = msg.match[3]
- if c != undefined
- query = "matching/" + c + "/" + new Buffer(c).toString('base64') + "/json"
- else
- query = "random/json"
- uri = uri + query
- command msg, uri, (cmd) ->
+ robot.respond /commandlinefu(?: me)? *(.*)?/i, (msg) ->
+ query = if msg.match[1]
+ "matching/#{msg.match[1]}/#{new Buffer(msg.match[1]).toString('base64')}/json"
+ else
+ "random/json"
+ command msg, "http://www.commandlinefu.com/commands/#{query}", (cmd) ->
msg.send cmd
command = (msg, uri, cb) ->
msg.http(uri)
- .header('User-Agent', 'Mozilla/5.0')
.get() (err, res, body) ->
# The random call passes back a 302 to redirect to a new page, if this
# happens we redirect through a recursive function call passing the new
# location to retrieve
if res.statusCode == 302
command msg, res.headers.location, cb
else
- cl = JSON.parse(body)
# choose a random command from the returned list
- cc = msg.random cl
- ret_str = "-- #{cc.summary}\n#{cc.command}"
- cb(ret_str)
+ cc = msg.random JSON.parse(body)
+ cb("-- #{cc.summary}\n#{cc.command}")

0 comments on commit a61c764

Please sign in to comment.