Class: TD::Client
- Inherits:
-
Object
- Object
- TD::Client
- Defined in:
- lib/tdlib/client.rb
Overview
Simple client for TDLib.
Constant Summary collapse
- TIMEOUT =
10
Instance Method Summary collapse
-
#authorization_state ⇒ Hash
Returns current authorization state (it's offline request).
-
#broadcast(query) {|update| ... } ⇒ Object
Sends asynchronous request to the TDLib client.
-
#broadcast_and_receive(query, timeout: TIMEOUT) ⇒ Hash
Sends asynchronous request to the TDLib client and returns received update synchronously.
-
#close ⇒ Object
Stops update manager and destroys TDLib client.
-
#execute(query) ⇒ Object
Synchronously executes TDLib request Only a few requests can be executed synchronously.
-
#initialize(td_client = TD::Api.client_create, update_manager = TD::UpdateManager.new(td_client), **extra_config) ⇒ Client
constructor
A new instance of Client.
-
#on(update_type) {|update| ... } ⇒ Object
Binds passed block as a handler for updates with type of update_type.
- #on_ready(timeout: TIMEOUT, &_) ⇒ Object
Constructor Details
#initialize(td_client = TD::Api.client_create, update_manager = TD::UpdateManager.new(td_client), **extra_config) ⇒ Client
Returns a new instance of Client.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/tdlib/client.rb', line 63 def initialize(td_client = TD::Api.client_create, update_manager = TD::UpdateManager.new(td_client), **extra_config) @td_client = td_client @update_manager = update_manager @config = TD.config.client.to_h.merge(extra_config) @ready_condition = Celluloid::Condition.new @update_manager.run end |
Instance Method Details
#authorization_state ⇒ Hash
Returns current authorization state (it's offline request)
115 116 117 |
# File 'lib/tdlib/client.rb', line 115 def broadcast_and_receive('@type' => 'getAuthorizationState') end |
#broadcast(query) {|update| ... } ⇒ Object
Sends asynchronous request to the TDLib client
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/tdlib/client.rb', line 77 def broadcast(query) if block_given? extra = TD::Utils.generate_extra(query) handler = ->(update) do return unless update['@extra'] == extra yield update @update_manager.remove_handler(handler) end @update_manager.add_handler(handler) query['@extra'] = extra end TD::Api.client_send(@td_client, query) end |
#broadcast_and_receive(query, timeout: TIMEOUT) ⇒ Hash
Sends asynchronous request to the TDLib client and returns received update synchronously
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/tdlib/client.rb', line 94 def broadcast_and_receive(query, timeout: TIMEOUT) condition = Celluloid::Condition.new extra = TD::Utils.generate_extra(query) handler = ->(update) { condition.signal(update) if update['@extra'] == extra } @update_manager.add_handler(handler) query['@extra'] = extra TD::Api.client_send(@td_client, query) condition.wait(timeout) rescue Celluloid::ConditionError raise TD::TimeoutError end |
#close ⇒ Object
Stops update manager and destroys TDLib client
137 138 139 140 |
# File 'lib/tdlib/client.rb', line 137 def close @update_manager.stop TD::Api.client_destroy(@td_client) end |
#execute(query) ⇒ Object
Synchronously executes TDLib request Only a few requests can be executed synchronously
109 110 111 |
# File 'lib/tdlib/client.rb', line 109 def execute(query) TD::Api.client_execute(@td_client, query) end |
#on(update_type) {|update| ... } ⇒ Object
Binds passed block as a handler for updates with type of update_type
122 123 124 125 126 127 128 |
# File 'lib/tdlib/client.rb', line 122 def on(update_type, &_) handler = ->(update) do return unless update['@type'] == update_type yield update end @update_manager.add_handler(handler) end |
#on_ready(timeout: TIMEOUT, &_) ⇒ Object
130 131 132 133 134 |
# File 'lib/tdlib/client.rb', line 130 def on_ready(timeout: TIMEOUT, &_) yield self if @ready || @ready_condition.wait(timeout) rescue Celluloid::ConditionError raise TD::TimeoutError end |