Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit e703a81

Browse files
Merge pull request #614 from aaronott/master
Add a call for commandlinefu
2 parents 325386b + a61c764 commit e703a81

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/scripts/commandlinefu.coffee

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Description:
2+
# Returns a command from commandlinefu.com
3+
#
4+
# Dependencies:
5+
# None
6+
#
7+
# Configuration:
8+
# None
9+
#
10+
# Commands:
11+
# hubot commandlinefu me - returns random command
12+
# hubot commandlinefu me <command> - random entry for the comand passed
13+
#
14+
# Author:
15+
# aaronott
16+
17+
module.exports = (robot) ->
18+
robot.respond /commandlinefu(?: me)? *(.*)?/i, (msg) ->
19+
query = if msg.match[1]
20+
"matching/#{msg.match[1]}/#{new Buffer(msg.match[1]).toString('base64')}/json"
21+
else
22+
"random/json"
23+
command msg, "http://www.commandlinefu.com/commands/#{query}", (cmd) ->
24+
msg.send cmd
25+
26+
command = (msg, uri, cb) ->
27+
msg.http(uri)
28+
.get() (err, res, body) ->
29+
# The random call passes back a 302 to redirect to a new page, if this
30+
# happens we redirect through a recursive function call passing the new
31+
# location to retrieve
32+
if res.statusCode == 302
33+
command msg, res.headers.location, cb
34+
else
35+
# choose a random command from the returned list
36+
cc = msg.random JSON.parse(body)
37+
cb("-- #{cc.summary}\n#{cc.command}")

0 commit comments

Comments
 (0)