contentstack_utils 0.1.0.pre.beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.ruby-version +1 -0
- data/.yardopts +3 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +76 -0
- data/LICENSE +21 -0
- data/README.md +88 -0
- data/Rakefile +33 -0
- data/contentstack_utils.gemspec +32 -0
- data/lib/contentstack_utils.rb +4 -0
- data/lib/contentstack_utils/interface/renderable.rb +9 -0
- data/lib/contentstack_utils/model/metadata.rb +44 -0
- data/lib/contentstack_utils/model/options.rb +34 -0
- data/lib/contentstack_utils/support/helper.rb +3 -0
- data/lib/contentstack_utils/utils.rb +54 -0
- data/lib/contentstack_utils/version.rb +3 -0
- data/spec/lib/model/metadata_spec.rb +55 -0
- data/spec/lib/model/option_spec.rb +86 -0
- data/spec/lib/utils_spec.rb +65 -0
- data/spec/mock/constant_render_options.rb +16 -0
- data/spec/mock/custom_render_option.rb +24 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/support/constant.rb +617 -0
- data/spec/support/xml_parse.rb +13 -0
- metadata +174 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: df97218ab0794ad8c4adff69d4146df7e736eecb5b302c2f2bf8892e45c53262
|
4
|
+
data.tar.gz: 1112de7f8c3df79c172b623f1981e8de31d54c4ac3407e07aad5a5058f72ba1f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 03d0dd9b13849d6f605855ec2e0bbdf551c0b02489bb38d8432747002639482db8188d065c53f9468367b7c9dba4654130927fdd1bf721d12576292853b4f868
|
7
|
+
data.tar.gz: 4e232dc32139bb1500fdfb6755710ba180d5492d1cda14ee3937af2b3a607c48fb3ba08fe5cf6bf742d8945bced1d7c8c01127d049d5f7760a146cef1abf30e5
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6
|
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
contentstack_utils (1.0.0)
|
5
|
+
activesupport
|
6
|
+
nokogiri
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (6.1.0)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 1.6, < 2)
|
14
|
+
minitest (>= 5.1)
|
15
|
+
tzinfo (~> 2.0)
|
16
|
+
zeitwerk (~> 2.3)
|
17
|
+
addressable (2.7.0)
|
18
|
+
public_suffix (>= 2.0.2, < 5.0)
|
19
|
+
concurrent-ruby (1.1.7)
|
20
|
+
crack (0.4.5)
|
21
|
+
rexml
|
22
|
+
diff-lcs (1.4.4)
|
23
|
+
docile (1.3.4)
|
24
|
+
hashdiff (1.0.1)
|
25
|
+
i18n (1.8.7)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
mini_portile2 (2.5.0)
|
28
|
+
minitest (5.14.2)
|
29
|
+
nokogiri (1.11.0)
|
30
|
+
mini_portile2 (~> 2.5.0)
|
31
|
+
racc (~> 1.4)
|
32
|
+
public_suffix (4.0.6)
|
33
|
+
racc (1.5.2)
|
34
|
+
rake (13.0.3)
|
35
|
+
rexml (3.2.4)
|
36
|
+
rspec (3.10.0)
|
37
|
+
rspec-core (~> 3.10.0)
|
38
|
+
rspec-expectations (~> 3.10.0)
|
39
|
+
rspec-mocks (~> 3.10.0)
|
40
|
+
rspec-core (3.10.1)
|
41
|
+
rspec-support (~> 3.10.0)
|
42
|
+
rspec-expectations (3.10.1)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.10.0)
|
45
|
+
rspec-mocks (3.10.1)
|
46
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
47
|
+
rspec-support (~> 3.10.0)
|
48
|
+
rspec-support (3.10.1)
|
49
|
+
simplecov (0.21.1)
|
50
|
+
docile (~> 1.1)
|
51
|
+
simplecov-html (~> 0.11)
|
52
|
+
simplecov_json_formatter (~> 0.1)
|
53
|
+
simplecov-html (0.12.3)
|
54
|
+
simplecov_json_formatter (0.1.2)
|
55
|
+
tzinfo (2.0.4)
|
56
|
+
concurrent-ruby (~> 1.0)
|
57
|
+
webmock (3.11.0)
|
58
|
+
addressable (>= 2.3.6)
|
59
|
+
crack (>= 0.3.2)
|
60
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
61
|
+
yard (0.9.26)
|
62
|
+
zeitwerk (2.4.2)
|
63
|
+
|
64
|
+
PLATFORMS
|
65
|
+
ruby
|
66
|
+
|
67
|
+
DEPENDENCIES
|
68
|
+
contentstack_utils!
|
69
|
+
rake
|
70
|
+
rspec
|
71
|
+
simplecov
|
72
|
+
webmock
|
73
|
+
yard
|
74
|
+
|
75
|
+
BUNDLED WITH
|
76
|
+
1.17.2
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2012-2021 Contentstack. All Rights Reserved
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
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
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'yard'
|
6
|
+
YARD::Rake::YardocTask.new do |t|
|
7
|
+
t.files = ['lib/contentstack_utils/*.rb', 'lib/contentstack_utils.rb'] # optional
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'rspec/core/rake_task'
|
11
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
12
|
+
t.pattern = Dir.glob('spec/**/*_spec.rb')
|
13
|
+
t.rspec_opts = '--format documentation'
|
14
|
+
end
|
15
|
+
|
16
|
+
require "rdoc/task"
|
17
|
+
task ghpages: :rdoc do
|
18
|
+
%x[git checkout gh-pages]
|
19
|
+
require "fileutils"
|
20
|
+
FileUtils.rm_rf "/tmp/html"
|
21
|
+
FileUtils.mv "html", "/tmp"
|
22
|
+
FileUtils.rm_rf "*"
|
23
|
+
FileUtils.cp_r Dir.glob("/tmp/html/*"), "."
|
24
|
+
end
|
25
|
+
|
26
|
+
RDoc::Task.new do |doc|
|
27
|
+
doc.main = "README.rdoc"
|
28
|
+
doc.title = "Rake -- Ruby Make"
|
29
|
+
doc.rdoc_files = FileList.new %w[lib LICENSE doc/**/*.rdoc *.rdoc]
|
30
|
+
doc.rdoc_dir = "html"
|
31
|
+
end
|
32
|
+
|
33
|
+
task default: :spec
|
@@ -0,0 +1,32 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'contentstack_utils/version'
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = %q{contentstack_utils}
|
7
|
+
s.version = ContentstackUtils::VERSION.dup
|
8
|
+
s.date = Time.now
|
9
|
+
s.authors = [%q{Contentstack}]
|
10
|
+
s.email = ["support@contentstack.com"]
|
11
|
+
|
12
|
+
s.required_ruby_version = '>= 2.0'
|
13
|
+
|
14
|
+
s.license = "MIT"
|
15
|
+
s.homepage = "https://github.com/contentstack/contentstack-utils-ruby"
|
16
|
+
|
17
|
+
s.summary = %q{Contentstack Ruby Utils for }
|
18
|
+
s.description = %q{Contentstack Ruby client for the Content Delivery API}
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = s.files.grep(%r{^spec/})
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
|
24
|
+
s.add_dependency 'activesupport', '~> 6.1.0'
|
25
|
+
s.add_dependency 'nokogiri', '~> 1.11.0'
|
26
|
+
|
27
|
+
s.add_development_dependency 'rake', '~> 13.0.3'
|
28
|
+
s.add_development_dependency 'rspec', '~> 3.10.0'
|
29
|
+
s.add_development_dependency 'webmock', '~> 3.11.0'
|
30
|
+
s.add_development_dependency 'simplecov', '~> 0.21.1'
|
31
|
+
s.add_development_dependency 'yard', '~> 0.9.26'
|
32
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
|
2
|
+
module ContentstackUtils
|
3
|
+
module Model
|
4
|
+
class Metadata
|
5
|
+
def initialize( element )
|
6
|
+
@itemType = element.attribute('type')
|
7
|
+
@styleType = element.attribute('sys-style-type')
|
8
|
+
@itemUid ||= element.attribute('data-sys-entry-uid') || element.attribute('data-sys-asset-uid')
|
9
|
+
@contentTypeUid = element.attribute('data-sys-content-type-uid')
|
10
|
+
@text = element.text
|
11
|
+
@element = element
|
12
|
+
@attributes = element.attributes
|
13
|
+
end
|
14
|
+
|
15
|
+
def item_type
|
16
|
+
@itemType ? @itemType.value : nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def style_type
|
20
|
+
@styleType ? @styleType.value : nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def item_uid
|
24
|
+
@itemUid ? @itemUid.value : nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def content_type_uid
|
28
|
+
@contentTypeUid ? @contentTypeUid.value : nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def text
|
32
|
+
@text
|
33
|
+
end
|
34
|
+
|
35
|
+
def element
|
36
|
+
@element
|
37
|
+
end
|
38
|
+
|
39
|
+
def attributes
|
40
|
+
@attributes
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative '../interface/renderable.rb'
|
2
|
+
require_relative './metadata.rb'
|
3
|
+
|
4
|
+
module ContentstackUtils
|
5
|
+
module Model
|
6
|
+
class Options < Interface::Rendarable
|
7
|
+
|
8
|
+
def initialize(entry)
|
9
|
+
@entry = entry
|
10
|
+
end
|
11
|
+
|
12
|
+
def entry
|
13
|
+
@entry
|
14
|
+
end
|
15
|
+
|
16
|
+
def render_option(embeddedObject, metadata)
|
17
|
+
renderString = ''
|
18
|
+
case metadata.style_type
|
19
|
+
when 'block'
|
20
|
+
renderString = "<div><p>#{embeddedObject['title'] || embeddedObject['uid']}</p><p>Content type: <span>#{embeddedObject['_content_type_uid']}</span></p></div>"
|
21
|
+
when 'inline'
|
22
|
+
renderString = "<span>#{embeddedObject["title"] || embeddedObject["uid"]}</span>";
|
23
|
+
when 'link'
|
24
|
+
renderString = "<a href='#{(metadata.attributes["href"] ? metadata.attributes["href"].value : nil) || embeddedObject["url"] || embeddedObject["title"] || embeddedObject["uid"]}'>#{(metadata.text && metadata.text != '' ? metadata.text : (embeddedObject["title"] || embeddedObject["uid"]))}</a>";
|
25
|
+
when 'display'
|
26
|
+
renderString = "<img src='#{(metadata.attributes["href"] ? metadata.attributes["href"].value : nil)|| embeddedObject["url"]}' alt='#{(metadata.attributes["alt"] ? metadata.attributes["alt"].value : (embeddedObject["title"] || embeddedObject["filename"] || embeddedObject["uid"]))}' />";
|
27
|
+
when 'download'
|
28
|
+
renderString = "<a href='#{(metadata.attributes["href"] ? metadata.attributes["href"].value : nil) || embeddedObject["url"]}'>#{(metadata.text && metadata.text != '' ? metadata.text : (embeddedObject["filename"] || embeddedObject["title"] || embeddedObject["uid"]))}</a>";
|
29
|
+
end
|
30
|
+
renderString
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative './model/options.rb'
|
2
|
+
require_relative './model/metadata.rb'
|
3
|
+
require_relative './support/helper.rb'
|
4
|
+
require 'nokogiri'
|
5
|
+
|
6
|
+
module ContentstackUtils
|
7
|
+
def self.render_content(content, options)
|
8
|
+
if (content.instance_of? Array)
|
9
|
+
result = []
|
10
|
+
content.each do |n|
|
11
|
+
result.push(render_string(n, options))
|
12
|
+
end
|
13
|
+
result
|
14
|
+
elsif content.instance_of? String
|
15
|
+
render_string(content, options)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private_class_method def self.render_string(string, options)
|
20
|
+
xml_doc = Nokogiri::HTML(appendFrame(string))
|
21
|
+
result = xml_doc.xpath('//documentfragmentcontainer').inner_html
|
22
|
+
findEmbeddedObject(xml_doc).each do |metadata|
|
23
|
+
object = findObject(metadata, options.entry)
|
24
|
+
replaceString = ''
|
25
|
+
if object!= nil && object.length() > 0
|
26
|
+
replaceString = options.render_option(object[0], metadata)
|
27
|
+
end
|
28
|
+
result = result.sub(metadata.element.to_html, replaceString)
|
29
|
+
end
|
30
|
+
result
|
31
|
+
end
|
32
|
+
|
33
|
+
private_class_method def self.findEmbeddedObject(doc)
|
34
|
+
metadataArray = []
|
35
|
+
doc.xpath('//*[contains(@class, "embedded-asset") or contains(@class, "embedded-entry")]').each do |n|
|
36
|
+
metadataArray.push(Model::Metadata.new(n))
|
37
|
+
end
|
38
|
+
metadataArray
|
39
|
+
end
|
40
|
+
|
41
|
+
private_class_method def self.findObject(metadata, entry)
|
42
|
+
if entry.has_key? '_embedded_items'
|
43
|
+
embedItems = entry['_embedded_items']
|
44
|
+
keys = embedItems.keys
|
45
|
+
keys.each do |key|
|
46
|
+
object = embedItems[key].select { |embedObject| embedObject['uid'] == metadata.item_uid }
|
47
|
+
if object != nil && object.length() > 0
|
48
|
+
return object
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
return nil
|
53
|
+
end
|
54
|
+
end
|