Class: Jekyll::Contentful::DataExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-contentful-data-import/data_exporter.rb

Overview

Data Exporter Class

Serializes Contentful data into YAML files

Constant Summary collapse

DATA_FOLDER =
'_data'.freeze
CONTENTFUL_FOLDER =
'contentful'.freeze
SPACES_FOLDER =
'spaces'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, entries, config = {}) ⇒ DataExporter

Returns a new instance of DataExporter.



15
16
17
18
19
# File 'lib/jekyll-contentful-data-import/data_exporter.rb', line 15

def initialize(name, entries, config = {})
  @name = name
  @entries = entries
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/jekyll-contentful-data-import/data_exporter.rb', line 13

def config
  @config
end

#entriesObject (readonly)

Returns the value of attribute entries.



13
14
15
# File 'lib/jekyll-contentful-data-import/data_exporter.rb', line 13

def entries
  @entries
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/jekyll-contentful-data-import/data_exporter.rb', line 13

def name
  @name
end

Instance Method Details

#base_directoryObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jekyll-contentful-data-import/data_exporter.rb', line 34

def base_directory
  directory = File.expand_path(Dir.pwd)
  if config.key?('base_path')
    directory = File.join(
      directory,
      config['base_path']
    )
  end

  directory
end

#destination_directoryObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jekyll-contentful-data-import/data_exporter.rb', line 46

def destination_directory
  destination_dir = File.join(
    base_directory, DATA_FOLDER,
    CONTENTFUL_FOLDER, SPACES_FOLDER
  )
  if config.key?('destination')
    destination_dir = File.join(
      base_directory, DATA_FOLDER, config['destination']
    )
  end

  destination_dir
end

#destination_fileObject



60
61
62
# File 'lib/jekyll-contentful-data-import/data_exporter.rb', line 60

def destination_file
  File.join(destination_directory, "#{name}.yaml")
end

#runObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jekyll-contentful-data-import/data_exporter.rb', line 21

def run
  setup_directory

  File.open(destination_file, 'w') do |file|
    file.write(
      ::Jekyll::Contentful::Serializer.new(
        entries,
        config
      ).to_yaml
    )
  end
end

#setup_directoryObject



64
65
66
# File 'lib/jekyll-contentful-data-import/data_exporter.rb', line 64

def setup_directory
  FileUtils.mkdir_p destination_directory
end