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 /dinobot.rb | |
parent | 9f44e1d9372e4168def6e90f6b49d61c74f0045d (diff) | |
download | dinobot-6d4f0b1a3c82a983edd7ccba6dee5a7c79a8c941.tar.gz dinobot-6d4f0b1a3c82a983edd7ccba6dee5a7c79a8c941.tar.xz |
Add method list validation.
Diffstat (limited to 'dinobot.rb')
-rw-r--r-- | dinobot.rb | 32 |
1 files changed, 21 insertions, 11 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 |