Class: ContentfulImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful_importer.rb

Constant Summary collapse

ENTRIES_IDS =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContentfulImporter

Returns a new instance of ContentfulImporter.



8
9
10
# File 'lib/contentful_importer.rb', line 8

def initialize
  Contentful::Management::Client.new(CREDENTIALS['ACCESS_TOKEN'])
end

Instance Attribute Details

#spaceObject (readonly)

Returns the value of attribute space.



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

def space
  @space
end

Instance Method Details

#create_spaceObject



36
37
38
39
40
# File 'lib/contentful_importer.rb', line 36

def create_space
  puts 'Name for a new created space on Contentful:'
  name_space = gets.strip
  @space = Contentful::Management::Space.create(name: name_space, organization_id: CREDENTIALS['ORGANIZATION_ID'])
end

#find_symbol_type_in_collectionObject



84
85
86
87
88
89
90
91
# File 'lib/contentful_importer.rb', line 84

def find_symbol_type_in_collection
  Dir.glob("#{COLLECTIONS_DATA_DIR}/*json") do |file_path|
    collection_attributes = JSON.parse(File.read(file_path))
    collection_attributes['fields'].each do |field|
      find_symbol_attribute(collection_attributes, field)
    end
  end
end

#import_content_typesObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/contentful_importer.rb', line 42

def import_content_types
  Dir.glob("#{COLLECTIONS_DATA_DIR}/*json") do |file_path|
    collection_attributes = JSON.parse(File.read(file_path))
    content_type = create_new_content_type(collection_attributes)
    puts "Importing content_type: #{content_type.name}"
    create_content_type_fields(collection_attributes, content_type)
    create_content_type_webhooks(collection_attributes['webhook_definitions'], content_type.space.id)
    add_content_type_id_to_file(collection_attributes, content_type.id, content_type.space.id, file_path)
    active_status(content_type.activate)
  end
end

#import_entriesObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/contentful_importer.rb', line 60

def import_entries
  map_entries_ids
  Dir.glob("#{ENTRIES_DATA_DIR}/*") do |dir_path|
    collection_name = File.basename(dir_path)
    puts "Importing entries for #{collection_name}."
    collection_attributes = JSON.parse(File.read("#{COLLECTIONS_DATA_DIR}/#{collection_name}.json"))
    content_type_id = collection_attributes['content_type_id']
    space_id = collection_attributes['space_id']
    import_entry(content_type_id, dir_path, space_id)
  end
end

#map_entries_idsObject



54
55
56
57
58
# File 'lib/contentful_importer.rb', line 54

def map_entries_ids
  Dir.glob("#{ENTRIES_DATA_DIR}/**/*json") do |dir_path|
    ENTRIES_IDS << File.basename(dir_path, '.json')
  end
end

#publish_all_entriesObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/contentful_importer.rb', line 72

def publish_all_entries
  Dir.glob("#{COLLECTIONS_DATA_DIR}/*json") do |dir_path|
    collection_name = File.basename(dir_path, '.json')
    puts "Publish entries for #{collection_name}."
    collection_attributes = JSON.parse(File.read("#{COLLECTIONS_DATA_DIR}/#{collection_name}.json"))
    Contentful::Management::Space.find(get_space_id(collection_attributes)).entries.all.each do |entry|
      puts "Publish an entry with ID #{entry.id}."
      active_status(entry.publish)
    end
  end
end

#test_contetful_credentialsObject



17
18
19
20
21
22
23
24
# File 'lib/contentful_importer.rb', line 17

def test_contetful_credentials
  space = Contentful::Management::Space.all
  if space.is_a? Contentful::Management::Array
    puts 'Contentful Management API credentials: OK'
  end
rescue NoMethodError => e
  puts 'Contentful Management API credentials: INVALID (check README)'
end

#test_credentialsObject



12
13
14
15
# File 'lib/contentful_importer.rb', line 12

def test_credentials
  test_contetful_credentials
  test_storageroom_credentials
end

#test_storageroom_credentialsObject



26
27
28
29
30
31
32
33
34
# File 'lib/contentful_importer.rb', line 26

def test_storageroom_credentials
  request = StorageRoomExporter.new.send(:get_request, 'collections')
  if request.is_a? Hash
    puts 'StorageRoom API credentials: OK'
  end
rescue RuntimeError => e
  puts 'StorageRoom API credentials: INVALID (check README)'
  puts e
end