Class: Botfly::Responder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
CommonResponderMethods
Defined in:
lib/botfly/responder/responder.rb

Constant Summary collapse

@@id =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommonResponderMethods

#remove, #tell

Constructor Details

#initialize(bot, &block) ⇒ Responder

Returns a new instance of Responder.



11
12
13
14
15
16
17
# File 'lib/botfly/responder/responder.rb', line 11

def initialize(bot,&block)
  Botfly.logger.info("RSP: #{self.class.to_s}#new")
  @matcher_chain = []
  @bot = bot
  @callback = block if block_given?
  @id = @@id += 1
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, condition, &block) ⇒ self

Allows for the nifty DSL that lets you chain matchers after specifying a callback type.

e.g. on.message.matcher1(:foo).matcher2(:bar) { … }

Parameters:

  • method (String)

    The name of the matcher to be added to the matcher chain

  • condition

    The condition or set of conditions to be matched against when attempting execution.

Returns:

  • (self)

    Chain to your hearts content.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/botfly/responder/responder.rb', line 28

def method_missing(method,condition,&block)
  Botfly.logger.info("RSP: Responder##{method}(#{condition.inspect})")
  
  add_matcher(method,condition)
  
  if block_given?
    Botfly.logger.info("RSP: Callback recorded: #{block.inspect}")
    @callback = block
    return @id
  end
  
  return self
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



7
8
9
# File 'lib/botfly/responder/responder.rb', line 7

def bot
  @bot
end

#callbackObject (readonly)

Returns the value of attribute callback.



7
8
9
# File 'lib/botfly/responder/responder.rb', line 7

def callback
  @callback
end

#callback_typeObject (readonly)

Returns the value of attribute callback_type.



7
8
9
# File 'lib/botfly/responder/responder.rb', line 7

def callback_type
  @callback_type
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/botfly/responder/responder.rb', line 7

def id
  @id
end

Instance Method Details

#callback_with(params) ⇒ Object

Attempt to execute the callback on a responder. Only called if all matchers pass based on the supplied params

Parameters:

  • params (Hash)

    The parameters necessary to execute the callback. Supplied by xmpp4r and pass through to here.



46
47
48
49
50
51
52
53
54
55
# File 'lib/botfly/responder/responder.rb', line 46

def callback_with(params)
  Botfly.logger.debug("RSP: Launching callback with params: #{params.inspect}")

  context = callback_context(params)
  if @matcher_chain.all? {|matcher| matcher.match(params) }
    Botfly.logger.debug("RSP: All matchers passed")
    cb = @callback # Ruby makes it difficult to apply & to an instance variable
    context.instance_eval &cb
  end
end