contentful_bootstrap 2.0.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -0
  3. data/CHANGELOG.md +6 -0
  4. data/Gemfile +1 -1
  5. data/examples/templates/catalogue.json +87 -62
  6. data/lib/contentful/bootstrap/commands/create_space.rb +1 -1
  7. data/lib/contentful/bootstrap/generator.rb +9 -9
  8. data/lib/contentful/bootstrap/templates/base.rb +42 -24
  9. data/lib/contentful/bootstrap/templates/blog.rb +2 -0
  10. data/lib/contentful/bootstrap/templates/json_template.rb +24 -11
  11. data/lib/contentful/bootstrap/templates/links/base.rb +5 -0
  12. data/lib/contentful/bootstrap/token.rb +3 -3
  13. data/lib/contentful/bootstrap/version.rb +5 -1
  14. data/spec/contentful/bootstrap/commands/create_space_spec.rb +56 -4
  15. data/spec/contentful/bootstrap/commands/generate_token_spec.rb +14 -0
  16. data/spec/contentful/bootstrap/templates/json_template_spec.rb +39 -1
  17. data/spec/contentful/bootstrap/templates/links/asset_spec.rb +14 -0
  18. data/spec/contentful/bootstrap/templates/links/base_spec.rb +15 -1
  19. data/spec/contentful/bootstrap/templates/links/entry_spec.rb +14 -0
  20. data/spec/fixtures/json_fixtures/high.json +6 -0
  21. data/spec/fixtures/json_fixtures/invalid.json +5 -0
  22. data/spec/fixtures/json_fixtures/issue_22.json +20 -15
  23. data/spec/fixtures/json_fixtures/links.json +18 -0
  24. data/spec/fixtures/json_fixtures/low.json +6 -0
  25. data/spec/fixtures/json_fixtures/no_ids.json +25 -0
  26. data/spec/fixtures/json_fixtures/ok_version.json +6 -0
  27. data/spec/fixtures/json_fixtures/simple.json +8 -3
  28. data/spec/fixtures/json_fixtures/wl1z0pal05vy.json +209 -172
  29. data/spec/fixtures/vcr_fixtures/create_space.yml +202 -0
  30. data/spec/fixtures/vcr_fixtures/create_space_with_blog_template.yml +4337 -0
  31. data/spec/fixtures/vcr_fixtures/create_space_with_catalogue_template.yml +9987 -0
  32. data/spec/fixtures/vcr_fixtures/create_space_with_gallery_template.yml +5609 -0
  33. data/spec/fixtures/vcr_fixtures/generate_json.yml +177 -177
  34. data/spec/fixtures/vcr_fixtures/generate_token.yml +394 -0
  35. data/spec/fixtures/vcr_fixtures/issue_22.yml +914 -307
  36. data/spec/fixtures/vcr_fixtures/no_ids.yml +1314 -0
  37. metadata +27 -3
@@ -59,11 +59,13 @@ module Contentful
59
59
  ],
60
60
  'post' => [
61
61
  {
62
+ 'id' => 'inferno',
62
63
  'title' => 'Inferno',
63
64
  'content' => 'Inferno is the last book in Dan Brown\'s collection...',
64
65
  'author' => Links::Entry.new('dan_brown')
65
66
  },
66
67
  {
68
+ 'id' => 'alturas',
67
69
  'title' => 'Alturas de Macchu Picchu',
68
70
  'content' => 'Alturas de Macchu Picchu is one of Pablo Neruda\'s most famous poetry books...',
69
71
  'author' => Links::Entry.new('pablo_neruda')
@@ -12,10 +12,12 @@ module Contentful
12
12
  @assets = nil
13
13
  @entries = nil
14
14
  json
15
+
16
+ check_version
15
17
  end
16
18
 
17
19
  def content_types
18
- json.fetch('content_types', [])
20
+ json.fetch('contentTypes', [])
19
21
  end
20
22
 
21
23
  def assets
@@ -28,6 +30,14 @@ module Contentful
28
30
 
29
31
  private
30
32
 
33
+ def check_version
34
+ json_version = json.fetch('version', 0)
35
+ gem_major_version = Contentful::Bootstrap.major_version
36
+ unless gem_major_version == json_version
37
+ fail "JSON Templates Version Mismatch. Current Version: #{gem_major_version}"
38
+ end
39
+ end
40
+
31
41
  def json
32
42
  @json ||= ::JSON.parse(::File.read(@file))
33
43
  end
@@ -49,29 +59,32 @@ module Contentful
49
59
  unprocessed_entries.each do |content_type_id, entry_list|
50
60
  entries_for_content_type = []
51
61
  entry_list.each do |entry|
62
+ processed_entry = {}
52
63
  array_fields = []
53
64
  link_fields = []
54
65
 
55
- entry.each do |field, value|
66
+ processed_entry['id'] = entry['sys']['id'] if entry.key?('sys') && entry['sys'].key?('id')
67
+
68
+ entry.fetch('fields', {}).each do |field, value|
56
69
  link_fields << field if value.is_a? ::Hash
57
70
  array_fields << field if value.is_a? ::Array
71
+
72
+ unless link_fields.include?(field) || array_fields.include?(field)
73
+ processed_entry[field] = value
74
+ end
58
75
  end
59
76
 
60
77
  link_fields.each do |lf|
61
- entry[lf] = create_link(entry[lf])
78
+ processed_entry[lf] = create_link(entry['fields'][lf])
62
79
  end
63
80
 
64
81
  array_fields.each do |af|
65
- entry[af].map! do |item|
66
- if item.is_a? ::Hash
67
- create_link(item)
68
- else
69
- item
70
- end
82
+ processed_entry[af] = entry['fields'][af].map do |item|
83
+ item.is_a?(::Hash) ? create_link(item) : item
71
84
  end
72
85
  end
73
86
 
74
- entries_for_content_type << entry
87
+ entries_for_content_type << processed_entry
75
88
  end
76
89
 
77
90
  processed_entries[content_type_id] = entries_for_content_type
@@ -81,7 +94,7 @@ module Contentful
81
94
  end
82
95
 
83
96
  def create_link(link_properties)
84
- link_type = link_properties['link_type'].capitalize
97
+ link_type = link_properties['linkType'].capitalize
85
98
  id = link_properties['id']
86
99
  case link_type
87
100
  when 'Entry'
@@ -27,6 +27,11 @@ module Contentful
27
27
  def management_class
28
28
  fail 'must implement'
29
29
  end
30
+
31
+ def ==(other)
32
+ return false unless other.is_a? self.class
33
+ other.id == id
34
+ end
30
35
  end
31
36
  end
32
37
  end
@@ -16,9 +16,9 @@ module Contentful
16
16
  @config_path = config_path
17
17
  end
18
18
 
19
- def ==(other_token)
20
- return false unless other_token.is_a?(Contentful::Bootstrap::Token)
21
- other_token.config_path == @config_path
19
+ def ==(other)
20
+ return false unless other.is_a?(Contentful::Bootstrap::Token)
21
+ other.config_path == @config_path
22
22
  end
23
23
 
24
24
  def present?
@@ -1,5 +1,9 @@
1
1
  module Contentful
2
2
  module Bootstrap
3
- VERSION = '2.0.2'
3
+ VERSION = '3.0.0'
4
+
5
+ def self.major_version
6
+ VERSION.split('.').first.to_i
7
+ end
4
8
  end
5
9
  end
@@ -18,7 +18,7 @@ describe Contentful::Bootstrap::Commands::CreateSpace do
18
18
  end
19
19
 
20
20
  it 'does not create template when template_name is nil' do
21
- create_space_command = subject.class.new(token, 'foo', nil, 'baz', false)
21
+ create_space_command = described_class.new(token, 'foo', nil, 'baz', false)
22
22
 
23
23
  expect(create_space_command.template_name).to eq nil
24
24
 
@@ -31,7 +31,7 @@ describe Contentful::Bootstrap::Commands::CreateSpace do
31
31
  end
32
32
 
33
33
  it 'does not create json template when json_template is nil' do
34
- create_space_command = subject.class.new(token, 'foo', 'bar', nil, false)
34
+ create_space_command = described_class.new(token, 'foo', 'bar', nil, false)
35
35
 
36
36
  expect(create_space_command.json_template).to eq nil
37
37
 
@@ -59,14 +59,66 @@ describe Contentful::Bootstrap::Commands::CreateSpace do
59
59
  it 'Importing asset array values does not work #22' do
60
60
  json_path = File.expand_path(File.join('spec', 'fixtures', 'json_fixtures', 'issue_22.json'))
61
61
 
62
- allow_any_instance_of(subject.class).to receive(:gets).and_return('y')
62
+ allow_any_instance_of(described_class).to receive(:gets).and_return('y')
63
63
  allow_any_instance_of(Contentful::Bootstrap::Commands::GenerateToken).to receive(:gets).and_return('n')
64
64
 
65
- command = subject.class.new(token, 'issue_22', nil, json_path)
65
+ command = described_class.new(token, 'issue_22', nil, json_path)
66
66
 
67
67
  vcr('issue_22') {
68
68
  command.run
69
69
  }
70
70
  end
71
71
  end
72
+
73
+ describe 'integration' do
74
+ before do
75
+ allow_any_instance_of(described_class).to receive(:gets).and_return('y')
76
+ allow_any_instance_of(Contentful::Bootstrap::Commands::GenerateToken).to receive(:gets).and_return('n')
77
+ end
78
+
79
+ it 'create space' do
80
+ command = described_class.new token, 'some_space'
81
+
82
+ vcr('create_space') {
83
+ command.run
84
+ }
85
+ end
86
+
87
+ it 'create space with blog template' do
88
+ command = described_class.new token, 'blog_space', 'blog'
89
+
90
+ vcr('create_space_with_blog_template') {
91
+ command.run
92
+ }
93
+ end
94
+
95
+ it 'create space with gallery template' do
96
+ command = described_class.new token, 'gallery_space', 'gallery'
97
+
98
+ vcr('create_space_with_gallery_template') {
99
+ command.run
100
+ }
101
+ end
102
+
103
+ it 'create space with catalogue template' do
104
+ command = described_class.new token, 'catalogue_space', 'catalogue'
105
+
106
+ vcr('create_space_with_catalogue_template') {
107
+ command.run
108
+ }
109
+ end
110
+
111
+ it 'create space with json template' do
112
+ skip 'covered by create_space_spec:issues/#22'
113
+ end
114
+
115
+ it 'create space with json template with no ids' do
116
+ json_path = File.expand_path(File.join('spec', 'fixtures', 'json_fixtures', 'no_ids.json'))
117
+ command = described_class.new token, 'no_ids_space', nil, json_path
118
+
119
+ vcr('no_ids') {
120
+ command.run
121
+ }
122
+ end
123
+ end
72
124
  end
@@ -79,4 +79,18 @@ describe Contentful::Bootstrap::Commands::GenerateToken do
79
79
  expect(subject.token_name).to eq 'bar'
80
80
  end
81
81
  end
82
+
83
+ describe 'integration' do
84
+ before do
85
+ allow_any_instance_of(subject.class).to receive(:gets).and_return('n')
86
+ end
87
+
88
+ it 'generates a token for a given space' do
89
+ command = subject.class.new token, 'zred3m25k5em', 'foo'
90
+
91
+ vcr('generate_token') {
92
+ command.run
93
+ }
94
+ end
95
+ end
82
96
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Contentful::Bootstrap::Templates::JsonTemplate do
4
4
  let(:space) { Contentful::Management::Space.new }
5
5
  let(:path) { File.expand_path(File.join('spec', 'fixtures', 'json_fixtures', 'simple.json')) }
6
- subject { Contentful::Bootstrap::Templates::JsonTemplate.new space, path }
6
+ subject { described_class.new space, path }
7
7
 
8
8
  describe 'instance methods' do
9
9
  it '#content_types' do
@@ -49,4 +49,42 @@ describe Contentful::Bootstrap::Templates::JsonTemplate do
49
49
  )
50
50
  end
51
51
  end
52
+
53
+ describe 'template version check' do
54
+ let(:invalid_version_path) { File.expand_path(File.join('spec', 'fixtures', 'json_fixtures', 'invalid.json')) }
55
+ let(:low_version_path) { File.expand_path(File.join('spec', 'fixtures', 'json_fixtures', 'low.json')) }
56
+ let(:high_version_path) { File.expand_path(File.join('spec', 'fixtures', 'json_fixtures', 'high.json')) }
57
+ let(:ok_version_path) { File.expand_path(File.join('spec', 'fixtures', 'json_fixtures', 'ok_version.json')) }
58
+
59
+ it 'rejects templates without version' do
60
+ expect { described_class.new space, invalid_version_path }.to raise_error "JSON Templates Version Mismatch. Current Version: 3"
61
+ end
62
+
63
+ it 'rejects templates with previous versions' do
64
+ expect { described_class.new space, low_version_path }.to raise_error "JSON Templates Version Mismatch. Current Version: 3"
65
+ end
66
+
67
+ it 'rejects templates with newer versions' do
68
+ expect { described_class.new space, high_version_path }.to raise_error "JSON Templates Version Mismatch. Current Version: 3"
69
+ end
70
+
71
+ it 'accepts templates with correct version' do
72
+ expect { described_class.new space, ok_version_path }.not_to raise_error
73
+ end
74
+ end
75
+
76
+ describe 'issues' do
77
+ let(:link_entry_path) { File.expand_path(File.join('spec', 'fixtures', 'json_fixtures', 'links.json')) }
78
+
79
+ it 'links are not properly getting processed - #33' do
80
+ subject = described_class.new space, link_entry_path
81
+
82
+ expect(subject.entries["cat"].first).to eq(
83
+ {
84
+ "id" => "foo",
85
+ "link" => Contentful::Bootstrap::Templates::Links::Entry.new('foobar')
86
+ }
87
+ )
88
+ end
89
+ end
52
90
  end
@@ -19,5 +19,19 @@ describe Contentful::Bootstrap::Templates::Links::Asset do
19
19
  it '#management_class' do
20
20
  expect(subject.management_class).to eq Contentful::Management::Asset
21
21
  end
22
+
23
+ describe '#==' do
24
+ it 'false when different type' do
25
+ expect(subject == 2).to be_falsey
26
+ end
27
+
28
+ it 'false when different id' do
29
+ expect(subject == described_class.new('bar')).to be_falsey
30
+ end
31
+
32
+ it 'true when same id' do
33
+ expect(subject == described_class.new('foo')).to be_truthy
34
+ end
35
+ end
22
36
  end
23
37
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Contentful::Bootstrap::Templates::Links::Base do
4
- subject { Contentful::Bootstrap::Templates::Links::Base.new 'foo' }
4
+ subject { described_class.new 'foo' }
5
5
 
6
6
  describe 'instance methods' do
7
7
  it '#link_type' do
@@ -20,6 +20,20 @@ describe Contentful::Bootstrap::Templates::Links::Base do
20
20
  it '#management_class' do
21
21
  expect { subject.management_class }.to raise_error "must implement"
22
22
  end
23
+
24
+ describe '#==' do
25
+ it 'false when different type' do
26
+ expect(subject == 2).to be_falsey
27
+ end
28
+
29
+ it 'false when different id' do
30
+ expect(subject == described_class.new('bar')).to be_falsey
31
+ end
32
+
33
+ it 'true when same id' do
34
+ expect(subject == described_class.new('foo')).to be_truthy
35
+ end
36
+ end
23
37
  end
24
38
  end
25
39
 
@@ -19,5 +19,19 @@ describe Contentful::Bootstrap::Templates::Links::Entry do
19
19
  it '#management_class' do
20
20
  expect(subject.management_class).to eq Contentful::Management::Entry
21
21
  end
22
+
23
+ describe '#==' do
24
+ it 'false when different type' do
25
+ expect(subject == 2).to be_falsey
26
+ end
27
+
28
+ it 'false when different id' do
29
+ expect(subject == described_class.new('bar')).to be_falsey
30
+ end
31
+
32
+ it 'true when same id' do
33
+ expect(subject == described_class.new('foo')).to be_truthy
34
+ end
35
+ end
22
36
  end
23
37
  end
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 4,
3
+ "content_types": [],
4
+ "assets": [],
5
+ "entries": {}
6
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "content_types": [],
3
+ "assets": [],
4
+ "entries": {}
5
+ }
@@ -1,9 +1,10 @@
1
1
  {
2
- "content_types": [
2
+ "version": 3,
3
+ "contentTypes": [
3
4
  {
4
5
  "id": "1VX60jHERm6yuem8I2agWG",
5
6
  "name": "yolo",
6
- "display_field": "title",
7
+ "displayField": "title",
7
8
  "fields": [
8
9
  {
9
10
  "id": "title",
@@ -16,7 +17,7 @@
16
17
  "type": "Array",
17
18
  "items": {
18
19
  "type": "Link",
19
- "link_type": "Asset"
20
+ "linkType": "Asset"
20
21
  }
21
22
  }
22
23
  ]
@@ -59,18 +60,22 @@
59
60
  "entries": {
60
61
  "1VX60jHERm6yuem8I2agWG": [
61
62
  {
62
- "id": "5nQB3z8RgW8CUG6uGyCsEw",
63
- "title": "yolo",
64
- "assets": [
65
- {
66
- "link_type": "Asset",
67
- "id": "4eX9aQsDdeqweg6Si00KaW"
68
- },
69
- {
70
- "link_type": "Asset",
71
- "id": "4D5sv49qaAoMIkWgAW8g0I"
72
- }
73
- ]
63
+ "sys": {
64
+ "id": "5nQB3z8RgW8CUG6uGyCsEw"
65
+ },
66
+ "fields": {
67
+ "title": "yolo",
68
+ "assets": [
69
+ {
70
+ "linkType": "Asset",
71
+ "id": "4eX9aQsDdeqweg6Si00KaW"
72
+ },
73
+ {
74
+ "linkType": "Asset",
75
+ "id": "4D5sv49qaAoMIkWgAW8g0I"
76
+ }
77
+ ]
78
+ }
74
79
  }
75
80
  ]
76
81
  }
@@ -0,0 +1,18 @@
1
+ {
2
+ "version": 3,
3
+ "content_types": [],
4
+ "assets": [],
5
+ "entries": {
6
+ "cat": [
7
+ {
8
+ "sys": { "id": "foo" },
9
+ "fields": {
10
+ "link": {
11
+ "id": "foobar",
12
+ "linkType": "Entry"
13
+ }
14
+ }
15
+ }
16
+ ]
17
+ }
18
+ }