Sha256: 35f60a522678f3aa714f5aa1d78bffa2ad6e574fcef3df8d9a6d7d347a8be14d

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

require 'ostruct'
require 'active_support/inflector'

require_relative 'matcher'
require_relative 'message_proxy'

class TelegramBot
  module EventHandler
    class Handler
      attr_accessor :action, :pass, :matcher

      def initialize(matcher, action, pass)
        @matcher = matcher
        @action  = action
        @pass    = pass
      end

      def pass?
        @pass
      end

      def ===(msg)
        @matcher === msg
      end

      def arguments(msg)
        @matcher.arguments(msg)
      end
    end

    module PrependMethods
      attr_accessor :handlers

      def initialize(*args, &block)
        @handlers = []
        super(*args, &block)
      end
    end

    def self.included(clazz)
      clazz.prepend PrependMethods
    end


    def on(type, *args, pass: false, &block)
      matcher_class_name = "telegram_bot/#{type}_matcher".classify
      matcher_class = matcher_class_name.constantize
      matcher = matcher_class.new(*args)
      handler = Handler.new(matcher, block, pass)
      @handlers << handler
    end

    def handle(msg)
      @handlers.each do |hndlr|
        next unless hndlr === msg

        proxy = MessageProxy.new(self, msg, hndlr)
        proxy.instance_exec(*hndlr.arguments(msg),
                            &hndlr.action)

        break unless hndlr.pass?
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
telegram_bot_ruby-0.1.7 lib/telegram_bot/handler.rb
telegram_bot_ruby-0.1.6 lib/telegram_bot/handler.rb
telegram_bot_ruby-0.1.5 lib/telegram_bot/handler.rb