Sha256: f2484d806a9352b301295215e49c18f6d89a8591425dc5a707390a323e9fccb3

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'tgbot/runner'

module Tgbot
  class DSL
    attr_accessor :runner
    def initialize(token, **opts)
      @runner = Runner.new(token, **opts)
      @procs = { command: {} }
    end
    def start(&blk)
      @procs[:start] = blk
    end
    def finish(&blk)
      @procs[:finish] = blk
    end
    def before(&blk)
      @procs[:before] = blk
    end
    def after(&blk)
      @procs[:after] = blk
    end
    def on(regex, &blk)
      @procs[:command][regex] = blk
    end
    alias get on
    def run
      yield self if block_given?
      @procs[:start]&.call
      begin
        @runner.mainloop do |update|
          @procs[:before]&.call update
          update.done = true
          @procs[:command].each do |key, blk|
            x = update.text&.match key
            blk.call x, update if x
          end
          @procs[:after]&.call update
        end
      rescue Interrupt
        @procs[:finish]&.call
      rescue => e
        puts $!
        puts e.backtrace
        retry
      end
    end
    def method_missing(meth, *args, &blk)
      @runner.send(meth, *args, &blk)
    end
  end
  def self.run(token, **opts, &blk)
    DSL.new(token, **opts).run(&blk)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tgbot-0.1.0 lib/tgbot/dsl.rb