contentful_bootstrap 3.10.0 → 3.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +9 -5
  3. data/.rubocop_todo.yml +34 -76
  4. data/CHANGELOG.md +7 -0
  5. data/Guardfile +20 -4
  6. data/README.md +6 -6
  7. data/bin/contentful_bootstrap +46 -42
  8. data/contentful_bootstrap.gemspec +6 -3
  9. data/lib/contentful/bootstrap/command_runner.rb +3 -1
  10. data/lib/contentful/bootstrap/commands/base.rb +15 -15
  11. data/lib/contentful/bootstrap/commands/create_space.rb +5 -6
  12. data/lib/contentful/bootstrap/commands/generate_json.rb +13 -2
  13. data/lib/contentful/bootstrap/commands/generate_token.rb +6 -6
  14. data/lib/contentful/bootstrap/commands/update_space.rb +2 -2
  15. data/lib/contentful/bootstrap/generator.rb +5 -4
  16. data/lib/contentful/bootstrap/support.rb +1 -1
  17. data/lib/contentful/bootstrap/templates/base.rb +22 -25
  18. data/lib/contentful/bootstrap/templates/json_template.rb +49 -56
  19. data/lib/contentful/bootstrap/templates/links/base.rb +1 -1
  20. data/lib/contentful/bootstrap/token.rb +1 -1
  21. data/lib/contentful/bootstrap/version.rb +1 -1
  22. data/spec/contentful/bootstrap/command_runner_spec.rb +2 -2
  23. data/spec/contentful/bootstrap/commands/base_spec.rb +3 -15
  24. data/spec/contentful/bootstrap/commands/create_space_spec.rb +2 -2
  25. data/spec/contentful/bootstrap/commands/generate_json_spec.rb +1 -1
  26. data/spec/contentful/bootstrap/commands/generate_token_spec.rb +1 -1
  27. data/spec/contentful/bootstrap/commands/update_space_spec.rb +36 -8
  28. data/spec/contentful/bootstrap/generator_spec.rb +17 -2
  29. data/spec/contentful/bootstrap/templates/base_spec.rb +10 -8
  30. data/spec/contentful/bootstrap/templates/blog_spec.rb +9 -1
  31. data/spec/contentful/bootstrap/templates/catalogue_spec.rb +9 -1
  32. data/spec/contentful/bootstrap/templates/gallery_spec.rb +9 -1
  33. data/spec/contentful/bootstrap/templates/json_template_spec.rb +17 -9
  34. data/spec/fixtures/json_fixtures/environment_template.json +47 -0
  35. data/spec/fixtures/vcr_fixtures/asset_no_transform.yml +119 -4
  36. data/spec/fixtures/vcr_fixtures/check_created_space.yml +1 -1
  37. data/spec/fixtures/vcr_fixtures/check_original_staging_environment_status.yml +201 -0
  38. data/spec/fixtures/vcr_fixtures/check_staging_environment_status.yml +81 -0
  39. data/spec/fixtures/vcr_fixtures/check_update_space_localized.yml +2 -2
  40. data/spec/fixtures/vcr_fixtures/check_update_space_with_draft_content.yml +4 -4
  41. data/spec/fixtures/vcr_fixtures/create_space_with_blog_template.yml +153 -38
  42. data/spec/fixtures/vcr_fixtures/create_space_with_catalogue_template.yml +196 -81
  43. data/spec/fixtures/vcr_fixtures/create_space_with_gallery_template.yml +165 -50
  44. data/spec/fixtures/vcr_fixtures/generate_json.yml +5 -5
  45. data/spec/fixtures/vcr_fixtures/generate_json_content_types_only.yml +1 -1
  46. data/spec/fixtures/vcr_fixtures/generate_json_environments.yml +447 -0
  47. data/spec/fixtures/vcr_fixtures/generate_json_multi_ct.yml +5 -5
  48. data/spec/fixtures/vcr_fixtures/generate_json_preview.yml +5 -5
  49. data/spec/fixtures/vcr_fixtures/generate_json_single_ct.yml +5 -5
  50. data/spec/fixtures/vcr_fixtures/issue_22.yml +142 -27
  51. data/spec/fixtures/vcr_fixtures/no_ids.yml +126 -11
  52. data/spec/fixtures/vcr_fixtures/update_existing_asset.yml +130 -15
  53. data/spec/fixtures/vcr_fixtures/update_space_localized.yml +134 -19
  54. data/spec/fixtures/vcr_fixtures/update_with_environment.yml +2531 -0
  55. metadata +55 -17
@@ -27,9 +27,12 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "simplecov"
28
28
  spec.add_development_dependency "guard"
29
29
  spec.add_development_dependency "guard-rspec"
30
- spec.add_development_dependency 'listen', '~> 3.0.0'
30
+ spec.add_development_dependency 'listen', '~> 3.0'
31
+ spec.add_development_dependency "guard-rubocop"
32
+ spec.add_development_dependency "rubocop", "~> 0.49"
33
+
31
34
  spec.add_runtime_dependency "launchy"
32
- spec.add_runtime_dependency "contentful-management", '~> 1.10'
33
- spec.add_runtime_dependency "contentful", ">= 2.1.0", "< 3"
35
+ spec.add_runtime_dependency "contentful-management", '~> 2.0', '>= 2.0.2'
36
+ spec.add_runtime_dependency "contentful", "~> 2.6.0"
34
37
  spec.add_runtime_dependency "inifile"
35
38
  end
@@ -32,16 +32,18 @@ module Contentful
32
32
  def generate_json(space_id, options = {})
33
33
  filename = options.fetch(:filename, nil)
34
34
  access_token = options.fetch(:access_token, nil)
35
+ environment = options.fetch(:environment, 'master')
35
36
  content_types_only = options.fetch(:content_types_only, false)
36
37
  quiet = options.fetch(:quiet, false)
37
38
  use_preview = options.fetch(:use_preview, false)
38
39
  content_type_ids = options.fetch(:content_type_ids, [])
39
40
 
40
- fail 'Access Token required' if access_token.nil?
41
+ raise 'Access Token required' if access_token.nil?
41
42
 
42
43
  Contentful::Bootstrap::Commands::GenerateJson.new(
43
44
  space_id,
44
45
  access_token,
46
+ environment,
45
47
  filename,
46
48
  content_types_only,
47
49
  quiet,
@@ -7,7 +7,7 @@ module Contentful
7
7
  module Bootstrap
8
8
  module Commands
9
9
  class Base
10
- attr_reader :space, :token, :options, :quiet, :no_input, :client
10
+ attr_reader :space, :token, :options, :quiet, :no_input
11
11
 
12
12
  def initialize(token, space, options = {})
13
13
  trigger_oauth = options.fetch(:trigger_oauth, true)
@@ -18,32 +18,32 @@ module Contentful
18
18
  @no_input = options.fetch(:no_input, false)
19
19
 
20
20
  configuration if trigger_oauth
21
- management_client_init if trigger_oauth
21
+ client if trigger_oauth
22
22
  @space = space
23
23
  end
24
24
 
25
25
  def run
26
- fail 'must implement'
26
+ raise 'must implement'
27
27
  end
28
28
 
29
- protected
30
-
31
- def output(text = nil)
32
- Support.output(text, @quiet)
33
- end
34
-
35
- private
36
-
37
- def management_client_init
29
+ def client
38
30
  @client ||= ::Contentful::Management::Client.new(
39
31
  @token.read,
40
- default_locale: options.fetch(:locale, "en-US"),
32
+ default_locale: options.fetch(:locale, 'en-US'),
41
33
  raise_errors: true,
42
34
  application_name: 'bootstrap',
43
35
  application_version: ::Contentful::Bootstrap::VERSION
44
36
  )
45
37
  end
46
38
 
39
+ protected
40
+
41
+ def output(text = nil)
42
+ Support.output(text, @quiet)
43
+ end
44
+
45
+ private
46
+
47
47
  def configuration
48
48
  if @token.present?
49
49
  output 'OAuth token found, moving on!'
@@ -51,13 +51,13 @@ module Contentful
51
51
  end
52
52
 
53
53
  Support.input('OAuth Token not found, do you want to create a new configuration file? (Y/n): ', no_input) do |answer|
54
- if answer.downcase == 'n'
54
+ if answer.casecmp('n').zero?
55
55
  output 'Exiting!'
56
56
  exit
57
57
  end
58
58
  end
59
59
 
60
- fail 'OAuth token required to proceed' if no_input
60
+ raise 'OAuth token required to proceed' if no_input
61
61
 
62
62
  output "Configuration will be saved on #{@token.filename}"
63
63
  output 'A new tab on your browser will open for requesting OAuth permissions'
@@ -13,8 +13,9 @@ module Contentful
13
13
  @template_name = options.fetch(:template, nil)
14
14
  @json_template = options.fetch(:json_template, nil)
15
15
  @mark_processed = options.fetch(:mark_processed, false)
16
- @locale = options.fetch(:locale, "en-US")
16
+ @locale = options.fetch(:locale, 'en-US')
17
17
  @no_publish = options.fetch(:no_publish, false)
18
+ @environment = 'master' # Can only add content to a new space through the master environment by default
18
19
 
19
20
  super(token, space, options)
20
21
  end
@@ -51,10 +52,9 @@ module Contentful
51
52
  defaultLocale: @locale
52
53
  }
53
54
  options[:organization_id] = @token.read_organization_id unless @token.read_organization_id.nil?
54
- management_client_init
55
55
  new_space = client.spaces.create(options)
56
56
  rescue Contentful::Management::NotFound
57
- fail "Organization ID is required, provide it in Configuration File" if no_input
57
+ raise 'Organization ID is required, provide it in Configuration File' if no_input
58
58
 
59
59
  output 'Your account has multiple organizations:'
60
60
  output organizations.join("\n")
@@ -76,7 +76,6 @@ module Contentful
76
76
  private
77
77
 
78
78
  def organizations
79
- management_client_init
80
79
  organizations = client.organizations.all
81
80
  organization_ids = organizations.map do |organization|
82
81
  sprintf('%-30s %s', organization.name, organization.id)
@@ -96,7 +95,7 @@ module Contentful
96
95
  if templates.key? @template_name.to_sym
97
96
  output "Creating Template '#{@template_name}'"
98
97
 
99
- templates[@template_name.to_sym].new(space, quiet).run
98
+ templates[@template_name.to_sym].new(space, @environment, quiet).run
100
99
  output "Template '#{@template_name}' created!"
101
100
  else
102
101
  output "Template '#{@template_name}' not found. Valid templates are '#{templates.keys.map(&:to_s).join('\', \'')}'"
@@ -107,7 +106,7 @@ module Contentful
107
106
  def create_json_template(space)
108
107
  if ::File.exist?(@json_template)
109
108
  output "Creating JSON Template '#{@json_template}'"
110
- Templates::JsonTemplate.new(space, @json_template, @mark_processed, true, quiet, false, @no_publish).run
109
+ Templates::JsonTemplate.new(space, @json_template, @environment, @mark_processed, true, quiet, false, @no_publish).run
111
110
  output "JSON Template '#{@json_template}' created!"
112
111
  else
113
112
  output "JSON Template '#{@json_template}' does not exist. Please check that you specified the correct file name."
@@ -5,10 +5,20 @@ module Contentful
5
5
  module Bootstrap
6
6
  module Commands
7
7
  class GenerateJson
8
- attr_reader :space_id, :filename, :access_token, :content_types_only, :use_preview, :content_type_ids
9
- def initialize(space_id, access_token, filename = nil, content_types_only = false, quiet = false, use_preview = false, content_type_ids = [])
8
+ attr_reader :space_id, :filename, :access_token, :content_types_only, :use_preview, :content_type_ids, :environment
9
+ def initialize(
10
+ space_id,
11
+ access_token,
12
+ environment = 'master',
13
+ filename = nil,
14
+ content_types_only = false,
15
+ quiet = false,
16
+ use_preview = false,
17
+ content_type_ids = []
18
+ )
10
19
  @space_id = space_id
11
20
  @access_token = access_token
21
+ @environment = environment
12
22
  @filename = filename
13
23
  @content_types_only = content_types_only
14
24
  @quiet = quiet
@@ -29,6 +39,7 @@ module Contentful
29
39
  json = Contentful::Bootstrap::Generator.new(
30
40
  space_id,
31
41
  access_token,
42
+ environment,
32
43
  content_types_only,
33
44
  use_preview,
34
45
  content_type_ids
@@ -24,7 +24,7 @@ module Contentful
24
24
 
25
25
  output "Token '#{token_name}' created! - '#{access_token}'"
26
26
  Support.input('Do you want to write the Delivery Token to your configuration file? (Y/n): ', no_input) do |answer|
27
- unless answer.downcase == 'n'
27
+ unless answer.casecmp('n').zero?
28
28
  @token.write_access_token(@actual_space.name, access_token)
29
29
  @token.write_space_id(@actual_space.name, @actual_space.id)
30
30
  end
@@ -34,11 +34,11 @@ module Contentful
34
34
  end
35
35
 
36
36
  def fetch_space
37
- if @space.is_a?(String)
38
- @actual_space = client.spaces.find(@space)
39
- else
40
- @actual_space = @space
41
- end
37
+ @actual_space = if @space.is_a?(String)
38
+ client.spaces.find(@space)
39
+ else
40
+ @space
41
+ end
42
42
  end
43
43
 
44
44
  def fetch_access_token
@@ -14,6 +14,7 @@ module Contentful
14
14
  @skip_content_types = options.fetch(:skip_content_types, false)
15
15
  @no_publish = options.fetch(:no_publish, false)
16
16
  @quiet = options.fetch(:quiet, false)
17
+ @environment = options.fetch(:environment, 'master')
17
18
 
18
19
  super(token, space_id, options)
19
20
  end
@@ -39,7 +40,6 @@ module Contentful
39
40
  protected
40
41
 
41
42
  def fetch_space
42
- management_client_init
43
43
  client.spaces.find(@space)
44
44
  rescue Contentful::Management::NotFound
45
45
  output 'Space Not Found. Exiting!'
@@ -51,7 +51,7 @@ module Contentful
51
51
  def update_json_template(space)
52
52
  if ::File.exist?(@json_template)
53
53
  output "Updating from JSON Template '#{@json_template}'"
54
- Templates::JsonTemplate.new(space, @json_template, @mark_processed, true, @quiet, @skip_content_types, @no_publish).run
54
+ Templates::JsonTemplate.new(space, @json_template, @environment, @mark_processed, true, @quiet, @skip_content_types, @no_publish).run
55
55
  output "JSON Template '#{@json_template}' updated!"
56
56
  else
57
57
  output "JSON Template '#{@json_template}' does not exist. Please check that you specified the correct file name."
@@ -12,10 +12,11 @@ module Contentful
12
12
 
13
13
  attr_reader :content_types_only, :content_type_ids, :client
14
14
 
15
- def initialize(space_id, access_token, content_types_only, use_preview, content_type_ids)
15
+ def initialize(space_id, access_token, environment, content_types_only, use_preview, content_type_ids)
16
16
  @client = Contentful::Client.new(
17
- access_token: access_token,
18
17
  space: space_id,
18
+ access_token: access_token,
19
+ environment: environment,
19
20
  application_name: 'bootstrap',
20
21
  application_version: ::Contentful::Bootstrap::VERSION,
21
22
  api_url: use_preview ? PREVIEW_API_URL : DELIVERY_API_URL
@@ -137,9 +138,9 @@ module Contentful
137
138
  def map_field_properties(field)
138
139
  properties = {}
139
140
 
140
- [:id, :name, :type, :link_type, :required, :localized].each do |property|
141
+ %i[id name type link_type required localized].each do |property|
141
142
  value = field.public_send(property) if field.respond_to?(property)
142
- properties[Support.camel_case(property.to_s).to_sym] = value unless value.nil? || [:required, :localized].include?(property)
143
+ properties[Support.camel_case(property.to_s).to_sym] = value unless value.nil? || %i[required localized].include?(property)
143
144
  end
144
145
 
145
146
  items = field.items if field.respond_to?(:items)
@@ -4,7 +4,7 @@ module Contentful
4
4
  module Bootstrap
5
5
  module Support
6
6
  def self.camel_case(a_string)
7
- a_string.split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
7
+ a_string.split('_').each_with_object([]) { |e, a| a.push(a.empty? ? e : e.capitalize) }.join
8
8
  end
9
9
 
10
10
  def self.silence_stderr
@@ -6,10 +6,10 @@ module Contentful
6
6
  module Bootstrap
7
7
  module Templates
8
8
  class Base
9
- attr_reader :space, :skip_content_types
9
+ attr_reader :environment, :skip_content_types
10
10
 
11
- def initialize(space, quiet = false, skip_content_types = false, no_publish = false)
12
- @space = space
11
+ def initialize(space, environment_id = 'master', quiet = false, skip_content_types = false, no_publish = false)
12
+ @environment = space.environments.find(environment_id)
13
13
  @quiet = quiet
14
14
  @skip_content_types = skip_content_types
15
15
  @no_publish = no_publish
@@ -42,8 +42,7 @@ module Contentful
42
42
  []
43
43
  end
44
44
 
45
- def after_run
46
- end
45
+ def after_run; end
47
46
 
48
47
  protected
49
48
 
@@ -54,7 +53,7 @@ module Contentful
54
53
  def create_file(name, url, properties = {})
55
54
  image = Contentful::Management::File.new
56
55
  image.properties[:contentType] = properties.fetch(:contentType, 'image/jpeg')
57
- image.properties[:fileName] = "#{name}"
56
+ image.properties[:fileName] = name.to_s
58
57
  image.properties[:upload] = url
59
58
  image
60
59
  end
@@ -67,12 +66,6 @@ module Contentful
67
66
  output "Creating Content Type '#{ct['name']}'"
68
67
 
69
68
  fields = []
70
- content_type = space.content_types.new
71
- content_type.id = ct['id']
72
- content_type.name = ct['name']
73
- content_type.display_field = ct['displayField']
74
- content_type.description = ct['description']
75
-
76
69
  ct['fields'].each do |f|
77
70
  field = Contentful::Management::Field.new
78
71
  field.id = f['id']
@@ -90,8 +83,14 @@ module Contentful
90
83
  fields << field
91
84
  end
92
85
 
93
- content_type.fields = fields
94
- content_type.save
86
+ content_type = environment.content_types.create(
87
+ id: ct['id'],
88
+ name: ct['name'],
89
+ displayField: ct['displayField'],
90
+ description: ct['description'],
91
+ fields: fields
92
+ )
93
+
95
94
  content_type.activate
96
95
  rescue Contentful::Management::Conflict
97
96
  output "ContentType '#{ct['id']}' already created! Skipping"
@@ -112,7 +111,7 @@ module Contentful
112
111
  assets.each do |asset_json|
113
112
  begin
114
113
  output "Creating Asset '#{asset_json['title']}'"
115
- asset = space.assets.create(
114
+ asset = environment.assets.create(
116
115
  id: asset_json['id'],
117
116
  title: asset_json['title'],
118
117
  file: asset_json['file']
@@ -121,7 +120,7 @@ module Contentful
121
120
  rescue Contentful::Management::Conflict
122
121
  output "Asset '#{asset_json['id']}' already created! Updating instead."
123
122
 
124
- asset = space.assets.find(asset_json['id']).tap do |a|
123
+ asset = environment.assets.find(asset_json['id']).tap do |a|
125
124
  a.title = asset_json['title']
126
125
  a.file = asset_json['file']
127
126
  end
@@ -134,7 +133,7 @@ module Contentful
134
133
  assets.each do |asset_json|
135
134
  attempts = 0
136
135
  while attempts < 10
137
- asset = space.assets.find(asset_json['id'])
136
+ asset = environment.assets.find(asset_json['id'])
138
137
  unless asset.file.url.nil?
139
138
  asset.publish unless @no_publish
140
139
  break
@@ -149,7 +148,7 @@ module Contentful
149
148
  def create_entries
150
149
  content_types = []
151
150
  processed_entries = entries.map do |content_type_id, entry_list|
152
- content_type = space.content_types.find(content_type_id)
151
+ content_type = environment.content_types.find(content_type_id)
153
152
  content_types << content_type
154
153
 
155
154
  entry_list.each.map do |e|
@@ -177,15 +176,13 @@ module Contentful
177
176
 
178
177
  regular_fields.each do |rf|
179
178
  value = e.delete(rf)
180
- if value.is_a? ::Contentful::Bootstrap::Templates::Links::Base
181
- value = value.to_management_object
182
- end
179
+ value = value.to_management_object if value.is_a? ::Contentful::Bootstrap::Templates::Links::Base
183
180
  e[rf.to_sym] = value
184
181
  end
185
182
 
186
183
  begin
187
184
  output "Creating Entry #{e[:id]}"
188
- entry = content_type.entries.create({:id => e[:id]})
185
+ entry = content_type.entries.create(id: e[:id])
189
186
  entry.save
190
187
 
191
188
  e = e.clone
@@ -201,13 +198,13 @@ module Contentful
201
198
  processed_entries = processed_entries.map do |e|
202
199
  output "Populating Entry #{e[:id]}"
203
200
 
204
- entry = space.entries.find(e[:id])
201
+ entry = environment.entries.find(e[:id])
205
202
  e.delete(:id)
206
203
  entry.update(e)
207
204
  entry.save
208
205
 
209
206
  10.times do
210
- break if space.entries.find(entry.id).sys[:version] >= 4
207
+ break if environment.entries.find(entry.id).sys[:version] >= 4
211
208
  sleep(0.5)
212
209
  end
213
210
 
@@ -215,7 +212,7 @@ module Contentful
215
212
  end
216
213
 
217
214
  processed_entries.each do |e|
218
- space.entries.find(e).publish unless @no_publish
215
+ environment.entries.find(e).publish unless @no_publish
219
216
  end
220
217
  end
221
218
  end
@@ -6,29 +6,29 @@ module Contentful
6
6
  module Bootstrap
7
7
  module Templates
8
8
  class JsonTemplate < Base
9
- CONTENT_TYPES_KEY = 'contentTypes'
10
- ENTRIES_KEY = 'entries'
11
- ASSETS_KEY = 'assets'
12
- BOOTSTRAP_PROCCESSED_KEY = 'bootstrapProcessed'
13
- DISPLAY_FIELD_KEY = 'displayField'
14
- ALTERNATE_DISPLAY_FIELD_KEY = 'display_field'
15
- SYS_KEY = 'sys'
9
+ CONTENT_TYPES_KEY = 'contentTypes'.freeze
10
+ ENTRIES_KEY = 'entries'.freeze
11
+ ASSETS_KEY = 'assets'.freeze
12
+ BOOTSTRAP_PROCCESSED_KEY = 'bootstrapProcessed'.freeze
13
+ DISPLAY_FIELD_KEY = 'displayField'.freeze
14
+ ALTERNATE_DISPLAY_FIELD_KEY = 'display_field'.freeze
15
+ SYS_KEY = 'sys'.freeze
16
16
 
17
17
  attr_reader :assets, :entries, :content_types
18
18
 
19
- def initialize(space, file, mark_processed = false, all = true, quiet = false, skip_content_types = false, no_publish = false)
20
- super(space, quiet, skip_content_types, no_publish)
19
+ def initialize(space, file, environment = 'master', mark_processed = false, all = true, quiet = false, skip_content_types = false, no_publish = false)
21
20
  @file = file
21
+ json
22
+ check_version
23
+
24
+ super(space, environment, quiet, skip_content_types, no_publish)
25
+
22
26
  @all = all
23
27
  @mark_processed = mark_processed
24
28
 
25
- json
26
-
27
29
  @assets = process_assets
28
30
  @entries = process_entries
29
31
  @content_types = process_content_types
30
-
31
- check_version
32
32
  end
33
33
 
34
34
  def after_run
@@ -66,30 +66,32 @@ module Contentful
66
66
 
67
67
  def parse_json
68
68
  ::JSON.parse(::File.read(@file))
69
- rescue
69
+ rescue StandardError
70
70
  output 'File is not JSON. Exiting!'
71
71
  exit(1)
72
72
  end
73
73
 
74
74
  def check_version
75
75
  json_version = json.fetch('version', 0)
76
- gem_major_version = Contentful::Bootstrap.major_version
77
- unless gem_major_version == json_version
78
- fail "JSON Templates Version Mismatch. Current Version: #{gem_major_version}"
79
- end
76
+ gem_major = Contentful::Bootstrap.major_version
77
+ raise "JSON Templates Version Mismatch. Current Version: #{gem_major}" unless gem_major == json_version
80
78
  end
81
79
 
82
80
  def process_content_types
83
81
  processed_content_types = json.fetch(CONTENT_TYPES_KEY, [])
84
82
 
85
83
  unless all?
86
- processed_content_types = processed_content_types.select do |content_type|
87
- !content_type.fetch(BOOTSTRAP_PROCCESSED_KEY, false)
84
+ processed_content_types = processed_content_types.reject do |content_type|
85
+ content_type.fetch(BOOTSTRAP_PROCCESSED_KEY, false)
88
86
  end
89
87
  end
90
88
 
91
89
  processed_content_types.each do |content_type|
92
- content_type[DISPLAY_FIELD_KEY] = content_type.key?(ALTERNATE_DISPLAY_FIELD_KEY) ? content_type.delete(ALTERNATE_DISPLAY_FIELD_KEY) : content_type[DISPLAY_FIELD_KEY]
90
+ content_type[DISPLAY_FIELD_KEY] = if content_type.key?(ALTERNATE_DISPLAY_FIELD_KEY)
91
+ content_type.delete(ALTERNATE_DISPLAY_FIELD_KEY)
92
+ else
93
+ content_type[DISPLAY_FIELD_KEY]
94
+ end
93
95
  end
94
96
 
95
97
  processed_content_types
@@ -99,8 +101,8 @@ module Contentful
99
101
  unprocessed_assets = json.fetch(ASSETS_KEY, [])
100
102
 
101
103
  unless all?
102
- unprocessed_assets = unprocessed_assets.select do |asset|
103
- !asset.fetch(BOOTSTRAP_PROCCESSED_KEY, false)
104
+ unprocessed_assets = unprocessed_assets.reject do |asset|
105
+ asset.fetch(BOOTSTRAP_PROCCESSED_KEY, false)
104
106
  end
105
107
  end
106
108
 
@@ -108,57 +110,48 @@ module Contentful
108
110
  asset['file'] = create_file(
109
111
  asset['file']['filename'],
110
112
  asset['file']['url'],
111
- { contentType: asset['file'].fetch('contentType', 'image/jpeg') }
113
+ contentType: asset['file'].fetch('contentType', 'image/jpeg')
112
114
  )
113
115
  asset
114
116
  end
115
117
  end
116
118
 
119
+ def process_entry(entry)
120
+ processed_entry = {}
121
+ processed_entry['id'] = entry[SYS_KEY]['id'] if entry.key?(SYS_KEY) && entry[SYS_KEY].key?('id')
122
+
123
+ entry.fetch('fields', {}).each do |field, value|
124
+ if link?(value)
125
+ processed_entry[field] = create_link(value)
126
+ next
127
+ elsif array?(value)
128
+ processed_entry[field] = value.map { |i| link?(i) ? create_link(i) : i }
129
+ next
130
+ end
131
+
132
+ processed_entry[field] = value
133
+ end
134
+
135
+ processed_entry
136
+ end
137
+
117
138
  def process_entries
118
- processed_entries = {}
119
139
  unprocessed_entries = json.fetch(ENTRIES_KEY, {})
120
- unprocessed_entries.each do |content_type_id, entry_list|
140
+ unprocessed_entries.each_with_object({}) do |(content_type_id, entry_list), processed_entries|
121
141
  entries_for_content_type = []
122
142
 
123
143
  unless all?
124
- entry_list = entry_list.select do |entry|
125
- !entry[SYS_KEY].fetch(BOOTSTRAP_PROCCESSED_KEY, false)
144
+ entry_list = entry_list.reject do |entry|
145
+ entry[SYS_KEY].fetch(BOOTSTRAP_PROCCESSED_KEY, false)
126
146
  end
127
147
  end
128
148
 
129
149
  entry_list.each do |entry|
130
- processed_entry = {}
131
- array_fields = []
132
- link_fields = []
133
-
134
- processed_entry['id'] = entry[SYS_KEY]['id'] if entry.key?(SYS_KEY) && entry[SYS_KEY].key?('id')
135
-
136
- entry.fetch('fields', {}).each do |field, value|
137
- link_fields << field if link?(value)
138
- array_fields << field if array?(value)
139
-
140
- unless link_fields.include?(field) || array_fields.include?(field)
141
- processed_entry[field] = value
142
- end
143
- end
144
-
145
- link_fields.each do |lf|
146
- processed_entry[lf] = create_link(entry['fields'][lf])
147
- end
148
-
149
- array_fields.each do |af|
150
- processed_entry[af] = entry['fields'][af].map do |item|
151
- link?(item) ? create_link(item) : item
152
- end
153
- end
154
-
155
- entries_for_content_type << processed_entry
150
+ entries_for_content_type << process_entry(entry)
156
151
  end
157
152
 
158
153
  processed_entries[content_type_id] = entries_for_content_type
159
154
  end
160
-
161
- processed_entries
162
155
  end
163
156
 
164
157
  def create_link(link_properties)