lib/lita/handlers/markov.rb in lita-markov-1.0.0 vs lib/lita/handlers/markov.rb in lita-markov-1.0.1

- old
+ new

@@ -3,24 +3,20 @@ require 'lita/handlers/markov/engine' module Lita::Handlers class Markov - attr_reader :engine - - config :database_url + config :database_url, type: String, required: true route(/.+/, :ingest, command: false) route(/markov (.+)/, :generate, command: true, help: { 'markov USER' => 'Generate a markov chain from the given user.' }) - def initialize(robot) - super(robot) - - @engine = Engine.new self + def engine + @engine ||= Engine.new config.database_url end def ingest(chat) # Don't ingest messages addressed to ourselves return if chat.command? @@ -28,18 +24,18 @@ message = chat.matches[0].strip # Get the mention name (ie. 'dirk') of the user id = chat.user.id - @engine.ingest id, message + engine.ingest id, message end def generate(chat) name = chat.matches[0][0].strip id = Lita::User.fuzzy_find(name).id begin - sentence = @engine.generate_sentence_for id + sentence = engine.generate_sentence_for id chat.reply sentence rescue Engine::EmptyDictionaryError chat.reply "Looks like #{name} hasn't said anything!" end