PageRenderTime 6ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/scripts/abstract.coffee

http://github.com/github/hubot-scripts
CoffeeScript | 45 lines | 21 code | 1 blank | 23 comment | 9 complexity | 854ee6ef075de384eb4fac03ca955d1b MD5 | raw file
 1# Description:
 2#   None
 3#
 4# Dependencies:
 5#   None
 6#
 7# Configuration:
 8#   None
 9#
10# Commands:
11#   hubot abstract <topic> - Prints a nice abstract of the given topic
12#
13# Author:
14#   tantalor
15
16module.exports = (robot) ->
17  robot.respond /(abs|abstract) (.+)/i, (res) ->
18    abstract_url = "http://api.duckduckgo.com/?format=json&q=#{encodeURIComponent(res.match[2])}"
19    res.http(abstract_url)
20      .header('User-Agent', 'Hubot Abstract Script')
21      .get() (err, _, body) ->
22        return res.send "Sorry, the tubes are broken." if err
23        data = JSON.parse(body.toString("utf8"))
24        return unless data
25        topic = data.RelatedTopics[0] if data.RelatedTopics and data.RelatedTopics.length
26        if data.AbstractText
27          # hubot abs numerology
28          # Numerology is any study of the purported mystical relationship between a count or measurement and life.
29          # http://en.wikipedia.org/wiki/Numerology
30          res.send data.AbstractText
31          res.send data.AbstractURL if data.AbstractURL
32        else if topic and not /\/c\//.test(topic.FirstURL)
33          # hubot abs astronomy
34          # Astronomy is the scientific study of celestial objects.
35          # http://duckduckgo.com/Astronomy
36          res.send topic.Text
37          res.send topic.FirstURL
38        else if data.Definition
39          # hubot abs contumacious
40          # contumacious definition: stubbornly disobedient.
41          # http://merriam-webster.com/dictionary/contumacious
42          res.send data.Definition
43          res.send data.DefinitionURL if data.DefinitionURL
44        else
45          res.send "I don't know anything about that."