class Contentful::Bootstrap::Templates::JsonTemplate

Constants

ALTERNATE_DISPLAY_FIELD_KEY
ASSETS_KEY
BOOTSTRAP_PROCCESSED_KEY
CONTENT_TYPES_KEY
DISPLAY_FIELD_KEY
ENTRIES_KEY
SYS_KEY

Attributes

assets[R]
content_types[R]
entries[R]

Public Class Methods

new(space, file, environment = 'master', mark_processed = false, all = true, quiet = false, skip_content_types = false, no_publish = false) click to toggle source
# File lib/contentful/bootstrap/templates/json_template.rb, line 19
def initialize(space, file, environment = 'master', mark_processed = false, all = true, quiet = false, skip_content_types = false, no_publish = false)
  @file = file
  json
  check_version

  super(space, environment, quiet, skip_content_types, no_publish)

  @all = all
  @mark_processed = mark_processed

  @assets = process_assets
  @entries = process_entries
  @content_types = process_content_types
end

Public Instance Methods

after_run() click to toggle source
# File lib/contentful/bootstrap/templates/json_template.rb, line 34
def after_run
  return unless mark_processed?

  # Re-parse JSON to avoid side effects from `Templates::Base`
  @json = parse_json

  json.fetch(CONTENT_TYPES_KEY, []).each do |content_type|
    content_type[BOOTSTRAP_PROCCESSED_KEY] = true
  end

  json.fetch(ASSETS_KEY, []).each do |asset|
    asset[BOOTSTRAP_PROCCESSED_KEY] = true
  end

  json.fetch(ENTRIES_KEY, {}).each do |_content_type_name, entry_list|
    entry_list.each do |entry|
      if entry.key?(SYS_KEY)
        entry[SYS_KEY][BOOTSTRAP_PROCCESSED_KEY] = true
      else
        entry[SYS_KEY] = { BOOTSTRAP_PROCCESSED_KEY => true }
      end
    end
  end

  ::File.write(@file, JSON.pretty_generate(@json))
end
json() click to toggle source
# File lib/contentful/bootstrap/templates/json_template.rb, line 61
def json
  @json ||= parse_json
end

Private Instance Methods

all?() click to toggle source
# File lib/contentful/bootstrap/templates/json_template.rb, line 168
def all?
  @all
end
array?(value) click to toggle source
# File lib/contentful/bootstrap/templates/json_template.rb, line 180
def array?(value)
  value.is_a?(::Array)
end
check_version() click to toggle source
# File lib/contentful/bootstrap/templates/json_template.rb, line 74
def check_version
  json_version = json.fetch('version', 0)
  gem_major = Contentful::Bootstrap.major_version
  raise "JSON Templates Version Mismatch. Current Version: #{gem_major}" unless gem_major == json_version
end
mark_processed?() click to toggle source
# File lib/contentful/bootstrap/templates/json_template.rb, line 172
def mark_processed?
  @mark_processed
end
parse_json() click to toggle source
# File lib/contentful/bootstrap/templates/json_template.rb, line 67
def parse_json
  ::JSON.parse(::File.read(@file))
rescue StandardError
  output 'File is not JSON. Exiting!'
  exit(1)
end
process_assets() click to toggle source
# File lib/contentful/bootstrap/templates/json_template.rb, line 100
def process_assets
  unprocessed_assets = json.fetch(ASSETS_KEY, [])

  unless all?
    unprocessed_assets = unprocessed_assets.reject do |asset|
      asset.fetch(BOOTSTRAP_PROCCESSED_KEY, false)
    end
  end

  unprocessed_assets.map do |asset|
    asset['file'] = create_file(
      asset['file']['filename'],
      asset['file']['url'],
      contentType: asset['file'].fetch('contentType', 'image/jpeg')
    )
    asset
  end
end
process_content_types() click to toggle source
# File lib/contentful/bootstrap/templates/json_template.rb, line 80
def process_content_types
  processed_content_types = json.fetch(CONTENT_TYPES_KEY, [])

  unless all?
    processed_content_types = processed_content_types.reject do |content_type|
      content_type.fetch(BOOTSTRAP_PROCCESSED_KEY, false)
    end
  end

  processed_content_types.each do |content_type|
    content_type[DISPLAY_FIELD_KEY] = if content_type.key?(ALTERNATE_DISPLAY_FIELD_KEY)
                                        content_type.delete(ALTERNATE_DISPLAY_FIELD_KEY)
                                      else
                                        content_type[DISPLAY_FIELD_KEY]
                                      end
  end

  processed_content_types
end
process_entries() click to toggle source
# File lib/contentful/bootstrap/templates/json_template.rb, line 138
def process_entries
  unprocessed_entries = json.fetch(ENTRIES_KEY, {})
  unprocessed_entries.each_with_object({}) do |(content_type_id, entry_list), processed_entries|
    entries_for_content_type = []

    unless all?
      entry_list = entry_list.reject do |entry|
        entry[SYS_KEY].fetch(BOOTSTRAP_PROCCESSED_KEY, false)
      end
    end

    entry_list.each do |entry|
      entries_for_content_type << process_entry(entry)
    end

    processed_entries[content_type_id] = entries_for_content_type
  end
end
process_entry(entry) click to toggle source
# File lib/contentful/bootstrap/templates/json_template.rb, line 119
def process_entry(entry)
  processed_entry = {}
  processed_entry['id'] = entry[SYS_KEY]['id'] if entry.key?(SYS_KEY) && entry[SYS_KEY].key?('id')

  entry.fetch('fields', {}).each do |field, value|
    if link?(value)
      processed_entry[field] = create_link(value)
      next
    elsif array?(value)
      processed_entry[field] = value.map { |i| link?(i) ? create_link(i) : i }
      next
    end

    processed_entry[field] = value
  end

  processed_entry
end