contentstack_utils 1.1.3.2 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,97 +1,97 @@
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
- ```
89
- ### GQL Json RTE to HTML
90
- To parse JSON RTE content from GQL response to HTML content use `ContentstackUtils::GQL.json_to_html` function as below:
91
-
92
- ```ruby
93
- require 'contentstack_utils'
94
-
95
- result = ContentstackUtils::GQL.json_to_html(entry['single_rte'], ContentstackUtils::Model::Options.new())
96
-
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
+ ```
89
+ ### GQL Json RTE to HTML
90
+ To parse JSON RTE content from GQL response to HTML content use `ContentstackUtils::GQL.json_to_html` function as below:
91
+
92
+ ```ruby
93
+ require 'contentstack_utils'
94
+
95
+ result = ContentstackUtils::GQL.json_to_html(entry['single_rte'], ContentstackUtils::Model::Options.new())
96
+
97
97
  ```
data/Rakefile CHANGED
@@ -1,33 +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
-
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
33
  task default: :spec
data/SECURITY.md CHANGED
@@ -1,27 +1,27 @@
1
- ## Security
2
-
3
- Contentstack takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations.
4
-
5
- If you believe you have found a security vulnerability in any Contentstack-owned repository, please report it to us as described below.
6
-
7
- ## Reporting Security Issues
8
-
9
- **Please do not report security vulnerabilities through public GitHub issues.**
10
-
11
- Send email to [security@contentstack.com](mailto:security@contentstack.com).
12
-
13
- You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message.
14
-
15
- Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
16
-
17
- * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
18
- * Full paths of source file(s) related to the manifestation of the issue
19
- * The location of the affected source code (tag/branch/commit or direct URL)
20
- * Any special configuration required to reproduce the issue
21
- * Step-by-step instructions to reproduce the issue
22
- * Proof-of-concept or exploit code (if possible)
23
- * Impact of the issue, including how an attacker might exploit the issue
24
-
25
- This information will help us triage your report more quickly.
26
-
27
- [https://www.contentstack.com/trust/](https://www.contentstack.com/trust/)
1
+ ## Security
2
+
3
+ Contentstack takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations.
4
+
5
+ If you believe you have found a security vulnerability in any Contentstack-owned repository, please report it to us as described below.
6
+
7
+ ## Reporting Security Issues
8
+
9
+ **Please do not report security vulnerabilities through public GitHub issues.**
10
+
11
+ Send email to [security@contentstack.com](mailto:security@contentstack.com).
12
+
13
+ You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message.
14
+
15
+ Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
16
+
17
+ * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
18
+ * Full paths of source file(s) related to the manifestation of the issue
19
+ * The location of the affected source code (tag/branch/commit or direct URL)
20
+ * Any special configuration required to reproduce the issue
21
+ * Step-by-step instructions to reproduce the issue
22
+ * Proof-of-concept or exploit code (if possible)
23
+ * Impact of the issue, including how an attacker might exploit the issue
24
+
25
+ This information will help us triage your report more quickly.
26
+
27
+ [https://www.contentstack.com/trust/](https://www.contentstack.com/trust/)
@@ -1,32 +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', '>= 3.2'
25
- s.add_dependency 'nokogiri', '~> 1.11'
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
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 = '>= 3.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', '>= 7.0'
25
+ s.add_dependency 'nokogiri', '>= 1.11'
26
+
27
+ s.add_development_dependency 'rake', '~> 13.0'
28
+ s.add_development_dependency 'rspec', '~> 3.13'
29
+ s.add_development_dependency 'webmock', '~> 3.23'
30
+ s.add_development_dependency 'simplecov', '~> 0.22'
31
+ s.add_development_dependency 'yard', '~> 0.9.36'
32
+ end
@@ -1,9 +1,9 @@
1
- module ContentstackUtils
2
- module Interface
3
- class Rendarable
4
- def render_option(embeddedObject, metadata)
5
- raise NotImplementedError, "Implement this method in a child class"
6
- end
7
- end
8
- end
1
+ module ContentstackUtils
2
+ module Interface
3
+ class Rendarable
4
+ def render_option(embeddedObject, metadata)
5
+ raise NotImplementedError, "Implement this method in a child class"
6
+ end
7
+ end
8
+ end
9
9
  end
@@ -1,69 +1,69 @@
1
-
2
- module ContentstackUtils
3
- module Model
4
- class Metadata
5
- def initialize( element )
6
- if element.instance_of? Nokogiri::XML::Element
7
- @itemType = element.attribute('type') ? element.attribute('type').value : nil
8
- @styleType = element.attribute('sys-style-type') ? element.attribute('sys-style-type').value : nil
9
- @itemUid ||= (element.attribute('data-sys-entry-uid') ? element.attribute('data-sys-entry-uid').value : nil) || (element.attribute('data-sys-asset-uid') ? element.attribute('data-sys-asset-uid').value : nil)
10
- @contentTypeUid = element.attribute('data-sys-content-type-uid') ? element.attribute('data-sys-content-type-uid').value : nil
11
- @text = element.text
12
- @element = element
13
- @attributes = element
14
- else
15
- @itemType = element["attrs"]['type']
16
- @styleType = element["attrs"]['display-type']
17
- @itemUid ||= element["attrs"]['entry-uid'] || element["attrs"]['asset-uid']
18
- @contentTypeUid = element["attrs"]['content-type-uid']
19
- if element["children"] && element["children"].length() > 0
20
- child = element["children"]
21
- for item in child do
22
- if item["type"] == nil && item["text"]
23
- @text = item["text"]
24
- end
25
- end
26
- end
27
- @element = element
28
- @attributes = element["attrs"]
29
- end
30
- end
31
-
32
- def item_type
33
- @itemType ? @itemType : nil
34
- end
35
-
36
- def style_type
37
- @styleType ? @styleType : nil
38
- end
39
-
40
- def item_uid
41
- @itemUid ? @itemUid : nil
42
- end
43
-
44
- def content_type_uid
45
- @contentTypeUid ? @contentTypeUid : nil
46
- end
47
-
48
- def text
49
- @text
50
- end
51
-
52
- def element
53
- @element
54
- end
55
-
56
- def attributes
57
- @attributes
58
- end
59
-
60
- def get_attribute_value(string)
61
- if @attributes.instance_of? Nokogiri::XML::Element
62
- @attributes.attribute(string) ? @attributes.attribute(string).value : nil
63
- else
64
- @attributes[string]
65
- end
66
- end
67
- end
68
- end
1
+
2
+ module ContentstackUtils
3
+ module Model
4
+ class Metadata
5
+ def initialize( element )
6
+ if element.instance_of? Nokogiri::XML::Element
7
+ @itemType = element.attribute('type') ? element.attribute('type').value : nil
8
+ @styleType = element.attribute('sys-style-type') ? element.attribute('sys-style-type').value : nil
9
+ @itemUid ||= (element.attribute('data-sys-entry-uid') ? element.attribute('data-sys-entry-uid').value : nil) || (element.attribute('data-sys-asset-uid') ? element.attribute('data-sys-asset-uid').value : nil)
10
+ @contentTypeUid = element.attribute('data-sys-content-type-uid') ? element.attribute('data-sys-content-type-uid').value : nil
11
+ @text = element.text
12
+ @element = element
13
+ @attributes = element
14
+ else
15
+ @itemType = element["attrs"]['type']
16
+ @styleType = element["attrs"]['display-type']
17
+ @itemUid ||= element["attrs"]['entry-uid'] || element["attrs"]['asset-uid']
18
+ @contentTypeUid = element["attrs"]['content-type-uid']
19
+ if element["children"] && element["children"].length() > 0
20
+ child = element["children"]
21
+ for item in child do
22
+ if item["type"] == nil && item["text"]
23
+ @text = item["text"]
24
+ end
25
+ end
26
+ end
27
+ @element = element
28
+ @attributes = element["attrs"]
29
+ end
30
+ end
31
+
32
+ def item_type
33
+ @itemType ? @itemType : nil
34
+ end
35
+
36
+ def style_type
37
+ @styleType ? @styleType : nil
38
+ end
39
+
40
+ def item_uid
41
+ @itemUid ? @itemUid : nil
42
+ end
43
+
44
+ def content_type_uid
45
+ @contentTypeUid ? @contentTypeUid : nil
46
+ end
47
+
48
+ def text
49
+ @text
50
+ end
51
+
52
+ def element
53
+ @element
54
+ end
55
+
56
+ def attributes
57
+ @attributes
58
+ end
59
+
60
+ def get_attribute_value(string)
61
+ if @attributes.instance_of? Nokogiri::XML::Element
62
+ @attributes.attribute(string) ? @attributes.attribute(string).value : nil
63
+ else
64
+ @attributes[string]
65
+ end
66
+ end
67
+ end
68
+ end
69
69
  end