class Contentful::Management::ResourceBuilder

Transforms a Contentful::Response into a Contentful::Resource or a Contentful::Error See example/resource_mapping.rb for avanced usage

Constants

DEFAULT_ENTRY_MAPPING

Default Entry Mapping @see _ README for more information on Entry Mapping

DEFAULT_RESOURCE_MAPPING

Default Resource Mapping @see _ README for more information on Resource Mapping

Attributes

client[R]
entry_mapping[R]
resource[R]
resource_mapping[R]
response[R]

Public Class Methods

new(response, client, resource_mapping = {}, entry_mapping = {}) click to toggle source
# File lib/contentful/management/resource_builder.rb, line 74
def initialize(response, client, resource_mapping = {}, entry_mapping = {})
  @response = response
  @client = client
  @included_resources = {}
  @known_resources = Hash.new { |hash, key| hash[key] = {} }
  @nested_locales = true
  @resource_mapping = default_resource_mapping.merge(resource_mapping)
  @entry_mapping = default_entry_mapping.merge(entry_mapping)
end

Public Instance Methods

array_or_sync_page(object) click to toggle source

Detects if a resource is an Contentful::Array or a SyncPage

# File lib/contentful/management/resource_builder.rb, line 150
def array_or_sync_page(object)
  if object['nextPageUrl'] || object['nextSyncUrl']
    SyncPage
  else
    Array
  end
end
content_type_id_for_entry(object) click to toggle source

Returns the id of the related ContentType, if there is one

# File lib/contentful/management/resource_builder.rb, line 145
def content_type_id_for_entry(object)
  object['sys']['contentType']['sys']['id']
end
create_all_resources!() click to toggle source

PARSING MECHANISM

  • raise error if response not valid

  • look for included objects and parse them to resources

  • parse main object to resource

  • replace links in included resources with known resources

  • replace links in main resource with known resources

  • return main resource

# File lib/contentful/management/resource_builder.rb, line 101
def create_all_resources!
  create_included_resources! response.object['includes']
  @resource = create_resource(response.object)

  unless @included_resources.empty?
    replace_links_in_included_resources_with_known_resources
    replace_links_with_known_resources @resource
  end

  @resource
rescue UnparsableResource => e
  e
end
create_resource(object) click to toggle source

Creates a single resource from the response object

# File lib/contentful/management/resource_builder.rb, line 116
def create_resource(object)
  res_class = detect_resource_class(object)
  @nested_locales ||= res_class.nested_locale_fields?
  res = res_class.new(object, response.request, client, @nested_locales)

  add_to_known_resources res
  replace_children res, object
  replace_child_array res.items if res.array?

  res
end
default_entry_mapping() click to toggle source

The default entry mapping

# File lib/contentful/management/resource_builder.rb, line 185
def default_entry_mapping
  DEFAULT_ENTRY_MAPPING.dup
end
default_resource_mapping() click to toggle source

The default mapping for detect_resource_class

# File lib/contentful/management/resource_builder.rb, line 180
def default_resource_mapping
  DEFAULT_RESOURCE_MAPPING.dup
end
detect_resource_class(object) click to toggle source

Uses the resource mapping to find the proper Resource class to initialize for this Response object type

The mapping value can be a

  • Class

  • Proc: Will be called, expected to return the proper Class

  • Symbol: Will be called as method of the ResourceBuilder itself

# File lib/contentful/management/resource_builder.rb, line 165
def detect_resource_class(object)
  type = object['sys'] && object['sys']['type']
  case res_class = resource_mapping[type]
  when Symbol
    public_send(res_class, object)
  when Proc
    res_class[object]
  when nil
    fail UnparsableResource, response
  else
    res_class
  end
end
find_entry_class(object) click to toggle source

Checks in a custom class for an entry was defined in entry_mapping

# File lib/contentful/management/resource_builder.rb, line 129
def find_entry_class(object)
  entry_mapping[content_type_id_for_entry(object)] || try_dynamic_entry(object)
end
get_dynamic_entry(object) click to toggle source

Finds the proper DynamicEntry class for an entry

# File lib/contentful/management/resource_builder.rb, line 139
def get_dynamic_entry(object)
  content_id = content_type_id_for_entry(object)
  client.dynamic_entry_cache[content_id.to_sym] if content_id
end
run() click to toggle source

Starts the parsing process. @return [Contentful::Management::Resource, Contentful::Management::Error]

# File lib/contentful/management/resource_builder.rb, line 86
def run
  if response.status == :ok
    create_all_resources!
  else
    response.object
  end
end
try_dynamic_entry(object) click to toggle source

Automatically converts Entry to DynamicEntry if in cache

# File lib/contentful/management/resource_builder.rb, line 134
def try_dynamic_entry(object)
  get_dynamic_entry(object) || Contentful::Management::Entry
end

Private Instance Methods

add_to_known_resources(res) click to toggle source
# File lib/contentful/management/resource_builder.rb, line 212
def add_to_known_resources(res)
  @known_resources[res.type][res.id] = res if res.sys && res.id && res.type != 'Link'
end
create_included_resources!(included_objects) click to toggle source
# File lib/contentful/management/resource_builder.rb, line 233
def create_included_resources!(included_objects)
  return unless included_objects

  included_objects.each do |type, objects|
    @included_resources[type] = Hash[
      objects.map do |object|
        res = create_resource(object)
        [res.id, res]
      end
    ]
  end
end
detect_child_arrays(object) click to toggle source
# File lib/contentful/management/resource_builder.rb, line 199
def detect_child_arrays(object)
  if object.is_a? Hash
    object.select do |_, value|
      value.is_a?(::Array) &&
        value.first &&
        value.first.is_a?(Hash) &&
        value.first.key?('sys')
    end
  else
    {}
  end
end
detect_child_objects(object) click to toggle source
# File lib/contentful/management/resource_builder.rb, line 191
def detect_child_objects(object)
  if object.is_a? Hash
    object.select { |_key, value| value.is_a?(Hash) && value.key?('sys') }
  else
    {}
  end
end
replace_child_array(child_array) click to toggle source
# File lib/contentful/management/resource_builder.rb, line 229
def replace_child_array(child_array)
  child_array.map! { |resource_object| create_resource(resource_object) }
end
replace_children(res, object) click to toggle source
# File lib/contentful/management/resource_builder.rb, line 216
def replace_children(res, object)
  object.each do |name, potential_objects|
    detect_child_objects(potential_objects).each do |child_name, child_object|
      res.public_send(name)[child_name.to_sym] = create_resource(child_object)
    end
    next if %w[includes metadata].include?(name)

    detect_child_arrays(potential_objects).each do |child_name, _child_array|
      replace_child_array res.public_send(name)[child_name.to_sym]
    end
  end
end