Class: Aws::EventStream::Decoder
- Inherits:
-
Object
- Object
- Aws::EventStream::Decoder
- Includes:
- Enumerable
- Defined in:
- gems/aws-eventstream/lib/aws-eventstream/decoder.rb
Overview
This class provides method for decoding binary inputs into single or multiple messages (Aws::EventStream::Message).
- #decode - decodes messages from an IO like object responds to #read that containing binary data, returning decoded Aws::EventStream::Message along the way or wrapped in an enumerator
Examples
decoder = Aws::EventStream::Decoder.new
# decoding from IO decoder.decode(io) do |message| message.headers # => { ... } message.payload # => StringIO / Tempfile end
# alternatively message_pool = decoder.decode(io) message_pool.next # => Aws::EventStream::Message
- #decode_chunk - decodes a single message from a chunk of data, returning message object followed by boolean(indicating eof status of data) in an array object
Examples
# chunk containing exactly one message data message, chunk_eof = decoder.decode_chunk(chunk_str) message # => Aws::EventStream::Message chunk_eof # => true
# chunk containing a partial message message, chunk_eof = decoder.decode_chunk(chunk_str) message # => nil chunk_eof # => true # chunk data is saved at decoder's message_buffer
# chunk containing more that one data message message, chunk_eof = decoder.decode_chunk(chunk_str) message # => Aws::EventStream::Message chunk_eof # => false # extra chunk data is saved at message_buffer of the decoder
Instance Method Summary collapse
-
#decode(io) {|message| ... } ⇒ Enumerable<Message>?
Decodes messages from a binary stream.
-
#decode_chunk(chunk = nil) ⇒ Array<Message|nil, Boolean>
Decodes a single message from a chunk of string.
-
#initialize(options = {}) ⇒ Decoder
constructor
A new instance of Decoder.
Constructor Details
#initialize(options = {}) ⇒ Decoder
Returns a new instance of Decoder.
83 84 85 86 |
# File 'gems/aws-eventstream/lib/aws-eventstream/decoder.rb', line 83 def initialize( = {}) @format = .fetch(:format, true) @message_buffer = '' end |
Instance Method Details
#decode(io) {|message| ... } ⇒ Enumerable<Message>?
Decodes messages from a binary stream
96 97 98 99 100 101 102 103 |
# File 'gems/aws-eventstream/lib/aws-eventstream/decoder.rb', line 96 def decode(io, &block) = io.read = () return wrap_as_enumerator() unless block_given? # fetch message only raw_event, _eof = block.call(raw_event) end |
#decode_chunk(chunk = nil) ⇒ Array<Message|nil, Boolean>
Decodes a single message from a chunk of string
114 115 116 117 |
# File 'gems/aws-eventstream/lib/aws-eventstream/decoder.rb', line 114 def decode_chunk(chunk = nil) @message_buffer = [@message_buffer, chunk].pack('a*a*') if chunk (@message_buffer) end |