Class: Botfly::Responder
- Inherits:
-
Object
- Object
- Botfly::Responder
- Extended by:
- Forwardable
- Includes:
- CommonResponderMethods
- Defined in:
- lib/botfly/responder/responder.rb
Direct Known Subclasses
MUCResponder, MessageResponder, SubscriptionRequestResponder
Constant Summary collapse
- @@id =
1
Instance Attribute Summary collapse
-
#bot ⇒ Object
readonly
Returns the value of attribute bot.
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#callback_type ⇒ Object
readonly
Returns the value of attribute callback_type.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
-
#callback_with(params) ⇒ Object
Attempt to execute the callback on a responder.
-
#initialize(bot, &block) ⇒ Responder
constructor
A new instance of Responder.
-
#method_missing(method, condition, &block) ⇒ self
Allows for the nifty DSL that lets you chain matchers after specifying a callback type.
Methods included from CommonResponderMethods
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) { … }
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
#bot ⇒ Object (readonly)
Returns the value of attribute bot.
7 8 9 |
# File 'lib/botfly/responder/responder.rb', line 7 def bot @bot end |
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
7 8 9 |
# File 'lib/botfly/responder/responder.rb', line 7 def callback @callback end |
#callback_type ⇒ Object (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 |
#id ⇒ Object (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
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 |