Class: Rucoa::MessageReader

Inherits:
Object
  • Object
show all
Defined in:
lib/rucoa/message_reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ MessageReader

Returns a new instance of MessageReader.

Parameters:

  • io (IO)


8
9
10
11
# File 'lib/rucoa/message_reader.rb', line 8

def initialize(io)
  @io = io
  @io.binmode
end

Instance Method Details

#read {|message| ... } ⇒ Enumerator<Hash>, void

Yield Parameters:

  • message (Hash)

Returns:

  • (Enumerator<Hash>, void)


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rucoa/message_reader.rb', line 15

def read
  return enum_for(:read) unless block_given?

  while (buffer = @io.gets("\r\n\r\n"))
    content_length = buffer[/Content-Length: (\d+)/i, 1]
    raise Errors::ContentLengthHeaderNotFound unless content_length

    body = @io.read(content_length.to_i)
    raise Errors::ContentLengthHeaderNotFound unless body

    yield ::JSON.parse(body)
  end
end