Class: Telegram::Bot::ClientStub

Inherits:
Client
  • Object
show all
Defined in:
lib/telegram/bot/client_stub.rb

Overview

Stubbed client for tests. Saves all requests into #requests hash.

Defined Under Namespace

Modules: StubbedConstructor

Constant Summary

Constants inherited from Client

Telegram::Bot::Client::SERVER, Telegram::Bot::Client::URL_TEMPLATE

Constants included from Telegram::Bot::Client::ApiHelper

Telegram::Bot::Client::ApiHelper::METHODS_LIST_FILE

Constants included from Async

Async::MISSING_VALUE

Instance Attribute Summary collapse

Attributes inherited from Client

#base_uri, #client, #token, #username

Attributes included from Async

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

by_id, error_for_response, #http_request, #inspect, prepare_async_args, typed_response!, wrap

Methods included from Telegram::Bot::Client::ApiHelper

define_helpers, methods_list

Methods included from DebugClient

#debug!, #debug_off!

Methods included from Async

#async, #async=, prepare_hash, prepended, thread_store

Constructor Details

#initialize(token = nil, username = nil, **options) ⇒ ClientStub

rubocop:disable Lint/MissingSuper



39
40
41
42
43
# File 'lib/telegram/bot/client_stub.rb', line 39

def initialize(token = nil, username = nil, **options) # rubocop:disable Lint/MissingSuper
  @token = token || options[:token]
  @username = username || options[:username] || token
  reset
end

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests.



7
8
9
# File 'lib/telegram/bot/client_stub.rb', line 7

def requests
  @requests
end

Class Method Details

.stub_all!(enabled = true) ⇒ Object

Any call to Client.new will return ClientStub instance when ‘enabled` is true. Can be used with a block.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/telegram/bot/client_stub.rb', line 22

def stub_all!(enabled = true) # rubocop:disable Style/OptionalBooleanParameter
  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

.stub_all?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/telegram/bot/client_stub.rb', line 34

def stub_all?
  @_stub_all
end

Instance Method Details

#request(action, body = {}) ⇒ Object



49
50
51
# File 'lib/telegram/bot/client_stub.rb', line 49

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

#resetObject



45
46
47
# File 'lib/telegram/bot/client_stub.rb', line 45

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