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
Attributes
Public Class Methods
# 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
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
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
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
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
The default entry mapping
# File lib/contentful/management/resource_builder.rb, line 185 def default_entry_mapping DEFAULT_ENTRY_MAPPING.dup end
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
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
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
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
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
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
# 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
# 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
# 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
# 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
# File lib/contentful/management/resource_builder.rb, line 284 def maybe_replace_link(link, parent, key) return unless @known_resources[link.link_type] && @known_resources[link.link_type].key?(link.id) parent[key] = @known_resources[link.link_type][link.id] end
# 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
# 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
# File lib/contentful/management/resource_builder.rb, line 276 def replace_link_or_check_recursively(property_value, property_container, property_name, seen_resource_ids) if property_value.is_a? Link maybe_replace_link(property_value, property_container, property_name) elsif property_value.is_a?(Resource) && property_value.sys && !seen_resource_ids.include?(property_value.id) replace_links_with_known_resources(property_value, seen_resource_ids) end end
# File lib/contentful/management/resource_builder.rb, line 270 def replace_links_in_array(property_container, seen_resource_ids) property_container.each.with_index do |child_property, property_index| replace_link_or_check_recursively child_property, property_container, property_index, seen_resource_ids end end
# File lib/contentful/management/resource_builder.rb, line 291 def replace_links_in_included_resources_with_known_resources @included_resources.each do |_, for_type| for_type.each do |_, res| replace_links_with_known_resources(res) end end end
# File lib/contentful/management/resource_builder.rb, line 260 def replace_links_in_properties(property_container, seen_resource_ids) property_container.each do |property_name, property_value| if property_value.is_a? ::Array replace_links_in_array property_value, seen_resource_ids else replace_link_or_check_recursively property_value, property_container, property_name, seen_resource_ids end end end
# File lib/contentful/management/resource_builder.rb, line 246 def replace_links_with_known_resources(res, seen_resource_ids = []) seen_resource_ids << res.id property_containers = %i[properties sys fields].map do |property_container_name| res.public_send(property_container_name) end.compact property_containers.each do |property_container| replace_links_in_properties(property_container, seen_resource_ids) end replace_links_in_array res.items, seen_resource_ids if res.array? end