Class: ContentfulLite::EntriesArray

Inherits:
BaseArray
  • Object
show all
Defined in:
lib/contentful_lite/entries_array.rb

Instance Attribute Summary

Attributes inherited from BaseArray

#limit, #skip, #total

Instance Method Summary collapse

Methods inherited from BaseArray

#__getobj__, #__setobj__

Constructor Details

#initialize(raw) ⇒ EntriesArray

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of EntriesArray.

Parameters:

  • raw (Hash)

    raw response from Contentful API



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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