|
1 | | -# contentstack Utils Ruby |
| 1 | +# Contentstack Utils Ruby |
| 2 | + |
| 3 | +Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. Read More. |
| 4 | + |
| 5 | +This guide will help you get started with Contentstack Ruby Utils SDK to build apps powered by Contentstack. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- Ruby version 2.0 or later |
| 10 | + |
| 11 | +## SDK Installation and Setup |
| 12 | + |
| 13 | +To set up Ruby Utils SDK, install it via gem: |
| 14 | +```sh |
| 15 | +gem install contentstack_utils |
| 16 | +``` |
| 17 | + |
| 18 | +> Note: If you are using Contentstack Ruby SDK, then “contentstack/utils” is already imported into your project. |
| 19 | +
|
| 20 | +## Usage |
| 21 | + |
| 22 | +Let’s learn how you can use Utils SDK to render embedded items. |
| 23 | + |
| 24 | +### Create Render Option: |
| 25 | + |
| 26 | +To render embedded items on the front-end, use the render_option function, and define the UI elements you want to show in the front-end of your website, as shown in the example code below: |
| 27 | +```ruby |
| 28 | +class CustomLOption < ContentstackUtils::Model::Option |
| 29 | + def render_option(embeddedObject, metadata) |
| 30 | + case metadata.style_type |
| 31 | + when 'block' |
| 32 | + if metadataArray.content_type_uid === 'product' |
| 33 | + return "<div> |
| 34 | + <h2 >#{embeddedObject["title"]}</h2> |
| 35 | + <img src=#{embeddedObject["product_image"]["url"]} alt=#{embeddedObject["product_image"]["title"]}/> |
| 36 | + <p>#{embeddedObject["price"]}</p> |
| 37 | + </div>" |
| 38 | + end |
| 39 | + when 'inline' |
| 40 | + return "<span><b>#{embeddedObject["title"]}</b> - #{embeddedObject["description"]}</span>" |
| 41 | + when link |
| 42 | + return "<a href='#{metadata.attributes["href"].value}'>#{metadata.text}</a>" |
| 43 | + when 'display' |
| 44 | + return "<img src='#{metadata.attributes["src"].value}' alt='#{metadata.alt}' />" |
| 45 | + when download |
| 46 | + return "<a href='#{metadata.attributes["href"].value}'>#{metadata.text}</a>" |
| 47 | + end |
| 48 | + super(embeddedObject, metadata) |
| 49 | + end |
| 50 | +end |
| 51 | +``` |
| 52 | +## Basic Queries |
| 53 | + |
| 54 | +Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry. |
| 55 | + |
| 56 | +### Fetch Embedded Item(s) from a Single Entry: |
| 57 | + |
| 58 | +To get an embedded item of a single entry, you need to provide the stack API key, environment name, delivery token, content type’s UID, and entry’s UID. Then, use the include_embedded_items function as shown below: |
| 59 | +```ruby |
| 60 | +require 'contentstack' |
| 61 | + |
| 62 | +@stack = Contentstack::Client.new('<API_KEY>', '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>', '<ENVIRONMENT>') |
| 63 | +@entry = @stack.content_type('<CONTENT_TYPE>').entry('<ENTRY_UID>') |
| 64 | +.include_embedded_items |
| 65 | +.fetch |
| 66 | + |
| 67 | +@rendered_rich_text = Contentstack.render_content(@entry.rte_field_uid, ContentstackUtils::Model::Option.new(@entry)) |
| 68 | +``` |
| 69 | + |
| 70 | +If you want to render embedded items using the CustomOption function, you can refer to the code below: |
| 71 | +```ruby |
| 72 | +@rendered_rich_text = Contentstack.render_content(@entry.rte_field_uid, CustomLOption.new(@entry)) |
| 73 | +``` |
| 74 | +### Fetch Embedded Item(s) from Multiple Entries |
| 75 | + |
| 76 | +To get embedded items from multiple entries, you need to provide the stack API key, environment name, delivery token, and content type’s UID. |
| 77 | +```ruby |
| 78 | +require 'contentstack' |
| 79 | +@stack = Contentstack::Client.new('<API_KEY>', '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>', '<ENVIRONMENT>') |
| 80 | +@query = @stack.content_type('<CONTENT_TYPE>').query |
| 81 | +@entries = @query.where('title', 'welcome') |
| 82 | +.include_embedded_items |
| 83 | +.fetch |
| 84 | + |
| 85 | +@entries.each do |entry| |
| 86 | + Contentstack.render_content(@entry.rte_field_uid, ContentstackUtils::Model::Option.new(@entry)) |
| 87 | +end |
| 88 | +``` |
0 commit comments