Module: ContentfulLite::CommonData

Included in:
Asset, Entry
Defined in:
lib/contentful_lite/common_data.rb

Overview

Parses data common to all Contentful resources.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



7
8
9
# File 'lib/contentful_lite/common_data.rb', line 7

def created_at
  @created_at
end

#default_localeObject (readonly)

Returns the value of attribute default_locale.



7
8
9
# File 'lib/contentful_lite/common_data.rb', line 7

def default_locale
  @default_locale
end

#environment_idObject (readonly)

Returns the value of attribute environment_id.



7
8
9
# File 'lib/contentful_lite/common_data.rb', line 7

def environment_id
  @environment_id
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/contentful_lite/common_data.rb', line 7

def id
  @id
end

#localesObject (readonly)

Returns the value of attribute locales.



7
8
9
# File 'lib/contentful_lite/common_data.rb', line 7

def locales
  @locales
end

#localized_fieldsObject (readonly)

Returns the value of attribute localized_fields.



7
8
9
# File 'lib/contentful_lite/common_data.rb', line 7

def localized_fields
  @localized_fields
end

#retrieved_atObject (readonly)

Returns the value of attribute retrieved_at.



7
8
9
# File 'lib/contentful_lite/common_data.rb', line 7

def retrieved_at
  @retrieved_at
end

#revisionObject (readonly)

Returns the value of attribute revision.



7
8
9
# File 'lib/contentful_lite/common_data.rb', line 7

def revision
  @revision
end

#space_idObject (readonly)

Returns the value of attribute space_id.



7
8
9
# File 'lib/contentful_lite/common_data.rb', line 7

def space_id
  @space_id
end

#sysObject (readonly)

Returns the value of attribute sys.



7
8
9
# File 'lib/contentful_lite/common_data.rb', line 7

def sys
  @sys
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



7
8
9
# File 'lib/contentful_lite/common_data.rb', line 7

def updated_at
  @updated_at
end

Instance Method Details

#as_json(args = {}) ⇒ Hash

Provided for compatibility with Rails JSON serializer

Parameters:

  • serialized_ids (Array<String>)

    Ids already serialized, required for possible mutual references

  • options (Hash)

    Serialization options, only provided for compatibility

Returns:

  • (Hash)

    a Hash representation of the link, to be formated as JSON



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/contentful_lite/common_data.rb', line 80

def as_json(args = {})
  args[:serialized_ids] ||= []

  return to_link.as_json if args[:serialized_ids].include?(id)

  {
    "sys" => sys,
    "fields" => fields.transform_values do |value|
      if value.respond_to?(:as_json)
        value.as_json(**args.merge(serialized_ids: args[:serialized_ids] + [id]))
      else
        value
      end
    end
  }
end

#fields(locale: nil) ⇒ Hash

Returns a hash with field => value format using specified locale

Parameters:

  • locale (String, nil) (defaults to: nil)

    the locale that will be used for reading the fields. Defaults to #locale

Returns:

  • (Hash)


52
53
54
# File 'lib/contentful_lite/common_data.rb', line 52

def fields(locale: nil)
  @localized_fields.fetch(locale || self.locale, {})
end

#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

#localeObject

Provides access to the locale being used to read fields



36
37
38
# File 'lib/contentful_lite/common_data.rb', line 36

def locale
  @locale || @default_locale
end

#locale=(value) ⇒ Object

Sets the locale that will be used to read fields

Parameters:

  • value (String)

    the locale code

Raises:

  • (StandardError)

    ‘Invalid Locale’ for locales not included on the API response



43
44
45
46
47
# File 'lib/contentful_lite/common_data.rb', line 43

def locale=(value)
  raise 'Invalid Locale' unless value.in?(locales)

  @locale = value
end

Gets a ContentfulLite::Link to the entry

Returns:



72
73
74
# File 'lib/contentful_lite/common_data.rb', line 72

def to_link
  ContentfulLite::Link.new(self)
end

#with_locale(locale) { ... } ⇒ Object

Executes a block with #locale set to the received locale. Then sets it back to current locale.

Parameters:

  • locale (String)

    the locale to run the block with

Yields:



60
61
62
63
64
65
66
67
68
# File 'lib/contentful_lite/common_data.rb', line 60

def with_locale(locale)
  old_locale = @locale
  @locale = locale unless locale.nil?
  begin
    yield
  ensure
    @locale = old_locale
  end
end