Class: Contentful::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Configuration



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/configuration.rb', line 17

def initialize(settings)
  @config = settings
  validate_required_parameters
  @data_dir = config['data_dir']
  @collections_dir = "#{data_dir}/collections"
  @entries_dir = "#{data_dir}/entries"
  @assets_dir = "#{data_dir}/assets"
  @helpers_dir = "#{data_dir}/helpers"
  @contentful_structure = load_contentful_structure_file
  @db = adapter_setup
  @converted_model_dir = @config['converted_model_dir']
  @content_types = config['content_model_json']
end

Instance Attribute Details

#assets_dirObject (readonly)

Returns the value of attribute assets_dir.



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

def assets_dir
  @assets_dir
end

#collections_dirObject (readonly)

Returns the value of attribute collections_dir.



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

def collections_dir
  @collections_dir
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#content_typesObject (readonly)

Returns the value of attribute content_types.



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

def content_types
  @content_types
end

#contentful_structureObject (readonly)

Returns the value of attribute contentful_structure.



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

def contentful_structure
  @contentful_structure
end

#converted_model_dirObject (readonly)

Returns the value of attribute converted_model_dir.



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

def converted_model_dir
  @converted_model_dir
end

#data_dirObject (readonly)

Returns the value of attribute data_dir.



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

def data_dir
  @data_dir
end

#dbObject (readonly)

Returns the value of attribute db.



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

def db
  @db
end

#entries_dirObject (readonly)

Returns the value of attribute entries_dir.



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

def entries_dir
  @entries_dir
end

#helpers_dirObject (readonly)

Returns the value of attribute helpers_dir.



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

def helpers_dir
  @helpers_dir
end

Instance Method Details

#adapter_setupObject



65
66
67
# File 'lib/configuration.rb', line 65

def adapter_setup
  Sequel.connect(:adapter => config['adapter'], :user => config['user'], :host => config['host'], :database => config['database'], :password => config['password'])
end

#create_empty_contentful_structure_fileObject



56
57
58
59
# File 'lib/configuration.rb', line 56

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

#define_adapterObject



40
41
42
43
44
# File 'lib/configuration.rb', line 40

def define_adapter
  %w(adapter host database).each do |param|
    fail ArgumentError, "Set database connection parameters [adapter, host, database, user, password]. Missing the '#{param}' parameter! Password and User are optional. View README!" unless config[param]
  end
end

#file_exists?Boolean



52
53
54
# File 'lib/configuration.rb', line 52

def file_exists?
  File.exists?(config['contentful_structure_dir'])
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.



48
49
50
# File 'lib/configuration.rb', line 48

def load_contentful_structure_file
  file_exists? ? load_existing_contentful_structure_file : create_empty_contentful_structure_file
end

#load_existing_contentful_structure_fileObject



61
62
63
# File 'lib/configuration.rb', line 61

def load_existing_contentful_structure_file
  JSON.parse(File.read(config['contentful_structure_dir']), symbolize_names: true).with_indifferent_access
end

#validate_required_parametersObject



31
32
33
34
35
36
37
38
# File 'lib/configuration.rb', line 31

def validate_required_parameters
  fail ArgumentError, 'Set PATH to data_dir. Folder where all data will be stored. View README' if config['data_dir'].nil?
  fail ArgumentError, 'Set PATH to contentful structure JSON file. View README' if config['contentful_structure_dir'].nil?
  fail ArgumentError, 'Set PATH to mapping structure JSON file. View README' if config['mapping_dir'].nil?
  fail ArgumentError, 'Set PATH to Content model JSON file, which is downloaded structure from Contentful. View README' if config['converted_model_dir'].nil?
  fail ArgumentError, 'Set PATH to converted contentful model and saved as JSON file. View README' if config['content_model_json'].nil?
  define_adapter
end