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, prepare_body, 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

Returns a new instance of ClientStub.



37
38
39
40
41
# File 'lib/telegram/bot/client_stub.rb', line 37

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

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests.



5
6
7
# File 'lib/telegram/bot/client_stub.rb', line 5

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.



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

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

.stub_all?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/telegram/bot/client_stub.rb', line 32

def stub_all?
  @_stub_all
end

Instance Method Details

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



47
48
49
# File 'lib/telegram/bot/client_stub.rb', line 47

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

#resetObject



43
44
45
# File 'lib/telegram/bot/client_stub.rb', line 43

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