class Contentful::Importer::ContentfulModelToJson

Constants

FIELD_TYPE

Attributes

config[R]
content_types[R]
converted_model_dir[R]
logger[R]

Public Class Methods

new(settings) click to toggle source
# File lib/contentful/importer/converters/contentful_model_to_json.rb, line 10
def initialize(settings)
  @config = settings
  @logger = Logger.new(STDOUT)
end

Public Instance Methods

content_type_name(content_type) click to toggle source
# File lib/contentful/importer/converters/contentful_model_to_json.rb, line 69
def content_type_name(content_type)
  I18n.transliterate(content_type).underscore.tr(' ', '_')
end
convert_to_import_form() click to toggle source
# File lib/contentful/importer/converters/contentful_model_to_json.rb, line 26
def convert_to_import_form
  set_content_model_parameters
  logger.info 'Converting Contentful model to Contentful import structure...'
  File.open(converted_model_dir, 'w') { |file| file.write({}) }
  content_type_file = JSON.parse(File.read(content_types))['items']
  content_type_file.each do |content_type|
    parsed_content_type = {
        id: content_type['sys']['id'],
        name: content_type['name'],
        description: content_type['description'],
        displayField: content_type['displayField'],
        fields: create_content_type_fields(content_type)
    }
    import_form = JSON.parse(File.read(converted_model_dir))
    File.open(converted_model_dir, 'w') do |file|
      file.write(JSON.pretty_generate(import_form.merge!(content_type['name'] => parsed_content_type)))
    end
  end
  logger.info "Done! Contentful import structure file saved in #{converted_model_dir}"
end
create_content_type_fields(content_type) click to toggle source
# File lib/contentful/importer/converters/contentful_model_to_json.rb, line 47
def create_content_type_fields(content_type)
  content_type['fields'].each_with_object({}) do |(field, _value), results|
    id = link_id(field)
    results[id] = case field['type']
                    when 'Link'
                      {id: field['id'], type: field['linkType'], link: 'Link'}
                    when 'Array'
                      {id: field['id'], type: field['type'], link_type: field['items']['linkType'], link: field['items']['type']}
                    else
                      field['type']
                  end
  end
end
create_content_type_json() click to toggle source
# File lib/contentful/importer/converters/contentful_model_to_json.rb, line 15
def create_content_type_json
  contentful_structure = load_contentful_structure_file
  logger.info 'Create JSON files with content types structure...'
  contentful_structure.each do |content_type, values|
    content_type_name = content_type_name(content_type)
    create_directory(config.collections_dir)
    ContentTypesStructureCreator.new(config).create_content_type_json_file(content_type_name, values)
  end
  logger.info 'Done!'
end
create_directory(path) click to toggle source
# File lib/contentful/importer/converters/contentful_model_to_json.rb, line 73
def create_directory(path)
  FileUtils.mkdir_p(path) unless File.directory?(path)
end
create_empty_contentful_structure_file() click to toggle source
# File lib/contentful/importer/converters/contentful_model_to_json.rb, line 88
def create_empty_contentful_structure_file
  File.open(config.config['contentful_structure_dir'], 'w') { |file| file.write({}) }
  load_existing_contentful_structure_file
end
file_exists?() click to toggle source
# File lib/contentful/importer/converters/contentful_model_to_json.rb, line 84
def file_exists?
  File.exists?(config.config['contentful_structure_dir'])
end
load_contentful_structure_file() click to toggle source

If contentful_structure JSON file exists, it will load the file. If not, it will automatically create an empty file. This file is required to convert contentful model to contentful import structure.

# File lib/contentful/importer/converters/contentful_model_to_json.rb, line 79
def load_contentful_structure_file
  fail ArgumentError, 'Set PATH for contentful structure JSON file. View README' if config.config['contentful_structure_dir'].nil?
  file_exists? ? load_existing_contentful_structure_file : create_empty_contentful_structure_file
end
load_existing_contentful_structure_file() click to toggle source
# File lib/contentful/importer/converters/contentful_model_to_json.rb, line 93
def load_existing_contentful_structure_file
  JSON.parse(File.read(config.config['contentful_structure_dir']), symbolize_names: true).with_indifferent_access
end
set_content_model_parameters() click to toggle source
# File lib/contentful/importer/converters/contentful_model_to_json.rb, line 97
def set_content_model_parameters
  validate_content_model_files
  @converted_model_dir = config.config['converted_model_dir']
  @content_types = config.config['content_model_json']
end
validate_content_model_files() click to toggle source
# File lib/contentful/importer/converters/contentful_model_to_json.rb, line 103
def validate_content_model_files
  fail ArgumentError, 'Set PATH for content model JSON file. View README' if config.config['content_model_json'].nil?
  fail ArgumentError, 'Set PATH where converted content model file will be saved. View README' if config.config['converted_model_dir'].nil?
end