Method: ContentfulLite::CommonData#initialize

Defined in:
lib/contentful_lite/common_data.rb

#initialize(raw) ⇒ Object

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.

Parameters:

  • raw (Hash)

    raw response from Contentful API



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/contentful_lite/common_data.rb', line 11

def initialize(raw)
  @sys = raw['sys']
  @id = sys['id']
  @created_at = DateTime.parse sys['createdAt']
  @updated_at = DateTime.parse sys['updatedAt']
  @locale = sys['locale']
  @revision = sys['revision']
  @space_id = sys['space']['sys']['id']
  @environment_id = sys['environment']['sys']['id']
  @retrieved_at = DateTime.now

  if locale
    @locales = [locale]
    @localized_fields = { locale => raw['fields'] }
  else
    @locales = raw.fetch('fields', {}).values.collect_concat(&:keys).uniq
    @localized_fields = @locales.each_with_object({}) do |locale, hash|
      hash[locale] = raw['fields'].transform_values { |value| value[locale] }
    end
  end

  @default_locale = @locales.first
end