Module: BeerBot::BotMsg

Defined in:
lib/beerbot/01.bot/botmsg.rb

Overview

BotMsg’s are hashes or arrays of hashes and represent a bot response (usually to some input eg from an irc server).

The hash is generally expected to have the following keys:

:msg
:to

but may carry additional ones like

:action (will be used instead of :msg)

The array-form of the botmsg might look like this:

[msg:'ho!',to:'#chan1']

and it is easy to add extra messages this way:

[msg:'ho!',to:'#chan1'] + [msg:'ho again!',to:'#chan1']

etc

Class Method Summary collapse

Class Method Details

.actionify(botmsg) ⇒ Object

Convert botmsg to an action if it starts with ‘*’.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/beerbot/01.bot/botmsg.rb', line 85

def self.actionify botmsg
  case botmsg
  when Hash
    case botmsg[:msg]
    when /^\*\s*/
      botmsg[:action] = botmsg[:msg].sub(/^\*\s*/,'')
      botmsg[:msg] = nil
    end
    botmsg
  when Array
    botmsg.map {|b|
      self.actionify(b)
    }
  else
    botmsg
  end
end

.to_a(botmsg) ⇒ Object

Convert botmsg to an array of one or more botmsg hashes.

Returns array of botmsg hashes or empty array if not given something that is valid.

Proc’s are executed to retrieve an array or hash.

Array => Array Hash => [Hash] Proc => [Hash]



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/beerbot/01.bot/botmsg.rb', line 68

def self.to_a botmsg
  case botmsg
  when Hash
    return [] unless self.valid?(botmsg)
    return [botmsg]
  when Array
    return [] unless self.valid?(botmsg)
    return botmsg
  when Proc
    return self.to_a(botmsg.call)
  else
    return []
  end
end

.to_reply_format(thing) ⇒ Object

Transform a botmsg (aka SINGLE RETURN FORMAT) or ARRAY FORMAT to ARRAY FORMAT (described below).

Background: Bot modules may reply in either format. But we convert it here. The single return format is the simplest, which is why we have 2 formats.

ARRAY FORMAT

[<bool>,<reply>]

where bool = true => “use <reply> and keep going”

= false => "use <reply> but stop here"

where reply = whatever the bot returns.

NOTE: any other type of array will assumed to be in single return form…

SINGLE RETURN FORMAT

This form assumes you return one thing, either nil/false or a botmsg. Returning nil/false won’t suppress subsequent modules from being evaluated.

Note, that “whatever the bot returns” will need to be one of the valid forms of a botmsg for it to get sent out.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/beerbot/01.bot/botmsg.rb', line 131

def self.to_reply_format thing
  case thing
  when Array
    bool,botmsg = thing
    case bool
    when TrueClass,FalseClass
      # Assume array format:
      [bool,self.to_a(botmsg)]
    else
      # Assume single return format...
      # Array of any sort is truthy, so suppress.
      [true,self.to_a(thing)]
    end
  else
    # Asume single return format...
    # 
    # Look at truthiness of thing to determine whether to suppress
    # further responses (true) or continue (false).
    if thing then
      [true,self.to_a(thing)]
    else
      [false,self.to_a(thing)]
    end
  end
end

.valid?(botmsg) ⇒ Boolean

Determine if botmsg is a valid botmsg.

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/beerbot/01.bot/botmsg.rb', line 36

def self.valid? botmsg
  case botmsg
  when Hash
    a = botmsg.has_key?(:to)
    b = (botmsg.has_key?(:msg) || botmsg.has_key?(:action))
    a && b
  when Array
    botmsg.inject(true){|s,v|
      # Members must be hash... 
      break false unless v.kind_of?(Hash)
      s = s && self.valid?(v);
      break false unless s;
      s
    }
  when Proc
    false
  else
    false
  end
end