Class: Twibot::Handler
- Inherits:
-
Object
- Object
- Twibot::Handler
- Defined in:
- lib/twibot/handlers.rb
Overview
A Handler object is an object which can handle a direct message, tweet or at reply.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#dispatch(message) ⇒ Object
Process message to build params hash and pass message along with params of to
handle
. -
#handle(message, params) ⇒ Object
Handle a message.
-
#initialize(pattern = nil, options = {}, &blk) ⇒ Handler
constructor
A new instance of Handler.
-
#pattern=(pattern) ⇒ Object
Parse pattern string and set options.
-
#recognize?(message) ⇒ Boolean
Determines if this handler is suited to handle an incoming message.
Constructor Details
#initialize(pattern = nil, options = {}, &blk) ⇒ Handler
Returns a new instance of Handler.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/twibot/handlers.rb', line 44 def initialize(pattern = nil, = {}, &blk) if pattern.is_a?(Hash) = pattern pattern = nil end @options = @options[:from].collect! { |s| s.to_s } if @options[:from] && @options[:from].is_a?(Array) @options[:from] = [@options[:from].to_s] if @options[:from] && @options[:from].is_a?(String) @handler = nil @handler = block_given? ? blk : nil self.pattern = pattern end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
43 44 45 |
# File 'lib/twibot/handlers.rb', line 43 def @options end |
Instance Method Details
#dispatch(message) ⇒ Object
Process message to build params hash and pass message along with params of to handle
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/twibot/handlers.rb', line 97 def dispatch() return unless recognize?() @params = {} if @options[:pattern] && @options[:tokens] matches = .text.match(@options[:pattern]) @options[:tokens].each_with_index { |token, i| @params[token] = matches[i+1] } @params[:text] = (matches[@options[:tokens].length+1] || "").strip elsif @options[:pattern] && !@options[:tokens] @params = .text.match(@options[:pattern]).to_a[1..-1] || [] else @params[:text] = .text end handle(, @params) end |
#handle(message, params) ⇒ Object
Handle a message. Calls the internal Proc with the message and the params hash as parameters.
118 119 120 |
# File 'lib/twibot/handlers.rb', line 118 def handle(, params) @handler.call(, params) if @handler end |
#pattern=(pattern) ⇒ Object
Parse pattern string and set options
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/twibot/handlers.rb', line 61 def pattern=(pattern) return if pattern.nil? || pattern == "" if pattern.is_a?(Regexp) @options[:pattern] = pattern return end words = pattern.split.collect { |s| s.strip } # Get all words in pattern @options[:tokens] = words.inject([]) do |sum, token| # Find all tokens, ie :symbol :like :names next sum unless token =~ /^:.*/ # Don't process regular words sym = token.sub(":", "").to_sym # Turn token string into symbol, ie ":token" => :token regex = @options[sym] || '[^\s]+' # Fetch regex if configured, else use any character but space matching pattern.sub!(/(^|\s)#{token}(\s|$)/, '\1(' + regex.to_s + ')\2') # Make sure regex captures named switch sum << sym end @options[:pattern] = /#{pattern}(\s.+)?/ end |
#recognize?(message) ⇒ Boolean
Determines if this handler is suited to handle an incoming message
84 85 86 87 88 89 90 91 |
# File 'lib/twibot/handlers.rb', line 84 def recognize?() return false if @options[:pattern] && .text !~ @options[:pattern] # Pattern check users = @options[:from] ? @options[:from] : nil sender = .respond_to?(:sender) ? .sender : .user return false if users && !users.include?(sender.screen_name.downcase) # Check allowed senders true end |