Class: Readapt::DataReader
- Inherits:
-
Object
- Object
- Readapt::DataReader
- Defined in:
- lib/readapt/data_reader.rb
Instance Method Summary collapse
-
#initialize ⇒ DataReader
constructor
A new instance of DataReader.
-
#receive(data) ⇒ Object
Process raw data received from the client.
-
#set_message_handler {|The| ... } ⇒ Object
Declare a block to be executed for each message received from the client.
Constructor Details
#initialize ⇒ DataReader
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.
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 if @buffer.end_with?("\r\n\r\n") else 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.
13 14 15 |
# File 'lib/readapt/data_reader.rb', line 13 def &block @message_handler = block end |