Class: Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/delivery/resolvers/content_link_resolver.rb

Overview

Instance Method Summary collapse

Constructor Details

#initialize(found_handler = nil, not_found_handler = nil) ⇒ ContentLinkResolver

Constructor.

  • Args:
    • found_handler (+lambda+) optional Method to be called when resolving a content link and the content item is present in the response
    • not_found_handler (+lambda+) optional Method to be called when resolving a content link and the content item isn't present in the response


16
17
18
19
# File 'lib/delivery/resolvers/content_link_resolver.rb', line 16

def initialize(found_handler = nil, not_found_handler = nil)
  @found_handler = found_handler
  @not_found = not_found_handler
end

Instance Method Details

#resolve(content, links) ⇒ Object

Resolves all links in the content.

  • Args:

    • content (+string+) The string value stored in the element
    • links (+Array+) The collection of links from an element's 'links' JSON node
  • Returns:

    • string The original content passed, with all links resolved


29
30
31
32
33
34
35
36
# File 'lib/delivery/resolvers/content_link_resolver.rb', line 29

def resolve(content, links)
  doc = Nokogiri::HTML.parse(content).xpath('//body')
  links = links.map { |link| ContentLink.new link }
  tags = doc.xpath('//a[@data-item-id]')
  # This line performs the link resolving and replaces the tags in doc
  tags.map { |tag| resolve_tag tag, links }
  doc.to_xhtml
end