Class: BeerBot::Dispatcher
- Inherits:
-
Object
- Object
- BeerBot::Dispatcher
- Defined in:
- lib/beerbot/06.dispatcher.rb
Overview
A dispatcher’s main task is to receive incoming generic events (not protocol specific) and return a botmsg or empty array.
This is done via the Dispatcher#receive.
Dispatcher#receive takes: 1) event - a symbol representing an event eg :msg, :join etc 2) args - an array representing arguments ‘event’ and ‘args’ are intended to be protocol-neutral.
This class is basically a glorified struct with a receive method that is the default way to dispatch to an instance of Bot.
You have several options if you want to customize… 1) subclass if you want to pre or post filter 2) override #receive with your own singleton receive (see tests) 3) make a new Dispatcher-like class
Constant Summary collapse
Instance Attribute Summary collapse
-
#bot ⇒ Object
Returns the value of attribute bot.
-
#config ⇒ Object
Returns the value of attribute config.
-
#nick ⇒ Object
Returns the value of attribute nick.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
Instance Method Summary collapse
-
#initialize(bot, nick, prefix: ',', config: nil) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
-
#receive(event, args) ⇒ Object
Receive generic events emitted by a protocol class and dispatch to an instance of Bot.
Constructor Details
#initialize(bot, nick, prefix: ',', config: nil) ⇒ Dispatcher
Returns a new instance of Dispatcher.
38 39 40 41 42 43 44 45 46 |
# File 'lib/beerbot/06.dispatcher.rb', line 38 def initialize bot,nick,prefix:',',config:nil @bot = bot @nick = nick @prefix = prefix @config = config @get_nick_cmd = Utils.make_prefix_parser(nick) @nickrx = Regexp.new("^#{nick}$",'i') @get_prefix_cmd = Utils.make_prefix_parser(prefix) end |
Instance Attribute Details
#bot ⇒ Object
Returns the value of attribute bot.
36 37 38 |
# File 'lib/beerbot/06.dispatcher.rb', line 36 def bot @bot end |
#config ⇒ Object
Returns the value of attribute config.
36 37 38 |
# File 'lib/beerbot/06.dispatcher.rb', line 36 def config @config end |
#nick ⇒ Object
Returns the value of attribute nick.
36 37 38 |
# File 'lib/beerbot/06.dispatcher.rb', line 36 def nick @nick end |
#prefix ⇒ Object
Returns the value of attribute prefix.
36 37 38 |
# File 'lib/beerbot/06.dispatcher.rb', line 36 def prefix @prefix end |
Instance Method Details
#receive(event, args) ⇒ Object
Receive generic events emitted by a protocol class and dispatch to an instance of Bot.
eg the output from BeerBot::Codecs::IRC.decode .
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/beerbot/06.dispatcher.rb', line 53 def receive event,args replies = nil # Otherwise, here is the default behaviour... case event when :unknown replies = @bot.event(event,args:args,config:@config) when :default replies = @bot.event(event,args:args,config:@config) when :nick old,nick = args replies = @bot.event(event,old:old,nick:nick,config:@config) when :quit nick,msg = args replies = @bot.event(event,nick:nick,msg:msg,config:@config) when :part nick,channel = args replies = @bot.event(event,nick:nick,channel:channel,config:@config) when :join nick,channel = args me = (@nickrx === nick) replies = @bot.event(event,me:me,nick:nick,channel:channel,config:@config) when :chanlist channel,users = args replies = @bot.event(event,channel:channel,users:users,config:@config) when :chanlistend # ignore when :action from,to,action = args me = (@nickrx === to) replies = @bot.action(action,from:from,to:to,me:me,config:@config) when :msg from,to,msg = args # Somebody messaging us privately: me = (@nickrx === to) # Somebody talking to us on channel: "Beerbot: ..." cmd = @get_nick_cmd.call(msg) if not cmd then # Somebody commanding us on channel: ",command ..." cmd = @get_prefix_cmd.call(msg) end if cmd then case cmd # dispatch help... when /^\s*help(?:\s+(.*))?$/ if $1.nil? then args = [] else args = $1.strip.split(/\s+/) end replies = @bot.help(args,from:from,to:to,me:me,config:@config) # dispatch cmd... else replies = @bot.cmd(cmd,from:from,to:to,me:me,config:@config) end else # We're just hearing something on a channel... replies = @bot.hear(msg,from:from,to:to,me:me,config:@config) end else puts "[dispatcher] unrecognised event: '#{event}'" end case replies when String # assume protocol string eg irc replies when Hash,Array,Proc BotMsg.to_a(replies) else [] end end |