class ContentfulLite::EntriesArray

Public Class Methods

new(raw) click to toggle source

@param raw [Hash] raw response from Contentful API @api private

Calls superclass method ContentfulLite::BaseArray::new
# File lib/contentful_lite/entries_array.rb, line 5
def initialize(raw)
  super(raw)

  # Collect arrays of missing (unresolvable) links
  @errors = raw.fetch('errors', []).collect! { |error| error.fetch('details', {}) }.each_with_object({}) do |error_detail, hash|
    type = error_detail['linkType'].downcase.to_sym
    hash[type] ||= []
    hash[type] << error_detail['id']
  end

  # Create the array of asset objects
  @assets = hash_by_id(
    raw.fetch('includes', {}).fetch('Asset', [])
  ).transform_values! { |asset| ContentfulLite::Asset.new(asset) }

  # Now parse the entries, this is the tricky part
  @entries = {}
  @raw_entries = hash_by_id(
    raw.fetch('items', []) + raw.fetch('includes', {}).fetch('Entry', [])
  )
  @items.collect! { |item| build_entry(item['sys']['id']) }
end

Private Instance Methods

build_entry(id) click to toggle source
# File lib/contentful_lite/entries_array.rb, line 48
def build_entry(id)
  @entries[id] || begin
    hash = @raw_entries.delete(id)
    return nil if hash.nil?

    klass = ContentfulLite::Entry.get_class(hash['sys']['contentType']['sys']['id'])
    @entries[id] = klass.new(hash)
    @entries[id].localized_fields.values.each do |fields|
      fields.transform_values! { |field| solve_link(field) }
    end
    @entries[id]
  end
end
hash_by_id(arr) click to toggle source
# File lib/contentful_lite/entries_array.rb, line 42
def hash_by_id(arr)
  arr.each_with_object({}) do |element, hash|
    hash[element['sys']['id']] = element
  end
end