Sha256: 65a2df7be0e3010d8dd8db38ae256b9ed67adf56dcd5f5dc15e6c775e4d86046

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Contentful
  module Converter
    class ContentTypesStructureCreator

      attr_reader :config

      def initialize(config)
        @config = config
      end

      def create_content_type_json_file(content_type_name, values)
        collection = {
            id: values[:id],
            name: values[:name],
            description: values[:description],
            displayField: values[:displayField],
            fields: create_fields(values[:fields])
        }
        write_json_to_file("#{config.collections_dir}/#{content_type_name}.json", collection)
      end

      def create_fields(fields)
        fields.each_with_object([]) do |(field, value), results|
          results << {
              name: create_field(field, value).capitalize,
              id: create_field(field, value),
              type: create_type_field(value),
              link_type: create_link_type_field(value),
              link: create_link_field(value)
          }.compact
        end
      end

      def create_field(field, value)
        value.is_a?(Hash) ? value[:id] : field
      end

      def create_link_type_field(value)
        value.is_a?(Hash) ? value[:link_type] : nil
      end

      def create_type_field(value)
        value.is_a?(Hash) ? value[:type] : value
      end

      def create_link_field(value)
        value.is_a?(Hash) ? value[:link] : nil
      end

      def write_json_to_file(path, data)
        File.open(path, 'w') do |file|
          file.write(JSON.pretty_generate(data))
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
database-exporter-0.0.2 lib/converters/content_types_structure_creator.rb
database-exporter-0.0.1 lib/converters/content_types_structure_creator.rb