Sha256: 65a56ce024ce78e63c0f87a1962bdfd7a24dd08494e209d318f54f59ae486245

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

require_relative 'aggregator'
require_relative 'callbacks'

module Json
  module Streamer
    class Parser
      def initialize(event_generator, options = {})
        @event_generator = event_generator
        @symbolize_keys = options[:symbolize_keys]

        @aggregator = Aggregator.new
        @event_consumer = Callbacks.new(@aggregator)
      end

      def get(conditions)
        @event_generator.start_object { @event_consumer.start_object }
        @event_generator.start_array { @event_consumer.start_array }

        @event_generator.key do |k|
          @event_consumer.key(k, @symbolize_keys)
        end

        @event_generator.value do |v|
          @event_consumer.value(v) do |value|
            yield value if conditions.yield_value.call(aggregator: @aggregator, value: value)
          end
        end

        @event_generator.end_object do
          @event_consumer.end_object do |object|
            yield object if conditions.yield_object.call(aggregator: @aggregator, object: object)
          end
        end

        @event_generator.end_array do
          @event_consumer.end_array do |array|
            yield array if conditions.yield_array.call(aggregator: @aggregator, array: array)
          end
        end
      end

      def <<(data)
        @event_generator << data
      end

      def aggregator
        @aggregator.get
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
json-streamer-2.1.0 lib/json/streamer/parser.rb
json-streamer-2.0.1 lib/json/streamer/parser.rb
json-streamer-2.0.0 lib/json/streamer/parser.rb