contentstack 0.6.3 → 0.7.0

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.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/check-branch.yml +20 -0
  3. data/.github/workflows/codeql-analysis.yml +68 -68
  4. data/.github/workflows/jira.yml +28 -28
  5. data/.github/workflows/release-gem.yml +30 -30
  6. data/.github/workflows/sast-scan.yml +10 -10
  7. data/.github/workflows/sca-scan.yml +15 -15
  8. data/.github/workflows/secrets-scan.yml +10 -10
  9. data/.gitignore +11 -11
  10. data/.yardopts +6 -6
  11. data/CHANGELOG.md +129 -114
  12. data/CODEOWNERS +1 -1
  13. data/CODE_OF_CONDUCT.md +73 -73
  14. data/Gemfile +2 -2
  15. data/Gemfile.lock +79 -76
  16. data/LICENSE.txt +21 -21
  17. data/README.md +197 -197
  18. data/SECURITY.md +27 -27
  19. data/contentstack.gemspec +29 -29
  20. data/lib/contentstack/api.rb +191 -191
  21. data/lib/contentstack/asset.rb +68 -68
  22. data/lib/contentstack/asset_collection.rb +27 -27
  23. data/lib/contentstack/client.rb +122 -91
  24. data/lib/contentstack/content_type.rb +53 -53
  25. data/lib/contentstack/entry.rb +235 -221
  26. data/lib/contentstack/entry_collection.rb +44 -44
  27. data/lib/contentstack/error.rb +6 -6
  28. data/lib/contentstack/query.rb +665 -653
  29. data/lib/contentstack/region.rb +13 -5
  30. data/lib/contentstack/sync_result.rb +29 -29
  31. data/lib/contentstack/version.rb +2 -2
  32. data/lib/contentstack.rb +31 -31
  33. data/lib/util.rb +110 -110
  34. data/rakefile.rb +3 -3
  35. data/spec/asset_collection_spec.rb +15 -15
  36. data/spec/asset_spec.rb +47 -47
  37. data/spec/content_type_spec.rb +80 -80
  38. data/spec/contentstack_spec.rb +63 -38
  39. data/spec/entry_collection_spec.rb +41 -41
  40. data/spec/entry_spec.rb +116 -101
  41. data/spec/fixtures/asset.json +1 -1
  42. data/spec/fixtures/asset_collection.json +1 -1
  43. data/spec/fixtures/category_content_type.json +1 -1
  44. data/spec/fixtures/category_entry.json +1 -1
  45. data/spec/fixtures/category_entry_collection.json +1 -1
  46. data/spec/fixtures/category_entry_collection_without_count.json +1 -1
  47. data/spec/fixtures/content_types.json +1 -1
  48. data/spec/fixtures/product_entry.json +1 -1
  49. data/spec/fixtures/product_entry_collection.json +1 -1
  50. data/spec/fixtures/sync_init.json +2974 -2974
  51. data/spec/query_spec.rb +210 -205
  52. data/spec/spec_helper.rb +180 -180
  53. data/spec/sync_spec.rb +26 -26
  54. metadata +6 -5
@@ -1,92 +1,123 @@
1
- require 'contentstack/api'
2
- require 'contentstack/content_type'
3
- require 'contentstack/asset_collection'
4
- require 'contentstack/sync_result'
5
- require 'util'
6
- require 'contentstack/error'
7
- module Contentstack
8
- class Client
9
- using Utility
10
- attr_reader :region, :host
11
- # Initialize "Contentstack" Client instance
12
- def initialize(api_key, delivery_token, environment, options={})
13
- raise Contentstack::Error.new("Api Key is not valid") if api_key.class != String
14
- raise Contentstack::Error.new("Api Key Field Should not be Empty") if api_key.empty?
15
- raise Contentstack::Error.new("Delivery Token is not valid") if delivery_token.class != String
16
- raise Contentstack::Error.new("Delivery Token Field Should not be Empty") if delivery_token.empty?
17
- raise Contentstack::Error.new("Envirnoment Field is not valid") if environment.class != String
18
- raise Contentstack::Error.new("Envirnoment Field Should not be Empty") if environment.empty?
19
- @region = options[:region].nil? ? Contentstack::Region::US : options[:region]
20
- @host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host]
21
- @live_preview = !options.key?(:live_preview) ? {} : options[:live_preview]
22
- @branch = options[:branch].nil? ? "" : options[:branch]
23
- @proxy_details = options[:proxy].nil? ? "" : options[:proxy]
24
- @timeout = options[:timeout].nil? ? 3000 : options[:timeout]
25
- @retryDelay = options[:retryDelay].nil? ? 3000 : options[:retryDelay]
26
- @retryLimit = options[:retryLimit].nil? ? 5 : options[:retryLimit]
27
- @errorRetry = options[:errorRetry].nil? ? [408, 429] : options[:errorRetry]
28
- retry_options = {
29
- "timeout" => @timeout.to_s,
30
- "retryDelay"=> @retryDelay,
31
- "retryLimit"=> @retryLimit,
32
- "errorRetry" => @errorRetry
33
- }
34
- raise Contentstack::Error.new("Proxy URL Should not be Empty") if @proxy_details.present? && @proxy_details[:url].empty?
35
- raise Contentstack::Error.new("Proxy Port Should not be Empty") if @proxy_details.present? && @proxy_details[:port].empty?
36
- API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview, @proxy_details, retry_options)
37
- end
38
-
39
- def content_types
40
- ContentType.all
41
- end
42
-
43
- def content_type(uid)
44
- ContentType.new({uid: uid})
45
- end
46
-
47
- def assets
48
- AssetCollection.new
49
- end
50
-
51
- def asset(uid)
52
- Asset.new(uid)
53
- end
54
-
55
- def live_preview_query(query={})
56
- API.live_preview_query(query)
57
- end
58
-
59
- # Syncs your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates
60
- #
61
- # Stack.sync({'init': true}) // For initializing sync
62
- #
63
- # Stack.sync({'init': true, 'locale': 'en-us'}) //For initializing sync with entries of a specific locale
64
- #
65
- # Stack.sync({'init': true, 'start_date': '2018-10-22'}) //For initializing sync with entries published after a specific date
66
- #
67
- # Stack.sync({'init': true, 'content_type_uid': 'session'}) //For initializing sync with entries of a specific content type
68
- #
69
- # Stack.sync({'init': true, 'type': 'entry_published'}) // Use the type parameter to get a specific type of content.Supports 'asset_published', 'entry_published', 'asset_unpublished', 'entry_unpublished', 'asset_deleted', 'entry_deleted', 'content_type_deleted'.
70
- #
71
- # Stack.sync({'pagination_token': '<pagination>'}) // For fetching the next batch of entries using pagination token
72
- #
73
- # Stack.sync({'sync_token': '<sync>'}) // For performing subsequent sync after initial sync
74
- #
75
- # @param params [Hash] params is an object that supports ‘locale’, ‘start_date’, ‘content_type_uid’, and ‘type’ queries.
76
- def sync(params)
77
- sync_result = API.get_sync_items(params)
78
- SyncResult.new(sync_result)
79
- end
80
-
81
- private
82
- def get_default_region_hosts(region='us')
83
- case region
84
- when "us"
85
- host = "https://cdn.contentstack.io"
86
- when "eu"
87
- host = "https://eu-cdn.contentstack.com"
88
- end
89
- host
90
- end
91
- end
1
+ require 'contentstack/api'
2
+ require 'contentstack/content_type'
3
+ require 'contentstack/asset_collection'
4
+ require 'contentstack/sync_result'
5
+ require 'util'
6
+ require 'contentstack/error'
7
+ module Contentstack
8
+ class Client
9
+ using Utility
10
+ attr_reader :region, :host
11
+ # Initialize "Contentstack" Client instance
12
+ def initialize(api_key, delivery_token, environment, options={})
13
+ raise Contentstack::Error.new("Api Key is not valid") if api_key.class != String
14
+ raise Contentstack::Error.new("Api Key Field Should not be Empty") if api_key.empty?
15
+ raise Contentstack::Error.new("Delivery Token is not valid") if delivery_token.class != String
16
+ raise Contentstack::Error.new("Delivery Token Field Should not be Empty") if delivery_token.empty?
17
+ raise Contentstack::Error.new("Envirnoment Field is not valid") if environment.class != String
18
+ raise Contentstack::Error.new("Envirnoment Field Should not be Empty") if environment.empty?
19
+ @region = options[:region].nil? ? Contentstack::Region::US : options[:region]
20
+ # @host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host] #removed for not supporting custom host with regions
21
+ @host = get_host_by_region(@region, options) # Added new method for custom host support with different regions
22
+ @live_preview = !options.key?(:live_preview) ? {} : options[:live_preview]
23
+ @branch = options[:branch].nil? ? "" : options[:branch]
24
+ @proxy_details = options[:proxy].nil? ? "" : options[:proxy]
25
+ @timeout = options[:timeout].nil? ? 3000 : options[:timeout]
26
+ @retryDelay = options[:retryDelay].nil? ? 3000 : options[:retryDelay]
27
+ @retryLimit = options[:retryLimit].nil? ? 5 : options[:retryLimit]
28
+ @errorRetry = options[:errorRetry].nil? ? [408, 429] : options[:errorRetry]
29
+ retry_options = {
30
+ "timeout" => @timeout.to_s,
31
+ "retryDelay"=> @retryDelay,
32
+ "retryLimit"=> @retryLimit,
33
+ "errorRetry" => @errorRetry
34
+ }
35
+ raise Contentstack::Error.new("Proxy URL Should not be Empty") if @proxy_details.present? && @proxy_details[:url].empty?
36
+ raise Contentstack::Error.new("Proxy Port Should not be Empty") if @proxy_details.present? && @proxy_details[:port].empty?
37
+ API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview, @proxy_details, retry_options)
38
+ end
39
+
40
+ def content_types
41
+ ContentType.all
42
+ end
43
+
44
+ def content_type(uid)
45
+ ContentType.new({uid: uid})
46
+ end
47
+
48
+ def assets
49
+ AssetCollection.new
50
+ end
51
+
52
+ def asset(uid)
53
+ Asset.new(uid)
54
+ end
55
+
56
+ def live_preview_query(query={})
57
+ API.live_preview_query(query)
58
+ end
59
+
60
+ # Syncs your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates
61
+ #
62
+ # Stack.sync({'init': true}) // For initializing sync
63
+ #
64
+ # Stack.sync({'init': true, 'locale': 'en-us'}) //For initializing sync with entries of a specific locale
65
+ #
66
+ # Stack.sync({'init': true, 'start_date': '2018-10-22'}) //For initializing sync with entries published after a specific date
67
+ #
68
+ # Stack.sync({'init': true, 'content_type_uid': 'session'}) //For initializing sync with entries of a specific content type
69
+ #
70
+ # Stack.sync({'init': true, 'type': 'entry_published'}) // Use the type parameter to get a specific type of content.Supports 'asset_published', 'entry_published', 'asset_unpublished', 'entry_unpublished', 'asset_deleted', 'entry_deleted', 'content_type_deleted'.
71
+ #
72
+ # Stack.sync({'pagination_token': '<pagination>'}) // For fetching the next batch of entries using pagination token
73
+ #
74
+ # Stack.sync({'sync_token': '<sync>'}) // For performing subsequent sync after initial sync
75
+ #
76
+ # @param params [Hash] params is an object that supports ‘locale’, ‘start_date’, ‘content_type_uid’, and ‘type’ queries.
77
+ def sync(params)
78
+ sync_result = API.get_sync_items(params)
79
+ SyncResult.new(sync_result)
80
+ end
81
+
82
+ private
83
+ def get_default_region_hosts(region='us')
84
+ host = "#{Contentstack::Host::PROTOCOL}#{Contentstack::Host::DEFAULT_HOST}" #set default host if region is nil
85
+ case region
86
+ when "us"
87
+ host = "#{Contentstack::Host::PROTOCOL}#{Contentstack::Host::DEFAULT_HOST}"
88
+ when "eu"
89
+ host = "#{Contentstack::Host::PROTOCOL}eu-cdn.#{Contentstack::Host::HOST}"
90
+ when "azure-na"
91
+ host = "#{Contentstack::Host::PROTOCOL}azure-na-cdn.#{Contentstack::Host::HOST}"
92
+ when "azure-eu"
93
+ host = "#{Contentstack::Host::PROTOCOL}azure-eu-cdn.#{Contentstack::Host::HOST}"
94
+ end
95
+ host
96
+ end
97
+
98
+ def get_host_by_region(region, options)
99
+ if options[:host].nil? && region.present?
100
+ host = get_default_region_hosts(region)
101
+ elsif options[:host].present? && region.present?
102
+ custom_host = options[:host]
103
+ case region
104
+ when "us"
105
+ host = "#{Contentstack::Host::PROTOCOL}cdn.#{custom_host}"
106
+ when "eu"
107
+ host = "#{Contentstack::Host::PROTOCOL}eu-cdn.#{custom_host}"
108
+ when "azure-na"
109
+ host = "#{Contentstack::Host::PROTOCOL}azure-na-cdn.#{custom_host}"
110
+ when "azure-eu"
111
+ host = "#{Contentstack::Host::PROTOCOL}azure-eu-cdn.#{custom_host}"
112
+ end
113
+ elsif options[:host].present? && region.empty?
114
+ custom_host = options[:host]
115
+ host = "#{Contentstack::Host::PROTOCOL}cdn.#{custom_host}"
116
+ else
117
+ host = "#{Contentstack::Host::PROTOCOL}#{Contentstack::Host::DEFAULT_HOST}" #set default host if region and host is empty
118
+ end
119
+ host
120
+ end
121
+
122
+ end
92
123
  end
@@ -1,54 +1,54 @@
1
- require 'contentstack/query'
2
- require 'util'
3
-
4
- module Contentstack
5
- class ContentType
6
- using Utility
7
- [:title, :uid, :created_at, :updated_at, :attributes].each do |method_name|
8
- if [:created_at, :updated_at].include?(method_name)
9
- define_method method_name do
10
- return Time.parse(@attributes[method_name]) if @attributes[method_name] && !@attributes[method_name].nil?
11
- end
12
- elsif :attributes == method_name
13
- define_method :attributes do
14
- {
15
- title: self.title,
16
- uid: self.uid,
17
- created_at: self.created_at,
18
- updated_at: self.updated_at,
19
- schema: @attributes[:schema]
20
- }
21
- end
22
- else
23
- define_method method_name do
24
- return @attributes[method_name]
25
- end
26
- end
27
- end
28
-
29
- def initialize(object)
30
- @attributes = object.symbolize_keys
31
- end
32
-
33
- def query
34
- Query.new(self.uid)
35
- end
36
-
37
- def entry(entry_uid)
38
- Entry.new({uid: entry_uid}, self.uid)
39
- end
40
-
41
-
42
- def self.all
43
- content_types = API.fetch_content_types["content_types"]
44
- content_types.map do |content_type|
45
- ContentType.new(content_type.inject({}){|clone,(k,v)| clone[k.to_sym] = v; clone})
46
- end
47
- end
48
-
49
- def fetch
50
- content_type = API.fetch_content_types(uid)["content_type"]
51
- ContentType.new(content_type.inject({}){|clone,(k,v)| clone[k.to_sym] = v; clone})
52
- end
53
- end
1
+ require 'contentstack/query'
2
+ require 'util'
3
+
4
+ module Contentstack
5
+ class ContentType
6
+ using Utility
7
+ [:title, :uid, :created_at, :updated_at, :attributes].each do |method_name|
8
+ if [:created_at, :updated_at].include?(method_name)
9
+ define_method method_name do
10
+ return Time.parse(@attributes[method_name]) if @attributes[method_name] && !@attributes[method_name].nil?
11
+ end
12
+ elsif :attributes == method_name
13
+ define_method :attributes do
14
+ {
15
+ title: self.title,
16
+ uid: self.uid,
17
+ created_at: self.created_at,
18
+ updated_at: self.updated_at,
19
+ schema: @attributes[:schema]
20
+ }
21
+ end
22
+ else
23
+ define_method method_name do
24
+ return @attributes[method_name]
25
+ end
26
+ end
27
+ end
28
+
29
+ def initialize(object)
30
+ @attributes = object.symbolize_keys
31
+ end
32
+
33
+ def query
34
+ Query.new(self.uid)
35
+ end
36
+
37
+ def entry(entry_uid)
38
+ Entry.new({uid: entry_uid}, self.uid)
39
+ end
40
+
41
+
42
+ def self.all
43
+ content_types = API.fetch_content_types["content_types"]
44
+ content_types.map do |content_type|
45
+ ContentType.new(content_type.inject({}){|clone,(k,v)| clone[k.to_sym] = v; clone})
46
+ end
47
+ end
48
+
49
+ def fetch
50
+ content_type = API.fetch_content_types(uid)["content_type"]
51
+ ContentType.new(content_type.inject({}){|clone,(k,v)| clone[k.to_sym] = v; clone})
52
+ end
53
+ end
54
54
  end