Class: Telegram::Connection
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- Telegram::Connection
- Defined in:
- lib/telegram/connection.rb
Overview
Don’t make a connection directly to the telegram-cli
Telegram-CLI Connection
Instance Method Summary collapse
-
#available? ⇒ Bool
The availiability of current connection.
-
#communicate(*messages) {|callback| ... } ⇒ Object
Communicate telegram-rb with telegram-cli connection.
-
#connected? ⇒ Bool
The availiability of current connection.
-
#connection_completed ⇒ Object
private
This method will be called by EventMachine when connection completed.
-
#initialize ⇒ Connection
constructor
Initialize connection.
-
#on_connect=(block) ⇒ Object
Set a block that will be called when connected.
-
#on_disconnect=(block) ⇒ Object
Set a block that will be called when disconnected.
-
#receive_data(data) ⇒ Object
private
This method will be called by EventMachine when data arrived then parse given data and execute callback method if exists.
-
#unbind ⇒ Object
private
This method will be called by EventMachine when connection unbinded.
Constructor Details
#initialize ⇒ Connection
Initialize connection
12 13 14 15 16 17 18 19 20 |
# File 'lib/telegram/connection.rb', line 12 def initialize super @connected = false @on_connect = nil @on_disconnect = nil @callback = nil @available = true @data = '' end |
Instance Method Details
#available? ⇒ Bool
Returns the availiability of current connection.
23 24 25 |
# File 'lib/telegram/connection.rb', line 23 def available? @available end |
#communicate(*messages) {|callback| ... } ⇒ Object
Communicate telegram-rb with telegram-cli connection
31 32 33 34 35 36 37 38 |
# File 'lib/telegram/connection.rb', line 31 def communicate(*, &callback) @available = false @data = '' @callback = callback = .first if .size == 1 and .first.is_a?(Array) = .join(' ') << "\n" send_data() end |
#connected? ⇒ Bool
Returns the availiability of current connection.
72 73 74 |
# File 'lib/telegram/connection.rb', line 72 def connected? @connected end |
#connection_completed ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method will be called by EventMachine when connection completed
58 59 60 61 |
# File 'lib/telegram/connection.rb', line 58 def connection_completed @connected = true @on_connect.call unless @on_connect.nil? end |
#on_connect=(block) ⇒ Object
Set a block that will be called when connected
43 44 45 |
# File 'lib/telegram/connection.rb', line 43 def on_connect=(block) @on_connect = block end |
#on_disconnect=(block) ⇒ Object
Set a block that will be called when disconnected
51 52 53 |
# File 'lib/telegram/connection.rb', line 51 def on_disconnect=(block) @on_disconnect = block end |
#receive_data(data) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method will be called by EventMachine when data arrived then parse given data and execute callback method if exists
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/telegram/connection.rb', line 80 def receive_data(data) @data << data return unless data.index("\n\n") begin result = _receive_data(@data) rescue raise result = nil end @callback.call(!result.nil?, result) unless @callback.nil? @callback = nil @available = true end |
#unbind ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method will be called by EventMachine when connection unbinded
66 67 68 69 |
# File 'lib/telegram/connection.rb', line 66 def unbind @connected = false @on_disconnect.call unless @on_disconnect.nil? end |