Class: Aws::EventStream::Decoder

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
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

Constructor Details

#initialize(options = {}) ⇒ Decoder

Returns a new instance of Decoder.

Parameters:

  • options (Hash) (defaults to: {})

    The initialization options.

Options Hash (options):

  • :format (Boolean) — default: true

    When ‘false` it disables user-friendly formatting for message header values including timestamp and uuid etc.



83
84
85
86
# File 'lib/aws-eventstream/decoder.rb', line 83

def initialize(options = {})
  @format = options.fetch(:format, true)
  @message_buffer = ''
end

Instance Method Details

#decode(io) {|message| ... } ⇒ Enumerable<Message>?

Decodes messages from a binary stream

Parameters:

  • io (IO#read)

    An IO-like object that responds to ‘#read`

Yield Parameters:

Returns:

  • (Enumerable<Message>, nil)

    Returns a new Enumerable containing decoded messages if no block is given



96
97
98
99
100
101
102
103
# File 'lib/aws-eventstream/decoder.rb', line 96

def decode(io, &block)
  raw_message = io.read
  decoded_message = decode_message(raw_message)
  return wrap_as_enumerator(decoded_message) unless block_given?
  # fetch message only
  raw_event, _eof = decoded_message
  block.call(raw_event)
end

#decode_chunk(chunk = nil) ⇒ Array<Message|nil, Boolean>

Decodes a single message from a chunk of string

Parameters:

  • chunk (String) (defaults to: nil)

    A chunk of string to be decoded, chunk can contain partial event message to multiple event messages When not provided, decode data from #message_buffer

Returns:

  • (Array<Message|nil, Boolean>)

    Returns single decoded message and boolean pair, the boolean flag indicates whether this chunk has been fully consumed, unused data is tracked at #message_buffer



114
115
116
117
# File 'lib/aws-eventstream/decoder.rb', line 114

def decode_chunk(chunk = nil)
  @message_buffer = [@message_buffer, chunk].pack('a*a*') if chunk
  decode_message(@message_buffer)
end