Class: MiddleSquid::Backends::Keyboard

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/middle_squid/backends/keyboard.rb

Overview

Receives data from the standard input.

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ Keyboard

Returns a new instance of Keyboard.

Parameters:

  • handler (#call)

    called when a full line has been received



5
6
7
8
# File 'lib/middle_squid/backends/keyboard.rb', line 5

def initialize(handler)
  @buffer = []
  @handler = handler
end

Instance Method Details

#receive_data(char) ⇒ Object

Parameters:

  • char (String)

    single character



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/middle_squid/backends/keyboard.rb', line 11

def receive_data(char)
  case char
  when "\x00"
    EM.stop
  when "\n"
    line = @buffer.join
    @buffer.clear

    receive_line line
  else
    @buffer << char
  end
end

#receive_line(line) ⇒ Object

Parameters:

  • line (String)

    full line without the trailing linebreak



26
27
28
29
# File 'lib/middle_squid/backends/keyboard.rb', line 26

def receive_line(line)
  # EventMachine sends ASCII-8BIT strings, somehow preventing the databases queries to match
  @handler.call line.force_encoding(Encoding::UTF_8)
end