Sha256: 15d2d47612c4b65bd5036cabf269d65bbd638debd87505c200c2532ff4cfb486

Contents?: true

Size: 1.78 KB

Versions: 36

Compression:

Stored size: 1.78 KB

Contents

require 'aws-eventstream'

module Aws
  module Binary
    # @api private
    class EventStreamDecoder

      # @param [String] protocol
      # @param [ShapeRef] rules ShapeRef of the eventstream member
      # @param [EventStream|nil] event_stream_handler A Service EventStream object
      # that registered with callbacks for processing events when they arrive
      def initialize(protocol, rules, io, event_stream_handler = nil)
        @decoder = Aws::EventStream::Decoder.new
        @event_parser = EventParser.new(parser_class(protocol), rules)
        @stream_class = extract_stream_class(rules.shape.struct_class)
        @emitter = event_stream_handler.event_emitter
        @events = []
      end

      # @return [Array] events Array of arrived event objects
      attr_reader :events

      def write(chunk)
        raw_event, eof = @decoder.decode_chunk(chunk)
        emit_event(raw_event) if raw_event
        while !eof
          # exhaust message_buffer data
          raw_event, eof = @decoder.decode_chunk
          emit_event(raw_event) if raw_event
        end
      end

      private

      def emit_event(raw_event)
        event = @event_parser.apply(raw_event)
        @events << event
        @emitter.signal(event.event_type, event) unless @emitter.nil?
      end

      def parser_class(protocol)
        case protocol
        when 'rest-xml' then Aws::Xml::Parser
        when 'rest-json' then Aws::Json::Parser
        else raise "unsupported protocol #{protocol} for event stream"
        end
      end

      def extract_stream_class(type_class)
        parts = type_class.to_s.split('::')
        parts.inject(Kernel) do |const, part_name|
          part_name == 'Types' ? const.const_get('EventStreams')
            : const.const_get(part_name)
        end
      end
    end

  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
aws-sdk-core-3.46.2 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.46.1 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.46.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.45.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.44.2 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.44.1 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.44.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.43.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.42.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.41.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.40.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.39.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.38.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.37.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.36.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.35.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.34.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.33.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.32.0 lib/aws-sdk-core/binary/event_stream_decoder.rb
aws-sdk-core-3.31.0 lib/aws-sdk-core/binary/event_stream_decoder.rb