Sha256: 08241adb387b23de5f43807b36d07f96a6da6cd3ee1ce3c6ef0ee0005ebc7345

Contents?: true

Size: 1.66 KB

Versions: 5

Compression:

Stored size: 1.66 KB

Contents

module Botfly
  class Responder
    @@id = 1
    include Botfly::CommonResponderMethods
    extend Forwardable

    attr_reader :callback, :callback_type, :id, :bot
    def_delegator :@bot, :client
    
    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
    
    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
    
    def callback_with(params)
      Botfly.logger.debug("    RSP: Launching callback with params: #{params.inspect}")

      setup_instance_variables(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
        instance_eval &cb
      end
    end

    def quit
      @bot.quit
    end
    # TODO: add other @client actions as delegates    

  private
    def add_matcher(method, condition)
      klass = Botfly.const_get(method.to_s.capitalize + "Matcher")
      @matcher_chain << klass.new(condition)
      
      Botfly.logger.debug("    RSP: Adding to matcher chain: #{@matcher_chain.inspect}")
    end

    def setup_instance_variables(params)
      raise "AbstractMethodError: You must implement this method in a concrete subclass"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
botfly-0.3.3 lib/botfly/responder/responder.rb
botfly-0.3.2 lib/botfly/responder/responder.rb
botfly-0.3.1 lib/botfly/responder/responder.rb
botfly-0.3.0 lib/botfly/responder/responder.rb
botfly-0.2.1 lib/botfly/responder/responder.rb