Class: Rucoa::MessageWriter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ MessageWriter

Returns a new instance of MessageWriter.

Parameters:

  • io (IO)


26
27
28
29
# File 'lib/rucoa/message_writer.rb', line 26

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

Instance Attribute Details

#ioIO (readonly)

Returns:

  • (IO)


23
24
25
# File 'lib/rucoa/message_writer.rb', line 23

def io
  @io
end

Class Method Details

.pack(message) ⇒ String

Parameters:

  • message (Hash)

Returns:

  • (String)


16
17
18
19
# File 'lib/rucoa/message_writer.rb', line 16

def pack(message)
  body = DEFAULT_MESSAGE.merge(message).to_json
  "Content-Length: #{body.bytesize}\r\n\r\n#{body}"
end

Instance Method Details

#write(message) ⇒ void

This method returns an undefined value.

Parameters:

  • message (Hash)


33
34
35
36
37
38
# File 'lib/rucoa/message_writer.rb', line 33

def write(message)
  @io.print(
    self.class.pack(message)
  )
  @io.flush
end