Sha256: 0e9b36f2479f205d62c53b910387978ce3ce625ae5d081aa38a64a213c7bdf5a

Contents?: true

Size: 1.23 KB

Versions: 8

Compression:

Stored size: 1.23 KB

Contents

module Telegram
  module Bot
    # Stubbed client for tests. Saves all requests into #requests hash.
    class ClientStub < Client
      attr_reader :requests

      module StubbedConstructor
        def new(*args)
          if self == ClientStub || !ClientStub.stub_all?
            super
          else
            ClientStub.new(*args)
          end
        end
      end

      class << self
        # Any call to Client.new will return ClientStub instance when `enabled` is true.
        # Can be used with a block.
        def stub_all!(enabled = true)
          Client.extend(StubbedConstructor) unless Client < StubbedConstructor
          return @_stub_all = enabled unless block_given?
          begin
            old = @_stub_all
            stub_all!(enabled)
            yield
          ensure
            stub_all!(old)
          end
        end

        def stub_all?
          @_stub_all
        end
      end

      def initialize(token = nil, username = nil, **options)
        @username = username || options[:username] || token
        reset
      end

      def reset
        @requests = Hash.new { |h, k| h[k] = [] }
      end

      def request(action, body = {})
        requests[action.to_sym] << body
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
telegram-bot-0.11.3 lib/telegram/bot/client_stub.rb
telegram-bot-0.11.2 lib/telegram/bot/client_stub.rb
telegram-bot-0.11.1 lib/telegram/bot/client_stub.rb
telegram-bot-0.11.0 lib/telegram/bot/client_stub.rb
telegram-bot-0.10.2 lib/telegram/bot/client_stub.rb
telegram-bot-0.10.1 lib/telegram/bot/client_stub.rb
telegram-bot-0.10.0 lib/telegram/bot/client_stub.rb
telegram-bot-0.9.0 lib/telegram/bot/client_stub.rb