Sha256: 2688271b871919ffda1f90962a76be5ec0bf21f37bcac325c4e8ed08a916b676

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

class Eco::API::Common::People::DefaultParsers::JsonParser < Eco::API::Common::Loaders::Parser
  attribute :json

  include Eco::API::Common::People::DefaultParsers::Helpers::ExpectedHeaders
  include Eco::API::Common::People::DefaultParsers::Helpers::NullParsing

  def parser(filename, deps)
    parse_json_file(filename).tap do |data|
      # Don't enable header checks for now
      next

      puts 'Identifying unified json object keys...'
      raw_headers = data.each_with_object([]) do |item, head|
        head.concat(item.keys - head)
      end

      require_headers!(raw_headers)

      next unless deps[:check_headers]
      next unless check_headers?

      check_headers!(
        data,
        order_check: false
      )
    end.each_with_object([]) do |item, arr_hash|
      item_hash = item.keys.each_with_object({}) do |attr, hash|
        next if attr.to_s.strip.empty?

        hash[attr.strip] = parse_null(item[attr])
      end

      arr_hash.push(item_hash)
    end
  end

  def serializer(array_hash, _deps)
    array_hash.to_json
  end

  private

  def check_headers?
    !options.dig(:input, :header_check, :skip)
  end

  def parse_json_file(filename)
    fd = File.open(filename)
    JSON.load fd # rubocop:disable Security/JSONLoad
  rescue JSON::ParserError => err
    log(:error) { "Parsing error on file '#{filename}'" }
    raise err
  ensure
    fd&.close
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/api/common/people/default_parsers/json_parser.rb
eco-helpers-3.0.20 lib/eco/api/common/people/default_parsers/json_parser.rb
eco-helpers-3.0.19 lib/eco/api/common/people/default_parsers/json_parser.rb