Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Update to CDA 2.0 and add extra metadata (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlitvakb authored Apr 10, 2017
1 parent e0ccce8 commit 0001685
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 17 deletions.
2 changes: 1 addition & 1 deletion jekyll-contentful.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |s|
s.add_dependency("jekyll", ">= 2.5.0", "< 4")

# Additional dependencies
s.add_dependency("contentful", '~> 1.0')
s.add_dependency("contentful", '>= 2.0.1', '~> 2.0')

s.add_development_dependency 'rubygems-tasks', '~> 0.2'
s.add_development_dependency "guard"
Expand Down
24 changes: 22 additions & 2 deletions lib/jekyll-contentful-data-import/mappers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ def initialize(entry, config)
@config = config
end

def map_entry_metadata
content_type = entry.sys.fetch(:content_type, nil)
return {
'id' => entry.sys.fetch(:id, nil),
'created_at' => entry.sys.fetch(:created_at, nil),
'updated_at' => entry.sys.fetch(:updated_at, nil),
'content_type_id' => content_type.nil? ? nil : content_type.id
}
end

def map
result = {'sys' => {'id' => entry.id}}
result = { 'sys' => map_entry_metadata }

fields = has_multiple_locales? ? entry.fields_with_locales : entry.fields

Expand Down Expand Up @@ -63,7 +73,7 @@ def map_value(value, locale = nil)
map_location(value)
when ::Contentful::Link
map_link(value)
when ::Contentful::DynamicEntry
when ::Contentful::Entry
map_entry(value)
when ::Array
map_array(value, locale)
Expand All @@ -80,12 +90,21 @@ def map_value(value, locale = nil)
end
end

def map_asset_metadata(asset)
return {
'id' => asset.id,
'created_at' => asset.sys.fetch(:created_at, nil),
'updated_at' => asset.sys.fetch(:updated_at, nil)
}
end

def map_asset(asset, locale = nil)
if locale
file = asset.fields(locale)[:file]
file_url = file.nil? ? '' : file.url

return {
'sys' => map_asset_metadata(asset),
'title' => asset.fields(locale)[:title],
'description' => asset.fields(locale)[:description],
'url' => file_url
Expand All @@ -96,6 +115,7 @@ def map_asset(asset, locale = nil)
file_url = file.nil? ? '' : file.url

{
'sys' => map_asset_metadata(asset),
'title' => asset.title,
'description' => asset.description,
'url' => file_url
Expand Down
63 changes: 55 additions & 8 deletions spec/jekyll-contentful/mappers/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ def initialize(url)

class AssetDouble < Contentful::Asset
attr_reader :title, :description, :file
def initialize(title, description, url, fields = nil)
def initialize(title, description, url, sys = {}, fields = nil)
@title = title
@description = description
@file = FileDouble.new(url)
@sys = sys
@fields = {
'en-US' => {
title: title,
Expand All @@ -49,6 +50,10 @@ def initialize(title, description, url, fields = nil)
@fields ||= fields
end

def id
@sys[:id]
end

def fields(locale = nil)
return { title: title, description: description, file: file } if locale.nil?
@fields[locale]
Expand All @@ -75,13 +80,20 @@ def initialize(id)
end

it 'maps a simple entry' do
expected = { 'sys' => { 'id' => 'foo' } }
expected = {
'sys' => {
'id' => 'foo',
'created_at' => nil,
'updated_at' => nil,
'content_type_id' => 'content_type'
}
}
expect(subject.map).to eq expected
end

it 'maps a complete entry' do
entry = EntryDouble.new('foo', ContentTypeDouble.new, {
'asset' => AssetDouble.new('some_title', 'foo', 'some_url'),
'asset' => AssetDouble.new('some_title', 'foo', 'some_url', {id: 'asset'}),
'location' => LocationDouble.new(12.32, 43.34),
'link' => LinkDouble.new('bar'),
'entry' => EntryDouble.new('baz'),
Expand All @@ -95,8 +107,18 @@ def initialize(id)
subject.instance_variable_set(:@entry, entry)

expected = {
'sys' => { 'id' => 'foo' },
'sys' => {
'id' => 'foo',
'created_at' => nil,
'updated_at' => nil,
'content_type_id' => 'content_type'
},
'asset' => {
'sys' => {
'id' => 'asset',
'created_at' => nil,
'updated_at' => nil,
},
'title' => 'some_title',
'description' => 'foo',
'url' => 'some_url'
Expand All @@ -109,7 +131,12 @@ def initialize(id)
'sys' => { 'id' => 'bar' }
},
'entry' => {
'sys' => { 'id' => 'baz' }
'sys' => {
'id' => 'baz',
'created_at' => nil,
'updated_at' => nil,
'content_type_id' => 'content_type'
},
},
'array' => [
{ 'sys' => { 'id' => 'foobar' } },
Expand All @@ -135,7 +162,12 @@ def initialize(id)
mapper = described_class.new(entry, config)

expected = {
'sys' => { 'id' => 'foo' },
'sys' => {
'id' => 'foo',
'created_at' => nil,
'updated_at' => nil,
'content_type_id' => 'content_type'
},
'foo' => {
'en-US' => 'bar',
'de-DE' => 'baz'
Expand All @@ -154,7 +186,7 @@ def initialize(id)
'asset' => AssetDouble.new('some_title', 'foo', 'some_url')
},
'de-DE' => {
'asset' => AssetDouble.new('some_title', 'foo', 'some_url', {
'asset' => AssetDouble.new('some_title', 'foo', 'some_url', {id: 'foo'}, {
'de-DE' => {
title: 'other_title',
description: 'other description',
Expand All @@ -167,14 +199,29 @@ def initialize(id)
mapper = described_class.new(entry, config)

expected = {
'sys' => { 'id' => 'foo' },
'sys' => {
'id' => 'foo',
'created_at' => nil,
'updated_at' => nil,
'content_type_id' => 'content_type'
},
'asset' => {
"en-US" => {
"sys" => {
"id" => nil,
"created_at" => nil,
"updated_at" => nil
},
"title" => "some_title",
"description" => "foo",
"url" => 'some_url'
},
"de-DE" => {
"sys" => {
"id" => "foo",
"created_at" => nil,
"updated_at" => nil
},
"title" => "other_title",
"description" => "other description",
"url" => 'other_url'
Expand Down
69 changes: 64 additions & 5 deletions spec/jekyll-contentful/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,37 @@
end

it 'serializes a single entry without fields' do
expected = {'content_type' => [{'sys' => {'id' => 'foo'}}]}
expected = {
'content_type' => [
{
'sys' => {
'id' => 'foo',
'created_at' => nil,
'updated_at' => nil,
'content_type_id' => 'content_type'
}
}
]
}
expect(subject.serialize).to eq(expected)
end

it 'serializes a single entry with fields' do
subject.instance_variable_set(:@entries, [EntryDouble.new('foo', ContentTypeDouble.new, {'foobar' => 'bar'})])

expected = {'content_type' => [{'sys' => {'id' => 'foo'}, 'foobar' => 'bar'}]}
expected = {
'content_type' => [
{
'sys' => {
'id' => 'foo',
'created_at' => nil,
'updated_at' => nil,
'content_type_id' => 'content_type'
},
'foobar' => 'bar'
}
]
}
expect(subject.serialize).to eq(expected)
end

Expand All @@ -43,7 +66,27 @@
})
])

expected = {'content_type' => [{'sys' => {'id' => 'foo'}, 'foobar' => {'sys' => {'id' => 'foobar'}, 'baz' => 1}}]}
expected = {
'content_type' => [
{
'sys' => {
'id' => 'foo',
'created_at' => nil,
'updated_at' => nil,
'content_type_id' => 'content_type'
},
'foobar' => {
'sys' => {
'id' => 'foobar',
'created_at' => nil,
'updated_at' => nil,
'content_type_id' => 'content_type'
},
'baz' => 1
}
}
]
}
expect(subject.serialize).to eq(expected)
end

Expand All @@ -55,8 +98,24 @@

expected = {
'content_type' => [
{'sys' => {'id' => 'foo'}, 'foobar' => 'bar'},
{'sys' => {'id' => 'bar'}, 'foobar' => 'baz'}
{
'sys' => {
'id' => 'foo',
'created_at' => nil,
'updated_at' => nil,
'content_type_id' => 'content_type'
},
'foobar' => 'bar'
},
{
'sys' => {
'id' => 'bar',
'created_at' => nil,
'updated_at' => nil,
'content_type_id' => 'content_type'
},
'foobar' => 'baz'
}
]
}
expect(subject.serialize).to match(expected)
Expand Down
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ def initialize(id = 'content_type')
end
end

class EntryDouble < Contentful::DynamicEntry
class EntryDouble < Contentful::Entry
attr_reader :id, :content_type

def initialize(id = '', content_type = ContentTypeDouble.new, fields = {}, locales = false)
@id = id
@content_type = content_type

@sys = { id: id, content_type: content_type, locale: 'en-US' }

if locales
@fields = fields
else
Expand Down

0 comments on commit 0001685

Please sign in to comment.