Class: Telegram::Connection

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/telegram/connection.rb

Overview

Note:

Don’t make a connection directly to the telegram-cli

Telegram-CLI Connection

See Also:

Version:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initializeConnection

Initialize connection

Version:

  • 0.1.0



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.

Returns:

  • (Bool)

    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

Parameters:

  • messages (Array<String>)

    Messages that will be sent

Yield Parameters:

  • callback (Block)

    Callback block that will be called when finished



31
32
33
34
35
36
37
38
# File 'lib/telegram/connection.rb', line 31

def communicate(*messages, &callback)
  @available = false
  @data = ''
  @callback = callback
  messages = messages.first if messages.size == 1 and messages.first.is_a?(Array)
  messages = messages.join(' ') << "\n"
  send_data(messages)
end

#connected?Bool

Returns the availiability of current connection.

Returns:

  • (Bool)

    the availiability of current connection



72
73
74
# File 'lib/telegram/connection.rb', line 72

def connected?
  @connected
end

#connection_completedObject

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

Parameters:

  • block (Block)


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

Parameters:

  • block (Block)


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

#unbindObject

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