Class: NNQ::CLI::Formatter
- Inherits:
-
Object
- Object
- NNQ::CLI::Formatter
- Defined in:
- lib/nnq/cli/formatter.rb
Overview
Handles encoding/decoding a single-body message in the configured format. Compression is handled by the NNQ::Zstd decorator around the socket, not by the formatter.
Unlike omq-cli’s Formatter, nnq messages are not multipart — one ‘String` body per message. The API takes and returns a plain `String`.
Class Method Summary collapse
-
.preview(body) ⇒ String
Formats a message body for human-readable preview (logging).
- .preview_body(body) ⇒ Object
Instance Method Summary collapse
-
#decode(line) ⇒ String
Decodes a formatted input line into a message body.
-
#decode_marshal(io) ⇒ Object?
Decodes one Marshal object from the given IO stream.
-
#decode_msgpack(io) ⇒ Object?
Decodes one MessagePack object from the given IO stream.
-
#encode(msg) ⇒ String
Encodes a message body into a printable string for output.
-
#initialize(format) ⇒ Formatter
constructor
A new instance of Formatter.
Constructor Details
#initialize(format) ⇒ Formatter
Returns a new instance of Formatter.
14 15 16 |
# File 'lib/nnq/cli/formatter.rb', line 14 def initialize(format) @format = format end |
Class Method Details
.preview(body) ⇒ String
Formats a message body for human-readable preview (logging).
84 85 86 87 |
# File 'lib/nnq/cli/formatter.rb', line 84 def self.preview(body) body = body.to_s "(#{body.bytesize}B) #{preview_body(body)}" end |
.preview_body(body) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/nnq/cli/formatter.rb', line 90 def self.preview_body(body) bytes = body.b return "''" if bytes.empty? sample = bytes[0, 12] printable = sample.count("\x20-\x7e") if printable < sample.bytesize / 2 "[#{bytes.bytesize}B]" elsif bytes.bytesize > 12 "#{sample.gsub(/[^[:print:]]/, ".")}..." else sample.gsub(/[^[:print:]]/, ".") end end |
Instance Method Details
#decode(line) ⇒ String
Decodes a formatted input line into a message body.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/nnq/cli/formatter.rb', line 45 def decode(line) case @format when :ascii, :marshal line.chomp when :quoted "\"#{line.chomp}\"".undump when :raw line end end |
#decode_marshal(io) ⇒ Object?
Decodes one Marshal object from the given IO stream.
61 62 63 64 65 |
# File 'lib/nnq/cli/formatter.rb', line 61 def decode_marshal(io) Marshal.load(io) rescue EOFError, TypeError nil end |
#decode_msgpack(io) ⇒ Object?
Decodes one MessagePack object from the given IO stream.
72 73 74 75 76 77 |
# File 'lib/nnq/cli/formatter.rb', line 72 def decode_msgpack(io) @msgpack_unpacker ||= MessagePack::Unpacker.new(io) @msgpack_unpacker.read rescue EOFError nil end |
#encode(msg) ⇒ String
Encodes a message body into a printable string for output.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/nnq/cli/formatter.rb', line 24 def encode(msg) case @format when :ascii msg.b.gsub(/[^[:print:]\t]/, ".") << "\n" when :quoted msg.b.dump[1..-2] << "\n" when :raw msg # FIXME: are these really the wire bytes? when :msgpack MessagePack.pack(msg) when :marshal msg.inspect << "\n" end end |