Class: Twibot::Handler

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

Overview

A Handler object is an object which can handle a direct message, tweet or at reply.

Instance Method Summary collapse

Constructor Details

#initialize(pattern = nil, options = {}, &blk) ⇒ Handler

Returns a new instance of Handler.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/twibot/handlers.rb', line 33

def initialize(pattern = nil, options = {}, &blk)
  if pattern.is_a?(Hash)
    options = pattern
    pattern = nil
  end

  @options = 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 Method Details

#dispatch(message) ⇒ Object

Process message to build params hash and pass message along with params of to handle



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/twibot/handlers.rb', line 80

def dispatch(message)
  return unless recognize?(message)
  @params = {}

  if @options[:pattern]
    matches = message.text.match(@options[:pattern])
    @options[:tokens].each_with_index { |token, i| @params[token] = matches[i+1] }
    @params[:text] = (matches[@options[:tokens].length+1] || "").strip
  else
    @params[:text] = message.text
  end

  handle(message, @params)
end

#handle(message, params) ⇒ Object

Handle a message. Calls the internal Proc with the message and the params hash as parameters.



99
100
101
# File 'lib/twibot/handlers.rb', line 99

def handle(message, params)
  @handler.call(message, params) if @handler
end

#pattern=(pattern) ⇒ Object

Parse pattern string and set options



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/twibot/handlers.rb', line 50

def pattern=(pattern)
  return if pattern.nil? || pattern == ""

  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

Returns:

  • (Boolean)


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

def recognize?(message)
  return false if @options[:pattern] && message.text !~ @options[:pattern] # Pattern check

  users = @options[:from] ? @options[:from] : nil
  return false if users && !users.include?(message.sender.screen_name) # Check allowed senders
  true
end