Sha256: 0e314c93384480d7c475459b22310b56ff92f43f01f24d4a4d84d21d8178bbc3

Contents?: true

Size: 1.53 KB

Versions: 79

Compression:

Stored size: 1.53 KB

Contents

module ForemanInventoryUpload
  module Generators
    class JsonStream
      attr_reader :out
      def initialize(out)
        @out = out
      end

      def array
        @out << '['
        yield
        @out << ']'
      end

      def object
        @out << '{'
        yield
        @out << '}'
      end

      def comma
        @out << ', '
      end

      def raw(string)
        @out << string
      end

      def simple_field(name, value, last = false, &block)
        return if value.nil? || value.try(:empty?)
        return if value.kind_of?(Array) && value.compact.empty?

        block ||= ->(value) { value }

        @out << "\"#{name}\": #{stringify_value(block.call(value))}#{last ? '' : ','}"
      end

      def array_field(name, last = false, &block)
        @out << "\"#{name}\": "
        array(&block)
        @out << ',' unless last
      end

      def string_array_value(name, value, last = false)
        return if value.empty?

        string_value = value.map { |v| stringify_value(v) }

        array_field(name, last) do
          raw(string_value.join(', '))
        end
      end

      def object_field(name, last = false, &block)
        @out << "\"#{name}\": "
        object(&block)
        @out << ',' unless last
      end

      def stringify_value(value)
        return value if value.is_a?(Integer)
        return value if value.is_a?(TrueClass)
        return value if value.is_a?(FalseClass)
        return value.to_json if value.is_a?(Hash)

        ActiveSupport::JSON.encode(value)
      end
    end
  end
end

Version data entries

79 entries across 79 versions & 1 rubygems

Version Path
foreman_rh_cloud-5.0.46 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-5.0.45 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-7.0.46 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-5.0.44 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-7.0.45 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-6.0.44 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-5.0.43 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-4.0.36 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-3.0.33 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-6.0.42.2 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-6.0.42.1 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-6.0.43 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-5.0.42 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-6.0.42 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-5.0.41 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-5.0.39 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-5.0.38 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-4.0.35 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-5.0.37 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-5.0.36 lib/foreman_inventory_upload/generators/json_stream.rb