Sha256: 3bbbc7078cbb10037f77623ed94c11a59f8b1202dd384f868012396af643f472

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require "json/stream"

require_relative 'conditions'
require_relative 'parser'

module Json
  module Streamer
    class JsonStreamer

      attr_reader :parser

      def initialize(file_io = nil, chunk_size = 1000)
        @event_generator = JSON::Stream::Parser.new

        @file_io = file_io
        @chunk_size = chunk_size
      end

      def <<(data)
        parser << data
      end

      def get(nesting_level: -1, key: nil, yield_values: true, symbolize_keys: false)
        conditions = Conditions.new(yield_level: nesting_level, yield_key: key)
        conditions.yield_value = ->(aggregator:, value:) { false } unless yield_values

        # TODO: deprecate symbolize_keys and move to initialize
        @parser = Parser.new(@event_generator, symbolize_keys: symbolize_keys)

        parser.get(conditions) do |obj|
          yield obj
        end

        process_io
      end

      def get_with_conditions(conditions, options = {})
        @parser = Parser.new(@event_generator, symbolize_keys: options[:symbolize_keys])

        parser.get(conditions) do |obj|
          yield obj
        end

        process_io
      end

      def aggregator
        parser.aggregator
      end

      private

      def process_io
        @file_io.each(@chunk_size) { |chunk| parser << chunk } if @file_io
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
json-streamer-2.0.1 lib/json/streamer/json_streamer.rb
json-streamer-2.0.0 lib/json/streamer/json_streamer.rb