From ed04bd3c0fff100f40ed45d300fcb152723226ef Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 21 Apr 2014 14:17:44 -0700 Subject: Move response validation out of main code. --- core/messageinfo.rb | 15 +++++++++++++++ dinobot.rb | 21 --------------------- module/admin.rb | 12 ++++++------ module/base.rb | 2 +- module/config.rb | 4 ++-- module/test.rb | 10 +++++----- 6 files changed, 29 insertions(+), 35 deletions(-) diff --git a/core/messageinfo.rb b/core/messageinfo.rb index 4499bdc..627ca90 100644 --- a/core/messageinfo.rb +++ b/core/messageinfo.rb @@ -9,6 +9,21 @@ module Dinobot @message = message @response = [] end + + def respond(arr) + raise "response not array -- #{arr}" unless arr.is_a?(Array) + + case arr.first + when :say + raise "wrong number of arguments -- #{arr}" unless arr.length == 3 + when :join, :part, :quit + raise "wrong number of arguments -- #{arr}" unless arr.length == 2 + else + raise "unknown method name -- #{arr}" + end + + @response << arr + end end end end diff --git a/dinobot.rb b/dinobot.rb index 752ca7e..503a3b4 100644 --- a/dinobot.rb +++ b/dinobot.rb @@ -139,7 +139,6 @@ module Dinobot exec_command(m, command) unless m.response.empty? - ensure_valid_response(m.response) process_response(m.response) end end @@ -162,15 +161,12 @@ module Dinobot if m.response.empty? @modules[mod].call(m, command) else - ensure_valid_response(m.response) - prev = m.response m.response = [] prev.each do |x| if x.first == :say @modules[mod].call(m, "#{command} #{x[2]}") - ensure_valid_response(m.response) else m.response << x end @@ -186,22 +182,5 @@ module Dinobot send(*x) end end - - def ensure_valid_response(response) - raise "method list not array -- #{response}" unless response.is_a?(Array) - - response.each do |x| - raise "method not array -- #{x}" unless x.is_a?(Array) - - case x.first - when :say - raise "wrong number of arguments -- #{x}" unless x.length == 3 - when :join, :part, :quit - raise "wrong number of arguments -- #{x}" unless x.length == 2 - else - raise "unknown method name -- #{x}" - end - end - end end end diff --git a/module/admin.rb b/module/admin.rb index 72c2a14..f26a652 100644 --- a/module/admin.rb +++ b/module/admin.rb @@ -38,19 +38,19 @@ module Dinobot def join(m, args) return unless is_admin?(m.user) - m.response << [:join, args.strip] + m.respond [:join, args.strip] end def part(m, args) return unless is_admin?(m.user) - m.response << [:part, args.strip] + m.respond [:part, args.strip] end def quit(m, args) return unless is_admin?(m.user) - m.response << [:quit, args ? args.strip : 'Quitting.'] + m.respond [:quit, args ? args.strip : 'Quitting.'] end def load(m, args) @@ -72,20 +72,20 @@ module Dinobot def listadmins(m, args) return unless is_admin?(m.user) - m.response << [:say, m.channel, "Admins: #{@admins.join(' ')}"] + m.respond [:say, m.channel, "Admins: #{@admins.join(' ')}"] end def listmodules(m, args) return unless is_admin?(m.user) - m.response << [:say, m.channel, + m.respond [:say, m.channel, "Modules: #{@bot.modules.keys.sort.join(' ')}"] end def listchannels(m, args) return unless is_admin?(m.user) - m.response << [:say, m.channel, + m.respond [:say, m.channel, "Channels: #{@bot.channels.sort.join(' ')}"] end end diff --git a/module/base.rb b/module/base.rb index 3f4d9cd..3d04895 100644 --- a/module/base.rb +++ b/module/base.rb @@ -19,7 +19,7 @@ module Dinobot end def commands(m, args) - m.response << [:say, m.channel, + m.respond [:say, m.channel, "Commands: #{@commands.sort.join(' ')}"] end end diff --git a/module/config.rb b/module/config.rb index fa3c151..cf71bd4 100644 --- a/module/config.rb +++ b/module/config.rb @@ -17,9 +17,9 @@ module Dinobot case args when 'trigger' - m.response << [:say, m.channel, @config.data[:trigger][:global]] + m.respond [:say, m.channel, @config.data[:trigger][:global]] when 'debug' - m.response << [:say, m.channel, @config.data[:debug].to_s] + m.respond [:say, m.channel, @config.data[:debug].to_s] end end diff --git a/module/test.rb b/module/test.rb index cfc929b..69bb888 100644 --- a/module/test.rb +++ b/module/test.rb @@ -11,21 +11,21 @@ module Dinobot end def echo(m, args) - m.response << [:say, m.channel, args] + m.respond [:say, m.channel, args] end def ping(m, args) - m.response << [:say, m.channel, 'pong'] + m.respond [:say, m.channel, 'pong'] end def x3(m, args) 3.times do - m.response << [:say, m.channel, args] + m.respond [:say, m.channel, args] end end def fooify(m, args) - m.response << [:say, m.channel, "foo#{args}"] + m.respond [:say, m.channel, "foo#{args}"] end def error(m, args) @@ -37,7 +37,7 @@ module Dinobot end def invalidresponse(m, args) - m.response << [:say, m.channel] + m.respond [:say, m.channel] end end end -- cgit v1.2.3-70-g09d2