class Ruboty::Handlers::Response

Constants

NAMESPACE

Public Instance Methods

add(message) click to toggle source
# File lib/ruboty/handlers/response.rb, line 24
def add(message)
  id = generate_id
  hash = {
    regex: message[:regex],
    response: message[:response]
  }

  # Insert to the brain
  responses[id] = hash

  message.reply("Response #{id} is registered.")
end
catchall(message) click to toggle source
# File lib/ruboty/handlers/response.rb, line 14
def catchall(message)
  responses.each do |id, hash|
    next unless message[:keyword] =~ /#{hash[:regex]}/ rescue false

    message.reply(hash[:response])
  end
rescue => e
  Ruboty.logger.error("Error: #{e.class}: #{e.message}}")
end
delete(message) click to toggle source
# File lib/ruboty/handlers/response.rb, line 37
def delete(message)
  if responses.delete(message[:id].to_i)
    message.reply("Response #{message[:id]} is unregistered.")
  else
    message.reply("Response #{message[:id]} is not found.")
  end
end
list(message) click to toggle source
# File lib/ruboty/handlers/response.rb, line 45
def list(message)
  if responses.empty?
    message.reply('Nothing is registered.')
  else
    response_list = responses.map do |id, hash|
      "#{id}: /#{hash[:regex]}/ -> #{hash[:response]}"
    end.join("\n")
    message.reply(response_list, code: true)
  end
end

Private Instance Methods

generate_id() click to toggle source
# File lib/ruboty/handlers/response.rb, line 62
def generate_id
  loop do
    id = rand(1000)
    break id unless responses.has_key?(id)
  end
end
responses() click to toggle source
# File lib/ruboty/handlers/response.rb, line 58
def responses
  robot.brain.data[NAMESPACE] ||= {}
end