Class: Bender::BaseProcessor Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/base_processer.rb

Overview

This class is abstract.

Subclass and override #process to implement

The base class for all Processors

-- Provides helpers for working with SpunkBot
Author

Michael Heijmans ([email protected])

Copyright

Copyright © 2013-2015 Michael Heijmans

License

MIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseProcessor

Returns a new instance of BaseProcessor


27
28
29
# File 'lib/base_processer.rb', line 27

def initialize
  @mutex   = Mutex.new
end

Instance Attribute Details

#botObject (readonly)

The SpunkBot object


25
# File 'lib/base_processer.rb', line 25

attr_reader :command, :msg, :bot, :room, :origin

#commandObject (readonly)

The IRC command issued


25
26
27
# File 'lib/base_processer.rb', line 25

def command
  @command
end

#msgObject (readonly)

The IRC message if any


25
# File 'lib/base_processer.rb', line 25

attr_reader :command, :msg, :bot, :room, :origin

#originObject (readonly)

Returns the value of attribute origin


25
# File 'lib/base_processer.rb', line 25

attr_reader :command, :msg, :bot, :room, :origin

#roomObject (readonly)

The room that the command was issued from if any


25
# File 'lib/base_processer.rb', line 25

attr_reader :command, :msg, :bot, :room, :origin

Instance Method Details

#call(opts) ⇒ Object

Note:

This method should not be used within the context of a Bender::Processor, use #process instead

The raw call that SpunkBot uses when a line of text is captured by the bot.

See Also:


34
35
36
37
38
39
40
41
42
43
# File 'lib/base_processer.rb', line 34

def call(opts)
  @mutex.synchronize {
    @command  = opts[:command]
    @origin   = opts[:origin]
    @msg      = opts[:msg]
    @bot      = opts[:bot]
    @room     = opts[:room]
  }
  process
end

#directly_addressed?Boolean (protected)

A helper used by #respond to test if Bender is being addressed directly

Returns:

  • (Boolean)

See Also:


86
87
88
89
90
# File 'lib/base_processer.rb', line 86

def directly_addressed?
  return false unless @msg
  return true if @msg.match(/^\@?#{bot.nickname}/i)
  return false
end

#hear(regexp) {|match| ... } ⇒ Object

A helper for listening for a match to `regexp` and processing it in the block

Examples:

Hear “sweet” and reply with “dude”

hear( 'sweet' ) { reply "dude" }

Yields:

  • (match)

56
57
58
59
60
61
# File 'lib/base_processer.rb', line 56

def hear(regexp, &block)
  return unless should_process?
  return unless @msg
  match = @msg.match(regexp)
  yield(match) if match
end

#processObject

Note:

Use this method in your processors to make Bender do things

Called on each line that SpunkBot sees

Raises:

  • (NotImplementedError)

47
48
49
50
# File 'lib/base_processer.rb', line 47

def process
  obj = self
  raise NotImplementedError, "you must implement #process method for #{obj.class}"
end

#reply(message) ⇒ Object

A helper for responding to a given room with a `message`


77
78
79
# File 'lib/base_processer.rb', line 77

def reply(message)
  bot.say @room, message if @room
end

#respond(regexp) {|msg, match| ... } ⇒ Object

A helper for responding when the bot's nickname is directly addressed and processing in a block

Examples:

“bender hello” replies with “hi!”

respond( 'hello' ) { reply "hi!" } #=> "bender hello" :: replies with "hi!"
respond( 'hello' ) { reply "hi!" } #=> "hello" :: nothing is sent

Yields:


68
69
70
71
72
73
74
# File 'lib/base_processer.rb', line 68

def respond(regexp, &block)
  return unless directly_addressed?
  return unless should_process?
  msg = @msg[@bot.nickname.length,@msg.length]
  match = msg.match(regexp)
  yield(msg, match) if match
end

#should_process?Boolean (protected)

A helper used by #hear and #respond to test if the given string is something we care about

Returns:

  • (Boolean)

See Also:


96
97
98
99
100
101
# File 'lib/base_processer.rb', line 96

def should_process?
  return false if @bot.nil?
  return false if @origin.nil?
  return false if @origin.nickname == @bot.nickname
  true
end