class Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver
Locates <a data-item-id=“”> tags in content and calls a user-defined method to supply the href for content item links. See github.com/Kentico/kontent-delivery-sdk-ruby#resolving-links
Public Class Methods
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
-
# 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
Public Instance Methods
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
-
# 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
Private Instance Methods
Uses the resolve_link
method to generate a URL for a ContentLink
object, or resolve_404
if the content item was not present in the response.
-
Args:
-
matches (
Array
) TheContentLink
objects with an ID matching a particular <a data-item-id=“”> tag -
id (
string
) The ID of the <a data-item-id=“”> tag being resolved
-
-
Returns:
-
string
A url to the item or 404 page
-
# File lib/delivery/resolvers/content_link_resolver.rb, line 67 def provide_url(matches, id) if matches.empty? if @not_found_handler.nil? resolve_404 id else @not_found_handler.call id end else if @found_handler.nil? resolve_link matches[0] else @found_handler.call matches[0] end end end
Accepts a tag found in the content and tries to locate matching source link from JSON response. If found, resolves URL and returns the tag with generated HREF.
-
Args:
-
tag (
string
) A <a data-item-id=“”> tag found in the content -
links (
Array
) The collection of links from an element's 'links' JSON node, converted toKentico::Kontent::Delivery::Resolvers::ContentLink
objects
-
-
Returns:
-
string
The <a data-item-id=“”> tag with an HREF generated by theprovide_url
method
-
# File lib/delivery/resolvers/content_link_resolver.rb, line 50 def resolve_tag(tag, links) matches = links.select { |link| link.id == tag['data-item-id'].to_s } url = provide_url matches, tag['data-item-id'] tag['href'] = url tag end