Sha256: dd7af1500ce1578e60ad192b07af336f2028e06ec10851cb02968e77e9e4d8c4

Contents?: true

Size: 1.36 KB

Versions: 12

Compression:

Stored size: 1.36 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)
        @out << "\"#{name}\": #{stringify_value(value)}#{last ? '' : ','}" unless value.nil?
      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)

        "\"#{value}\""
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
foreman_rh_cloud-0.9.12 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-1.0.12 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-2.0.12 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-0.9.11 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-1.0.11 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-2.0.11 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-0.9.10 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-1.0.10 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-2.0.10 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-2.0.9 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-1.0.9 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-0.9.9 lib/foreman_inventory_upload/generators/json_stream.rb