diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-15 07:56:44 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-15 07:56:44 -0700 |
commit | 6d4f0b1a3c82a983edd7ccba6dee5a7c79a8c941 (patch) | |
tree | 92f34b33eac5f55f06915d203d14edc680958f09 | |
parent | 9f44e1d9372e4168def6e90f6b49d61c74f0045d (diff) | |
download | dinobot-6d4f0b1a3c82a983edd7ccba6dee5a7c79a8c941.tar.gz dinobot-6d4f0b1a3c82a983edd7ccba6dee5a7c79a8c941.tar.xz |
Add method list validation.
-rw-r--r-- | dinobot.rb | 32 | ||||
-rw-r--r-- | test.rb | 6 |
2 files changed, 26 insertions, 12 deletions
@@ -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 @@ -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 |