From 6d4f0b1a3c82a983edd7ccba6dee5a7c79a8c941 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 15 Apr 2014 07:56:44 -0700 Subject: Add method list validation. --- dinobot.rb | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'dinobot.rb') diff --git a/dinobot.rb b/dinobot.rb index 6f2ef9a..ce08b7c 100644 --- a/dinobot.rb +++ b/dinobot.rb @@ -148,8 +148,8 @@ module Dinobot return unless message.sub!(/^#{Regexp.escape(@trigger)}/, '') methods = exec_command(user, channel, message) - # TODO: Check if methods is valid list of methods. - run_methods(methods) if methods.is_a?(Array) + ensure_valid_methods(methods) + run_methods(methods) end end @@ -163,14 +163,14 @@ module Dinobot if prev.nil? methods = @modules[mod].call(user, channel, command) else + ensure_valid_methods(prev) methods = [] prev.each do |p| if p.first == :say m = @modules[mod].call(user, p[1], "#{command} #{p[2]}") - - # TODO: Check if m is valid list of methods. - methods.concat(m) if m.is_a?(Array) + ensure_valid_methods(m) + methods.concat(m) else methods << p end @@ -181,15 +181,25 @@ module Dinobot end def run_methods(methods) - methods.each do |method| - log :info, "Executing method: #{method.inspect}" + methods.each do |m| + log :info, "Executing method: #{m.inspect}" + send(*m) + end + end + + def ensure_valid_methods(methods) + raise "method list not array -- #{methods}" unless methods.is_a?(Array) + + methods.each do |m| + raise "method not array -- #{m}" unless m.is_a?(Array) - # TODO: Raise error if unknown method name or incorrect argument count. - case method.first + case m.first when :say - send(*method) if method.length == 3 + raise "wrong number of arguments -- #{m}" unless m.length == 3 when :join, :part - send(*method) if method.length == 2 + raise "wrong number of arguments -- #{m}" unless m.length == 2 + else + raise "unknown method name -- #{m}" end end end -- cgit v1.2.3-70-g09d2