PageRenderTime 37ms CodeModel.GetById 34ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/src/scripts/abstract.coffee

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