Class: Contentful::Importer::ContentfulModelToJson

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful/importer/converters/contentful_model_to_json.rb

Constant Summary collapse

FIELD_TYPE =
%w( Link Array )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ ContentfulModelToJson

Returns a new instance of ContentfulModelToJson.



10
11
12
13
# File 'lib/contentful/importer/converters/contentful_model_to_json.rb', line 10

def initialize(settings)
  @config = settings
  @logger = Logger.new(STDOUT)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/contentful/importer/converters/contentful_model_to_json.rb', line 6

def config
  @config
end

#content_typesObject (readonly)

Returns the value of attribute content_types.



6
7
8
# File 'lib/contentful/importer/converters/contentful_model_to_json.rb', line 6

def content_types
  @content_types
end

#converted_model_dirObject (readonly)

Returns the value of attribute converted_model_dir.



6
7
8
# File 'lib/contentful/importer/converters/contentful_model_to_json.rb', line 6

def converted_model_dir
  @converted_model_dir
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/contentful/importer/converters/contentful_model_to_json.rb', line 6

def logger
  @logger
end

Instance Method Details

#content_type_name(content_type) ⇒ Object



69
70
71
# 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_formObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# 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_jsonObject



15
16
17
18
19
20
21
22
23
24
# 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) ⇒ Object



73
74
75
# 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_fileObject



88
89
90
91
# 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?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/contentful/importer/converters/contentful_model_to_json.rb', line 84

def file_exists?
  File.exists?(config.config['contentful_structure_dir'])
end


61
62
63
64
65
66
67
# File 'lib/contentful/importer/converters/contentful_model_to_json.rb', line 61

def link_id(field)
  if FIELD_TYPE.include? field['type']
    field['name'].capitalize
  else
    field['id']
  end
end

#load_contentful_structure_fileObject

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.



79
80
81
82
# 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_fileObject



93
94
95
# 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_parametersObject



97
98
99
100
101
# 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_filesObject



103
104
105
106
# 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