Class: Readapt::DataReader

Inherits:
Object
  • Object
show all
Defined in:
lib/readapt/data_reader.rb

Instance Method Summary collapse

Constructor Details

#initializeDataReader

Returns a new instance of DataReader.



3
4
5
6
7
# File 'lib/readapt/data_reader.rb', line 3

def initialize
  @in_header = true
  @content_length = 0
  @buffer = String.new
end

Instance Method Details

#receive(data) ⇒ Object

Process raw data received from the client. The data will be parsed into messages based on the JSON-RPC protocol. Each message will be passed to the block declared via set_message_handler. Incomplete data will be buffered and subsequent data will be appended to the buffer.

Parameters:

  • data (String)


23
24
25
26
27
28
29
30
31
32
# File 'lib/readapt/data_reader.rb', line 23

def receive data
  data.each_char do |char|
    @buffer.concat char
    if @in_header
      prepare_to_parse_message if @buffer.end_with?("\r\n\r\n")
    else
      parse_message_from_buffer if @buffer.bytesize == @content_length
    end
  end
end

#set_message_handler {|The| ... } ⇒ Object

Declare a block to be executed for each message received from the client.

Yield Parameters:

  • The (Hash)

    message received from the client



13
14
15
# File 'lib/readapt/data_reader.rb', line 13

def set_message_handler &block
  @message_handler = block
end