Sha256: 424d9e94572d33718ce198955e54b5126be0d7e4d05a0935c7d92b4700117a6a

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 KB

Contents

module Contentful
  module Importer
    class ContentTypesStructureCreator

      attr_reader :config, :logger

      def initialize(config)
        @config = config
        @logger = Logger.new(STDOUT)
      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)
        logger.info "Saving #{content_type_name}.json to #{config.collections_dir}"
      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

5 entries across 5 versions & 1 rubygems

Version Path
contentful-importer-0.2.2 lib/contentful/importer/converters/content_types_structure_creator.rb
contentful-importer-0.2.1 lib/contentful/importer/converters/content_types_structure_creator.rb
contentful-importer-0.2.0 lib/contentful/importer/converters/content_types_structure_creator.rb
contentful-importer-0.1.1 lib/contentful/importer/converters/content_types_structure_creator.rb
contentful-importer-0.1.0 lib/contentful/importer/converters/content_types_structure_creator.rb