Class: Kentico::Kontent::Delivery::ContentItem
- Inherits:
-
Object
- Object
- Kentico::Kontent::Delivery::ContentItem
- Defined in:
- lib/delivery/models/content_item.rb
Instance Attribute Summary collapse
-
#content_link_url_resolver ⇒ Object
Returns the value of attribute content_link_url_resolver.
-
#inline_content_item_resolver ⇒ Object
Returns the value of attribute inline_content_item_resolver.
Instance Method Summary collapse
-
#elements ⇒ Object
Parses the ‘elements’ JSON object as a dynamic OpenStruct object.
-
#get_assets(code_name) ⇒ Object
Returns an array of assets inserted into the specified element of the ‘asset’ type.
-
#get_inline_items(code_name) ⇒ Object
Returns an array of ContentItems that are inserted as inline content items or componenets of a ‘rich_text’ element.
-
#get_links(code_name) ⇒ Object
Returns an array of ContentItems that are linked in a ‘modular_content’ element.
-
#get_string(code_name) ⇒ Object
Gets a string representation of the data stored in the element.
-
#initialize(source, content_link_url_resolver, inline_content_item_resolver, linked_items_resolver) ⇒ ContentItem
constructor
Constructor.
-
#system ⇒ Object
Parses the ‘system’ JSON object as a dynamic OpenStruct object.
Constructor Details
#initialize(source, content_link_url_resolver, inline_content_item_resolver, linked_items_resolver) ⇒ ContentItem
Constructor.
-
Args:
-
source (
JSON
) The response from a REST request for content items. The item may be on the root or under the ‘item’ node -
content_link_url_resolver ( Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver )
-
inline_content_item_resolver ( Kentico::Kontent::Delivery::Resolvers::InlineContentItemResolver )
-
linked_items_resolver ( Kentico::Kontent::Delivery::Resolvers::LinkedItemResolver )
-
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/delivery/models/content_item.rb', line 42 def initialize(source, content_link_url_resolver, inline_content_item_resolver, linked_items_resolver) @source = if source['item'].nil? source else source['item'] end @linked_items_resolver = linked_items_resolver self.content_link_url_resolver = content_link_url_resolver self.inline_content_item_resolver = inline_content_item_resolver end |
Instance Attribute Details
#content_link_url_resolver ⇒ Object
Returns the value of attribute content_link_url_resolver.
8 9 10 |
# File 'lib/delivery/models/content_item.rb', line 8 def content_link_url_resolver @content_link_url_resolver end |
#inline_content_item_resolver ⇒ Object
Returns the value of attribute inline_content_item_resolver.
8 9 10 |
# File 'lib/delivery/models/content_item.rb', line 8 def inline_content_item_resolver @inline_content_item_resolver end |
Instance Method Details
#elements ⇒ Object
Parses the ‘elements’ JSON object as a dynamic OpenStruct object.
-
Returns:
-
OpenStruct
The elements of the content item
-
15 16 17 18 19 20 21 |
# File 'lib/delivery/models/content_item.rb', line 15 def elements @elements unless @elements.nil? @elements = JSON.parse( JSON.generate(@source['elements']), object_class: OpenStruct ) end |
#get_assets(code_name) ⇒ Object
Returns an array of assets inserted into the specified element of the ‘asset’ type.
-
Args:
-
code_name (
string
) The code name of the desired element
-
-
Returns:
-
Array
The element’s assets parsed asOpenStruct
objects
-
84 85 86 87 |
# File 'lib/delivery/models/content_item.rb', line 84 def get_assets(code_name) element = get_element code_name element['value'].map { |n| OpenStruct.new(n) } end |
#get_inline_items(code_name) ⇒ Object
Returns an array of ContentItems that are inserted as inline content items or componenets of a ‘rich_text’ element.
-
Args:
-
code_name (
string
) The code name of the desired element
-
-
Returns:
-
Array
The element’s inline content items parsed asContentItem
objects
-
110 111 112 113 |
# File 'lib/delivery/models/content_item.rb', line 110 def get_inline_items(code_name) element = get_element code_name get_linked_items element['modular_content'] end |
#get_links(code_name) ⇒ Object
Returns an array of ContentItems that are linked in a ‘modular_content’ element.
-
Args:
-
code_name (
string
) The code name of the desired element
-
-
Returns:
-
Array
The element’s linked items parsed asContentItem
objects
-
97 98 99 100 |
# File 'lib/delivery/models/content_item.rb', line 97 def get_links(code_name) element = get_element code_name get_linked_items element['value'] end |
#get_string(code_name) ⇒ Object
Gets a string representation of the data stored in the element. Using this method instead of directly accessing the elements
collection causes the content to be resolved using the resolvers passed during instantiation. See github.com/Kentico/kontent-delivery-sdk-ruby#resolving-links
-
Args:
-
code_name (
string
) The code name of the desired element
-
-
Returns:
-
string
The data converted to a string, resolved if the element is a ‘rich_text’ element
-
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/delivery/models/content_item.rb', line 64 def get_string(code_name) element = get_element code_name content = element['value'] if element['type'] == 'rich_text' content = content_link_url_resolver.resolve content, element['links'] if should_resolve_links element inline_items = get_inline_items code_name content = inline_content_item_resolver.resolve content, inline_items if should_resolve_inline_content element end content.to_s end |
#system ⇒ Object
Parses the ‘system’ JSON object as a dynamic OpenStruct object.
-
Returns:
-
OpenStruct
The system properties of the content item
-
27 28 29 30 31 32 33 |
# File 'lib/delivery/models/content_item.rb', line 27 def system @system unless @system.nil? @system = JSON.parse( JSON.generate(@source['system']), object_class: OpenStruct ) end |