Class: Jabbot::Handler
- Inherits:
-
Object
- Object
- Jabbot::Handler
- Defined in:
- lib/jabbot/handlers.rb
Overview
A Handler consists of a pattern to match a given message, some options and a handler block to be called on dispatch.
Instance Attribute Summary collapse
-
#pattern ⇒ Object
Public: Get the pattern RegExp.
Instance Method Summary collapse
-
#dispatch(message) ⇒ Object
Public: Process a message to build params hash and handle it.
-
#handle(message, params) ⇒ Object
Internal: Call the assigned message handler if any.
-
#initialize(pattern = nil, options = {}, &blk) ⇒ Handler
constructor
Public: Initialize a new handler instance.
-
#recognize?(message) ⇒ Boolean
Internal: Determines if this handler is suited to handle an incoming message.
Constructor Details
#initialize(pattern = nil, options = {}, &blk) ⇒ Handler
Public: Initialize a new handler instance.
pattern - The String, Symbol or Regexp pattern to match the messages
against or a Hash (default: nil).
If pattern is a Hash containing just one key :exact,
its value is used as the pattern and should therefore be
a String or Regexp.
If the pattern is a Hash, but not containing
the key :exact, the pattern is set to nil
and the passed value is re-used as `options`.
A pattern of nil will match every message.
options - The Hash options to refine the handler (default: {})
:from - A String, Symbol or Array of usernames to
accept messages from
* - Any String here is later used in the pattern
parsing and its value is used as a replacement
of the pattern parameter and should be a
valid String to be used in a Regexp,
containing just one match group.
blk - The block to handle a pattern-matched message
and respond to it.
It will be passed to arguments:
message - The actual Message struct.
params - An Array of matched params if any.
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 |
# File 'lib/jabbot/handlers.rb', line 77 def initialize(pattern = nil, = {}, &blk) @exact_match = false if pattern.is_a?(Hash) if pattern.keys.first == :exact @exact_match = true pattern = pattern[:exact] else = pattern pattern = nil end end @options = if from = @options[:from] if from.respond_to?(:collect) @options[:from] = from.collect {|s| s.to_s } elsif from.respond_to?(:to_s) @options[:from] = [@options[:from].to_s] else @options[:from] = nil end end @handler = block_given? ? blk : nil # Set pattern (parse it if needed) self.pattern = pattern ? pattern.dup : pattern end |
Instance Attribute Details
#pattern ⇒ Object
Public: Get the pattern RegExp.
171 172 173 |
# File 'lib/jabbot/handlers.rb', line 171 def pattern @pattern end |
Instance Method Details
#dispatch(message) ⇒ Object
Public: Process a message to build params hash and handle it.
message - The incoming String message.
Returns the response from ‘handle`.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/jabbot/handlers.rb', line 190 def dispatch() return unless recognize?() params = {} if @pattern && @tokens matches = .text.match(@pattern) @tokens.each_with_index {|token, i| params[token] = matches[i+1] } params[:text] = (matches[@tokens.length+1] || '').strip elsif @pattern && !@tokens params = .text.match(@pattern).to_a[1..-1] || [] else params[:text] = .text end handle(, params) end |
#handle(message, params) ⇒ Object
Internal: Call the assigned message handler if any.
message - The incoming String message. params - The hash containing matched tokens.
Returns the return from the handler block.
213 214 215 |
# File 'lib/jabbot/handlers.rb', line 213 def handle(, params) @handler.call(, params) if @handler end |
#recognize?(message) ⇒ Boolean
Internal: Determines if this handler is suited to handle
an incoming message.
Returns a Boolean if it recognized the given message.
177 178 179 180 181 182 183 |
# File 'lib/jabbot/handlers.rb', line 177 def recognize?() return false if @pattern && .text !~ @pattern users = @options[:from] ? @options[:from] : nil return false if users && !users.include?(.user) # Check allowed senders true end |