class StallmanBot::Listener

Public Class Methods

new() click to toggle source
# File lib/stallman_bot/listener.rb, line 5
def initialize
  @listening = false
  @cmd = Commands.all
  @replies = Locale.replies
end

Public Instance Methods

respond(message, bot = nil, id = nil) click to toggle source
# File lib/stallman_bot/listener.rb, line 11
def respond(message, bot = nil, id = nil)
  if command?(message)
    @listening = run_command(message, bot, id)
  elsif @listening
    answer = filter(message)
    Commands::Base.run(bot, id, answer) unless answer.nil?
  end
end

Private Instance Methods

command?(cmd) click to toggle source
# File lib/stallman_bot/listener.rb, line 20
def command?(cmd)
  @cmd.key?(cmd)
end
filter(message) click to toggle source
# File lib/stallman_bot/listener.rb, line 28
def filter(message)
  replies = @replies.select { |x| message.downcase.include?(x) }
  Locale.t("reply.#{replies.sample}") unless replies.empty?
end
run_command(command, bot, id) click to toggle source
# File lib/stallman_bot/listener.rb, line 24
def run_command(command, bot, id)
  Object.const_get(@cmd[command]).run(bot, id, @listening)
end