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 a 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.
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 |
# File 'lib/jabbot/handlers.rb', line 67 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.
161 162 163 |
# File 'lib/jabbot/handlers.rb', line 161 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`.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/jabbot/handlers.rb', line 180 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.
Both arguments are passed to the underlying handler block.
Returns the return from the handler block.
205 206 207 |
# File 'lib/jabbot/handlers.rb', line 205 def handle(, params) @handler.call(, params) if @handler end |
#recognize?(message) ⇒ Boolean
Internal: Determines if this handler is suited to handle
a message.
Returns true if it recognized the given message, false otherwise.
167 168 169 170 171 172 173 |
# File 'lib/jabbot/handlers.rb', line 167 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 |