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 +++++++++++++++++++++----------- test.rb | 6 +++++- 2 files changed, 26 insertions(+), 12 deletions(-) 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 diff --git a/test.rb b/test.rb index e721104..a54683e 100644 --- a/test.rb +++ b/test.rb @@ -6,7 +6,7 @@ module Dinobot super @commands << :echo << :ping << :x3 << :fooify - @commands << :error << :timeout << :wrongreturn + @commands << :error << :timeout << :wrongreturn << :invalidmethods end def echo(user, channel, argument) @@ -36,5 +36,9 @@ module Dinobot def wrongreturn(user, channel, argument) 0 end + + def invalidmethods(user, channel, argument) + [[:say, channel]] + end end end -- cgit v1.2.3-70-g09d2