Class: Contentful::Converter::ContentfulModelToJson

Inherits:
Object
  • Object
show all
Defined in:
lib/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/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/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/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/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/converters/contentful_model_to_json.rb', line 6

def logger
  @logger
end

#omit_content_modelObject (readonly)

Returns the value of attribute omit_content_model.



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

def omit_content_model
  @omit_content_model
end

Instance Method Details

#content_type_name(content_type) ⇒ Object



70
71
72
# File 'lib/converters/contentful_model_to_json.rb', line 70

def content_type_name(content_type)
  I18n.transliterate(content_type).underscore.tr(' ', '_')
end

#convert_to_import_formObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/converters/contentful_model_to_json.rb', line 27

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({}) }
  contentful_file = JSON.parse(File.read(content_types))['items']
  contentful_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: {}.merge!(create_contentful_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_json(omit_content_model_flag = true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/converters/contentful_model_to_json.rb', line 15

def create_content_type_json(omit_content_model_flag = true)
  @omit_content_model = omit_content_model_flag
  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_contentful_fields(content_type) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/converters/contentful_model_to_json.rb', line 48

def create_contentful_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_directory(path) ⇒ Object



74
75
76
# File 'lib/converters/contentful_model_to_json.rb', line 74

def create_directory(path)
  FileUtils.mkdir_p(path) unless File.directory?(path)
end

#create_empty_contentful_structure_fileObject



93
94
95
96
# File 'lib/converters/contentful_model_to_json.rb', line 93

def create_empty_contentful_structure_file
  File.open(config.settings['contentful_structure_dir'], 'w') { |file| file.write({}) }
  load_existing_contentful_structure_file
end

#file_exists?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/converters/contentful_model_to_json.rb', line 89

def file_exists?
  omit_content_model == false ? true : File.exist?(config.settings['contentful_structure_dir'])
end


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

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.



80
81
82
83
# File 'lib/converters/contentful_model_to_json.rb', line 80

def load_contentful_structure_file
  fail ArgumentError, 'Set PATH for contentful structure JSON file. View README' if omit_content_model?
  file_exists? ? load_existing_contentful_structure_file : create_empty_contentful_structure_file
end

#load_existing_contentful_structure_fileObject



98
99
100
101
102
103
104
105
# File 'lib/converters/contentful_model_to_json.rb', line 98

def load_existing_contentful_structure_file
  if omit_content_model == true
    JSON.parse(File.read(config.settings['contentful_structure_dir']), symbolize_names: true).with_indifferent_access
  else
    spec = Gem::Specification.find_by_name('wordpress-exporter')
    JSON.parse(File.read("#{spec.gem_dir}/wordpress_settings/default_contentful_structure.json"), symbolize_names: true).with_indifferent_access
  end
end

#omit_content_model?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/converters/contentful_model_to_json.rb', line 85

def omit_content_model?
  omit_content_model == true ? config.settings['contentful_structure_dir'].nil? : false
end

#set_content_model_parametersObject



107
108
109
110
111
# File 'lib/converters/contentful_model_to_json.rb', line 107

def set_content_model_parameters
  validate_content_model_files
  @converted_model_dir = config.settings['converted_model_dir']
  @content_types = config.settings['content_model_json']
end

#validate_content_model_filesObject



113
114
115
116
# File 'lib/converters/contentful_model_to_json.rb', line 113

def validate_content_model_files
  fail ArgumentError, 'Set PATH for content model JSON file. View README' if config.settings['content_model_json'].nil?
  fail ArgumentError, 'Set PATH where converted content model file will be saved. View README' if config.settings['converted_model_dir'].nil?
end