lib/lita/handlers/wtf.rb in lita-wtf-1.0.1 vs lib/lita/handlers/wtf.rb in lita-wtf-1.1.0
- old
+ new
@@ -1,8 +1,16 @@
module Lita
module Handlers
class Wtf < Handler
+ config :see_also, required: false, type: Array, default: []
+ config :api_keys, required: false, type: Hash, default: {}
+
+ SOURCE_NAMES = {
+ 'merriam' => 'Merriam-Webster Collegiate Dictionary',
+ 'urbandictionary' => 'UrbanDictionary'
+ }.freeze
+
route(
/^wtf(?:\s+is)?\s(?<term>[^\s@#]+)(?:\?)?/,
:lookup,
command: true,
help: {
@@ -19,12 +27,19 @@
}
)
def lookup(response)
term = response.match_data['term']
- return response.reply(t('wtf.unknown', term: term)) unless known?(term)
- response.reply(format_definition(term, definition(term)))
+ return response.reply(format_definition(term, definition(term))) if known?(term)
+
+ definition, source_name = alternate_definition(term)
+ return response.reply(t('wtf.seealso',
+ term: term,
+ definition: definition,
+ source: SOURCE_NAMES[source_name])) if definition
+
+ response.reply(t('wtf.unknown', term: term))
end
def define(response)
term = response.match_data['term']
info = response.match_data['definition']
@@ -47,9 +62,56 @@
end
def write(term, definition, owner)
redis.hset(term.downcase, 'definition', definition)
redis.hset(term.downcase, 'owner', owner)
+ end
+
+ def alternate_definition(term)
+ config.see_also.each do |source_name|
+ definition = send("lookup_#{source_name}", term)
+ return definition, source_name if definition
+ end
+
+ nil
+ end
+
+ def lookup_merriam(term)
+ api_key = config.api_keys['merriam']
+ # FIXME: Add timeouts.
+ response = http.get('http://www.dictionaryapi.com/api/v1/'\
+ "references/collegiate/xml/#{term}",
+ key: api_key)
+
+ raise unless response.status == 200
+
+ format_merriam_entries(response.body)
+ rescue StandardError
+ nil
+ end
+
+ def format_merriam_entries(content)
+ nokogiri_dom = Nokogiri::XML(content) do |config|
+ config.options = Nokogiri::XML::ParseOptions::STRICT |
+ Nokogiri::XML::ParseOptions::NONET
+ end
+
+ nokogiri_dom.css('//entry[1]/def/dt/text()').to_s[1..-1]
+ rescue StandardError
+ nil
+ end
+
+ def lookup_urbandictionary(term)
+ # FIXME: Add timeouts.
+ response = http.get('http://api.urbandictionary.com/v0/define',
+ term: term)
+
+ def_list = JSON.parse(response.body)['list']
+ def_text = def_list[0]['definition'].strip
+ def_text[0] = def_text[0].chr.downcase
+ def_text
+ rescue StandardError
+ nil
end
end
Lita.register_handler(Wtf)
end