class Contentful::RichTextCoercion
Coercion for RichText Types
Public Instance Methods
Source
# File lib/contentful/coercions.rb, line 105 def coerce(configuration) coerce_block(value, configuration) end
Resolves includes and removes unresolvable nodes
Private Instance Methods
Source
# File lib/contentful/coercions.rb, line 121 def coerce_block(block, configuration) return block unless block.is_a?(Hash) && block.key?('content') invalid_nodes = [] coerced_nodes = {} block['content'].each_with_index do |node, index| if link?(node) link = coerce_link(node, configuration) if !link.nil? node['data']['target'] = link else invalid_nodes << index end elsif content_block?(node) coerced_nodes[index] = coerce_block(node, configuration) end end coerced_nodes.each do |index, coerced_node| block['content'][index] = coerced_node end invalid_nodes.each do |index| block['content'].delete_at(index) end block end
Source
# File lib/contentful/coercions.rb, line 151 def coerce_link(node, configuration) return node unless node.key?('data') && node['data'].key?('target') return node['data']['target'] unless node['data']['target'].is_a?(::Hash) return node unless node['data']['target']['sys']['type'] == 'Link' return nil if Support.unresolvable?(node['data']['target'], configuration[:errors]) resource = configuration[:includes_for_single].find_link(node['data']['target']) # Resource is valid but unreachable return Link.new(node['data']['target'], configuration) if resource.nil? ResourceBuilder.new( resource, configuration, configuration[:localized], configuration[:depth] + 1, configuration[:errors] ).run end
Source
# File lib/contentful/coercions.rb, line 117 def content_block?(node) !node.fetch('content', []).empty? end
Source
# File lib/contentful/coercions.rb, line 111 def link?(node) !node['data'].is_a?(::Contentful::Entry) && !node.fetch('data', {}).empty? && node['data']['target'] end