Class: Telegram::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/telegram/authorization.rb

Instance Method Summary collapse

Constructor Details

#initialize(std_in_out, properties, logger) ⇒ Authorization

Returns a new instance of Authorization.



3
4
5
6
7
# File 'lib/telegram/authorization.rb', line 3

def initialize(std_in_out, properties, logger)
  @std_in_out = std_in_out
  @properties = properties
  @logger = logger
end

Instance Method Details

#performObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/telegram/authorization.rb', line 9

def perform
  stdout_log = ''

  # there is no success auth message from telegram-cli to stdou as result
  # no way detect finish of authorization or registration process (after
  # success auth message will be added to telegram-cli, watcher should be removed)
  watcher = Watcher.new do
    std_in_out.puts('get_self')
    logger.info 'Authorization: watcher fired get_self'
  end

  # low-level read of stdout which mean that we receive data by chuncks
  while (data = std_in_out.sysread(1024))
    watcher.stop
    stdout_log << data

    case stdout_log
    when /phone number:/
      stdout_log = ''
      handle_phone_number
    when /code \('CALL' for phone code\):/
      stdout_log = ''
      handle_confirmation_code
    when /register \(Y\/n\):/
      stdout_log = ''
      handle_registration
    when /"phone": "#{properties.phone_number}"/
      logger.info 'Authorization: successfully completed'
      return true
    else
      # ping telegram-cli after stdout is inactive 2 sec (expects that
      # there is no bigger delay between stdout and next stdin request)
      watcher.call_after(2)
    end
  end
end