This repository has been archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Individual Files per Entries Option (#45)
- Loading branch information
Showing
8 changed files
with
257 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
lib/jekyll-contentful-data-import/multi_file_data_exporter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'jekyll-contentful-data-import/base_data_exporter' | ||
require 'yaml' | ||
|
||
module Jekyll | ||
module Contentful | ||
# Single File Data Exporter Class | ||
# | ||
# Serializes Contentful data into a multiple YAML files | ||
class MultiFileDataExporter < BaseDataExporter | ||
def run | ||
data = ::Jekyll::Contentful::Serializer.new( | ||
entries, | ||
config | ||
).serialize | ||
|
||
data.each do |content_type, entries| | ||
content_type_directory = File.join(destination_directory, name, content_type.to_s) | ||
setup_directory(content_type_directory) | ||
|
||
entries.each do |entry| | ||
yaml_entry = YAML.dump(entry) | ||
|
||
File.open(destination_file(content_type_directory, entry), 'w') do |file| | ||
file.write(yaml_entry) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def destination_file(content_type_directory, entry) | ||
File.join(content_type_directory, "#{entry['sys']['id']}.yaml") | ||
end | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
lib/jekyll-contentful-data-import/single_file_data_exporter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'jekyll-contentful-data-import/base_data_exporter' | ||
|
||
module Jekyll | ||
module Contentful | ||
# Single File Data Exporter Class | ||
# | ||
# Serializes Contentful data into a single YAML file | ||
class SingleFileDataExporter < BaseDataExporter | ||
def run | ||
setup_directory(destination_directory) | ||
|
||
File.open(destination_file, 'w') do |file| | ||
file.write( | ||
::Jekyll::Contentful::Serializer.new( | ||
entries, | ||
config | ||
).to_yaml | ||
) | ||
end | ||
end | ||
|
||
def destination_file | ||
File.join(destination_directory, "#{name}.yaml") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require 'spec_helper' | ||
require 'stringio' | ||
|
||
describe Jekyll::Contentful::MultiFileDataExporter do | ||
subject { described_class.new('foo', []) } | ||
|
||
describe 'instance methods' do | ||
describe '#base_directory' do | ||
it 'default directory' do | ||
expect(subject.base_directory).to eq(Dir.pwd) | ||
end | ||
|
||
it 'overridden directory' do | ||
subject = described_class.new('foo', [], {'base_path' => 'foo_dir'}) | ||
|
||
expect(subject.base_directory).to eq(File.join(Dir.pwd, 'foo_dir')) | ||
end | ||
end | ||
|
||
describe '#destination_directory' do | ||
it 'default directory' do | ||
expected = File.join(Dir.pwd, '_data', 'contentful', 'spaces') | ||
expect(subject.destination_directory).to eq(expected) | ||
end | ||
|
||
it 'overridden directory' do | ||
subject = described_class.new('foo', [], {'base_path' => 'foo_dir'}) | ||
|
||
expected = File.join(Dir.pwd, 'foo_dir', '_data', 'contentful', 'spaces') | ||
expect(subject.destination_directory).to eq(expected) | ||
end | ||
end | ||
|
||
it '#destination_file' do | ||
entry_double = { 'sys' => { 'id' => 'bar' } } | ||
expected = File.join('foo', 'bar.yaml') | ||
expect(subject.destination_file('foo', entry_double)).to eq(expected) | ||
end | ||
|
||
describe '#setup_directory' do | ||
it 'default directory' do | ||
expected = File.join(Dir.pwd, '_data', 'contentful', 'spaces') | ||
expect(FileUtils).to receive(:mkdir_p).with(expected) | ||
|
||
subject.setup_directory(expected) | ||
end | ||
|
||
it 'overridden directory' do | ||
subject = described_class.new('foo', [], {'base_path' => 'foo_dir'}) | ||
|
||
expected = File.join(Dir.pwd, 'foo_dir', '_data', 'contentful', 'spaces') | ||
expect(FileUtils).to receive(:mkdir_p).with(expected) | ||
|
||
subject.setup_directory(expected) | ||
end | ||
end | ||
|
||
describe '#run' do | ||
before do | ||
allow(subject).to receive(:setup_directory) | ||
end | ||
|
||
it 'does nothing with no entries' do | ||
subject.run | ||
end | ||
|
||
it 'serializes entries' do | ||
expect_any_instance_of(::Jekyll::Contentful::Serializer).to receive(:serialize) { {} } | ||
|
||
subject.run | ||
end | ||
|
||
it 'creates a file per entry' do | ||
subject = described_class.new('foo', [EntryDouble.new('bar', ContentTypeDouble.new('bar_ct'))]) | ||
|
||
expected_directory_path = File.join(Dir.pwd, '_data', 'contentful', 'spaces', 'foo', 'bar_ct') | ||
expect(FileUtils).to receive(:mkdir_p).with(expected_directory_path) | ||
expect(File).to receive(:open).with(File.join(expected_directory_path, 'bar.yaml'), 'w') | ||
|
||
subject.run | ||
end | ||
end | ||
end | ||
end | ||
|
Oops, something went wrong.