class Contentful::Entry

Resource class for Entry. @see _ www.contentful.com/developers/documentation/content-delivery-api/#entries

Public Instance Methods

entry?() click to toggle source

Returns true for resources that are entries

# File lib/contentful/entry.rb, line 14
def entry?
  true
end

Protected Instance Methods

content_type_field?(name) click to toggle source
# File lib/contentful/entry.rb, line 107
def content_type_field?(name)
  content_type_key = Support.snakify('contentType', @configuration[:use_camel_case])

  content_type = ContentTypeCache.cache_get(
    sys[:space].id,
    sys[content_type_key.to_sym].id
  )

  return false if content_type.nil?

  !content_type.field_for(name).nil?
end
empty_field_error(name) click to toggle source
# File lib/contentful/entry.rb, line 120
def empty_field_error(name)
  return nil unless @configuration[:raise_for_empty_fields]
  fail EmptyFieldError, name
end
repr_name() click to toggle source
# File lib/contentful/entry.rb, line 125
def repr_name
  content_type_key = Support.snakify('contentType', @configuration[:use_camel_case]).to_sym
  "#{super}[#{sys[content_type_key].id}]"
end

Private Instance Methods

build_nested_resource(value, includes, entries, errors) click to toggle source

Maximum include depth is 10 in the API, but we raise it to 20 (by default), in case one of the included items has a reference in an upper level, so we can keep the include chain for that object as well Any included object after the maximum include resolution depth will be just a Link

# File lib/contentful/entry.rb, line 60
def build_nested_resource(value, includes, entries, errors)
  if @depth < @configuration.fetch(:max_include_resolution_depth, 20)
    resource = includes.find_link(value)
    return resolve_include(resource, includes, entries, errors) unless resource.nil?
  end

  build_link(value)
end
coerce(field_id, value, includes, errors, entries = {}) click to toggle source
Calls superclass method
# File lib/contentful/entry.rb, line 20
def coerce(field_id, value, includes, errors, entries = {})
  if Support.link?(value)
    return nil if Support.unresolvable?(value, errors)
    return build_nested_resource(value, includes, entries, errors)
  end
  return coerce_link_array(value, includes, errors, entries) if Support.link_array?(value)

  content_type_key = Support.snakify('contentType', @configuration[:use_camel_case])
  content_type = ContentTypeCache.cache_get(sys[:space].id, sys[content_type_key.to_sym].id)

  unless content_type.nil?
    content_type_field = content_type.field_for(field_id)
    coercion_configuration = @configuration.merge(
      includes_for_single:
        @configuration.fetch(:includes_for_single, Includes.new) + includes,
      _entries_cache: entries,
      localized: localized,
      depth: @depth,
      errors: errors
    )
    return content_type_field.coerce(value, coercion_configuration) unless content_type_field.nil?
  end

  super(field_id, value, includes, errors, entries)
end
known_contentful_object?(object) click to toggle source
# File lib/contentful/entry.rb, line 91
def known_contentful_object?(object)
  (object.is_a?(Contentful::Entry) || object.is_a?(Contentful::Asset))
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/contentful/entry.rb, line 95
def method_missing(name, *args, &block)
  return empty_field_error(name) if content_type_field?(name)

  super
end
resolve_include(resource, includes, entries, errors) click to toggle source
# File lib/contentful/entry.rb, line 69
def resolve_include(resource, includes, entries, errors)
  require_relative 'resource_builder'

  ResourceBuilder.new(
    resource,
    @configuration.merge(
      includes_for_single:
        @configuration.fetch(:includes_for_single, Includes.new) + includes,
      _entries_cache: entries
    ),
    localized,
    @depth + 1,
    errors
  ).run
end
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/contentful/entry.rb, line 101
def respond_to_missing?(name, include_private = false)
  content_type_field?(name) || super
end