contentstack 0.0.4 → 0.4.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.
@@ -1,17 +1,16 @@
1
1
  require 'contentstack/api'
2
2
  require 'contentstack/content_type'
3
3
  require 'contentstack/asset_collection'
4
-
4
+ require 'contentstack/sync_result'
5
5
  module Contentstack
6
6
  class Client
7
7
  attr_reader :region, :host
8
- # Initialize "Built.io Contentstack" Client instance
9
- def initialize(api_key, access_token, environment, options={})
8
+ # Initialize "Contentstack" Client instance
9
+ def initialize(api_key, delivery_token, environment, options={})
10
10
  @region = options[:region].nil? ? Contentstack::Region::US : options[:region]
11
11
  @host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host]
12
- API.init_api(api_key, access_token, environment, @host)
12
+ API.init_api(api_key, delivery_token, environment, @host)
13
13
  end
14
-
15
14
 
16
15
  def content_types
17
16
  ContentType.all
@@ -29,6 +28,29 @@ module Contentstack
29
28
  Asset.new(uid)
30
29
  end
31
30
 
31
+
32
+ # Syncs your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates
33
+ #
34
+ # Stack.sync({'init': true}) // For initializing sync
35
+ #
36
+ # Stack.sync({'init': true, 'locale': 'en-us'}) //For initializing sync with entries of a specific locale
37
+ #
38
+ # Stack.sync({'init': true, 'start_date': '2018-10-22'}) //For initializing sync with entries published after a specific date
39
+ #
40
+ # Stack.sync({'init': true, 'content_type_uid': 'session'}) //For initializing sync with entries of a specific content type
41
+ #
42
+ # 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'.
43
+ #
44
+ # Stack.sync({'pagination_token': '<btlsomething>'}) // For fetching the next batch of entries using pagination token
45
+ #
46
+ # Stack.sync({'sync_token': '<btlsomething>'}) // For performing subsequent sync after initial sync
47
+ #
48
+ # @param params [Hash] params is an object that supports ‘locale’, ‘start_date’, ‘content_type_uid’, and ‘type’ queries.
49
+ def sync(params)
50
+ sync_result = API.get_sync_items(params)
51
+ SyncResult.new(sync_result)
52
+ end
53
+
32
54
  private
33
55
  def get_default_region_hosts(region='us')
34
56
  case region
@@ -44,7 +44,7 @@ module Contentstack
44
44
  end
45
45
  end
46
46
 
47
- def self.find_by_uid(uid)
47
+ def fetch
48
48
  content_type = API.fetch_content_types(uid)["content_type"]
49
49
  ContentType.new(content_type.inject({}){|clone,(k,v)| clone[k.to_sym] = v; clone})
50
50
  end
@@ -1,13 +1,192 @@
1
+ require 'active_support/core_ext'
2
+
1
3
  module Contentstack
2
4
  class Entry
3
- attr_reader :fields, :content_type, :uid, :owner
5
+ attr_reader :fields, :content_type, :uid, :owner, :query, :schema, :content_type
4
6
  def initialize(attrs, content_type_uid=nil)
5
7
  setup(attrs, content_type_uid)
6
8
  end
7
9
 
10
+ # Get entries from the specified locale.
11
+ #
12
+ # @param [String] code The locale code of the entry
13
+ #
14
+ # Example
15
+ # @entry = @stack.content_type('category').entry(entry_uid)
16
+ # @entry.locale('en-us')
17
+ #
18
+ # @return [Contentstack::Entry]
19
+ def locale(code)
20
+ @query[:locale] = code
21
+ self
22
+ end
23
+
24
+ # Specifies an array of 'only' keys in BASE object that would be 'included' in the response.
25
+ #
26
+ # @param [Array] fields Array of the 'only' reference keys to be included in response.
27
+ # @param [Array] fields_with_base Can be used to denote 'only' fields of the reference class
28
+ #
29
+ # Example
30
+ # # Include only title and description field in response
31
+ # @entry = @stack.content_type('category').entry(entry_uid)
32
+ # @entry.only(['title', 'description'])
33
+ #
34
+ # # Query product and include only the title and description from category reference
35
+ # @entry = @stack.content_type('product').entry(entry_uid)
36
+ # @entry.include_reference('category')
37
+ # .only('category', ['title', 'description'])
38
+ #
39
+ # @return [Contentstack::Entry]
40
+ def only(fields, fields_with_base=nil)
41
+ q = {}
42
+ if [Array, String].include?(fields_with_base.class)
43
+ fields_with_base = [fields_with_base] if fields_with_base.class == String
44
+ q[fields.to_sym] = fields_with_base
45
+ else
46
+ fields = [fields] if fields.class == String
47
+ q = {BASE: fields}
48
+ end
49
+
50
+ @query[:only] = q
51
+ self
52
+ end
53
+
54
+ # Specifies list of field uids that would be 'excluded' from the response.
55
+ #
56
+ # @param [Array] fields Array of field uid which get 'excluded' from the response.
57
+ # @param [Array] fields_with_base Can be used to denote 'except' fields of the reference class
58
+ #
59
+ # Example
60
+ # # Exclude 'description' field in response
61
+ # @entry = @stack.content_type('category').entry(entry_uid)
62
+ # @entry.except(['description'])
63
+ #
64
+ # # Query product and exclude the 'description' from category reference
65
+ # @entry = @stack.content_type('product').entry(entry_uid)
66
+ # @entry.include_reference('category')
67
+ # .except('category', ['description'])
68
+ #
69
+ # @return [Contentstack::Entry]
70
+ def except(fields, fields_with_base=nil)
71
+ q = {}
72
+ if [Array, String].include?(fields_with_base.class)
73
+ fields_with_base = [fields_with_base] if fields_with_base.class == String
74
+ q[fields.to_sym] = fields_with_base
75
+ else
76
+ fields = [fields] if fields.class == String
77
+ q = {BASE: fields}
78
+ end
79
+
80
+ @query[:except] = q
81
+ self
82
+ end
83
+
84
+ # Add a constraint that requires a particular reference key details.
85
+ #
86
+ # @param [String/Array] reference_field_uids Pass string or array of reference fields that must be included in the response
87
+ #
88
+ # Example
89
+ #
90
+ # # Include reference of 'category'
91
+ # @entry = @stack.content_type('product').entry(entry_uid)
92
+ # @entry.include_reference('category')
93
+ #
94
+ # # Include reference of 'category' and 'reviews'
95
+ # @entry = @stack.content_type('product').entry(entry_uid)
96
+ # @entry.include_reference(['category', 'reviews'])
97
+ #
98
+ # @return [Contentstack::Entry]
99
+ def include_reference(reference_field_uids)
100
+ self.include(reference_field_uids)
101
+ end
102
+
103
+ # Include schemas of all returned objects along with objects themselves.
104
+ #
105
+ # Example
106
+ #
107
+ # @entry = @stack.content_type('product').entry(entry_uid)
108
+ # @entry.include_schema
109
+ #
110
+ # @return [Contentstack::Entry]
111
+ def include_schema(flag=true)
112
+ @query[:include_schema] = flag
113
+ self
114
+ end
115
+
116
+ # Include object owner's profile in the objects data.
117
+ #
118
+ # Example
119
+ #
120
+ # @entry = @stack.content_type('product').entry(entry_uid)
121
+ # @entry.include_owner
122
+ #
123
+ # @return [Contentstack::Entry]
124
+ def include_owner(flag=true)
125
+ @query[:include_owner] = flag
126
+ self
127
+ end
128
+
129
+ # Include object's content_type in response
130
+ #
131
+ # Example
132
+ #
133
+ # @entry = @stack.content_type('product').entry(entry_uid)
134
+ # @entry.include_content_type
135
+ #
136
+ # @return [Contentstack::Entry]
137
+ def include_content_type(flag=true)
138
+ @query[:include_content_type] = flag
139
+ self
140
+ end
141
+
142
+ # Include the fallback locale publish content, if specified locale content is not publish.
143
+ #
144
+ # Example
145
+ #
146
+ # @entry = @stack.content_type('product').entry(entry_uid)
147
+ # @entry.include_fallback
148
+ #
149
+ # @return [Contentstack::Entry]
150
+ def include_fallback(flag=true)
151
+ @query[:include_fallback] = flag
152
+ self
153
+ end
154
+
155
+ # Include Embedded Objects (Entries and Assets) along with entry/entries details.
156
+ #
157
+ # Example
158
+ #
159
+ # @entry = @stack.content_type('product').entry(entry_uid)
160
+ # @entry.include_embedded_items
161
+ #
162
+ # @return [Contentstack::Query]
163
+ def include_embedded_items()
164
+ @query[:include_embedded_items] = ['BASE']
165
+ self
166
+ end
167
+
168
+ #
169
+ # @return [Contentstack::Query]
170
+ def include(field_uids)
171
+ field_uids = [field_uids] if field_uids.class == String
172
+ @query[:include] ||= []
173
+ @query[:include] = @query[:include] | field_uids
174
+ self
175
+ end
176
+
177
+ # Execute entry
178
+ #
179
+ # Example
180
+ #
181
+ # @entry = @stack.content_type('product').entry(entry_uid)
182
+ # @entry.fetch
183
+ #
184
+ # @return [Contentstack::EntryCollection]
8
185
  def fetch
9
- entry = API.fetch_entry(@content_type, self.fields[:uid])
186
+ entry = API.fetch_entry(@content_type, self.fields[:uid], @query)
10
187
  setup(entry["entry"])
188
+ @schema = entry["schema"].symbolize_keys if entry["schema"]
189
+ @content_type = entry["content_type"].symbolize_keys if entry["content_type"]
11
190
  self
12
191
  end
13
192
 
@@ -22,6 +201,7 @@ module Contentstack
22
201
  @content_type = content_type_uid if !content_type_uid.blank?
23
202
  @owner = attrs[:_owner] if attrs[:_owner]
24
203
  @uid = attrs[:uid]
204
+ @query = {}
25
205
  end
26
206
  end
27
207
  end
@@ -502,6 +502,33 @@ module Contentstack
502
502
  self
503
503
  end
504
504
 
505
+
506
+ # Include the fallback locale publish content, if specified locale content is not publish.
507
+ #
508
+ # Example
509
+ #
510
+ # @query = @stack.content_type('product').query
511
+ # @query.include_fallback
512
+ #
513
+ # @return [Contentstack::Query]
514
+ def include_fallback(flag=true)
515
+ @query[:include_fallback] = flag
516
+ self
517
+ end
518
+
519
+ # Include Embedded Objects (Entries and Assets) along with entry/entries details.
520
+ #
521
+ # Example
522
+ #
523
+ # @query = @stack.content_type('product').query
524
+ # @query.include_embedded_items
525
+ #
526
+ # @return [Contentstack::Query]
527
+ def include_embedded_items()
528
+ @query[:include_embedded_items] = ['BASE']
529
+ self
530
+ end
531
+
505
532
  # Include objects in 'Draft' mode in response
506
533
  #
507
534
  # Example
@@ -0,0 +1,30 @@
1
+ require 'active_support/core_ext'
2
+
3
+ module Contentstack
4
+ class SyncResult
5
+ attr_reader :items
6
+ attr_reader :sync_token
7
+ attr_reader :pagination_token
8
+ attr_reader :total_count
9
+ attr_reader :skip
10
+ attr_reader :limit
11
+
12
+ def initialize(sync_result)
13
+ if sync_result.nil?
14
+ @items = []
15
+ @sync_token = nil
16
+ @pagination_token = nil
17
+ @total_count = 0
18
+ @skip = 0
19
+ @limit = 100
20
+ else
21
+ @items = sync_result["items"] || []
22
+ @sync_token = sync_result["sync_token"] || nil
23
+ @pagination_token = sync_result["pagination_token"] || nil
24
+ @total_count = sync_result["total_count"] || 0
25
+ @skip = sync_result["skip"] || 0
26
+ @limit = sync_result["limit"] || 100
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module Contentstack
2
- VERSION = "0.0.4"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -3,8 +3,8 @@ require_relative '../lib/contentstack.rb'
3
3
 
4
4
  describe Contentstack::ContentType do
5
5
  let(:client) { create_client }
6
- let(:eu_client) { create_client('ACCESS_TOKEN', 'API_KEY', 'STACK_ENV', {region: Contentstack::Region::EU}) }
7
- let(:custom_host_client) { create_client('ACCESS_TOKEN', 'API_KEY', 'STACK_ENV', {host: "https://custom-cdn.contentstack.com"}) }
6
+ let(:eu_client) { create_client('DELIVERY_TOKEN', 'API_KEY', 'STACK_ENV', {region: Contentstack::Region::EU}) }
7
+ let(:custom_host_client) { create_client('DELIVERY_TOKEN', 'API_KEY', 'STACK_ENV', {host: "https://custom-cdn.contentstack.com"}) }
8
8
 
9
9
  describe "Fetch data from API" do
10
10
  it "has class as Contentstack::ContentType" do
@@ -46,6 +46,11 @@ describe Contentstack::ContentType do
46
46
  @data = client.content_types.first
47
47
  expect(@data.attributes).not_to be nil
48
48
  end
49
+
50
+ it "Should get content type from uid" do
51
+ @data = client.content_type("category").fetch
52
+ expect(@data.attributes).not_to be nil
53
+ end
49
54
  end
50
55
 
51
56
  describe "Initialized using class" do
@@ -3,8 +3,8 @@ require_relative '../lib/contentstack.rb'
3
3
 
4
4
  describe Contentstack do
5
5
  let(:client) { create_client }
6
- let(:eu_client) { create_client('ACCESS_TOKEN', 'API_KEY', 'STACK_ENV', {region: Contentstack::Region::EU}) }
7
- let(:custom_host_client) { create_client('ACCESS_TOKEN', 'API_KEY', 'STACK_ENV', {host: "https://custom-cdn.contentstack.com"}) }
6
+ let(:eu_client) { create_client('DELIVERY_TOKEN_TOKEN', 'API_KEY', 'STACK_ENV', {region: Contentstack::Region::EU}) }
7
+ let(:custom_host_client) { create_client('DELIVERY_TOKEN_TOKEN', 'API_KEY', 'STACK_ENV', {host: "https://custom-cdn.contentstack.com"}) }
8
8
 
9
9
  it "has a version number" do
10
10
  expect(Contentstack::VERSION).not_to be nil
data/spec/entry_spec.rb CHANGED
@@ -3,6 +3,9 @@ require_relative '../lib/contentstack.rb'
3
3
 
4
4
  describe Contentstack::Entry do
5
5
  let(:client) { create_client }
6
+ let(:uid) { "blt05056a2f5e0ebf76" }
7
+ let(:category) {client.content_type("category").entry(uid)}
8
+ let(:product) {client.content_type("product").entry(uid)}
6
9
 
7
10
  it "Contentstack::EntryCollection should have Contentstack::Entry instance" do
8
11
  data = client.content_type("category").query.fetch
@@ -11,13 +14,81 @@ describe Contentstack::Entry do
11
14
  end
12
15
 
13
16
  it "is an instance of Contentstack::Entry if single entry is fetched" do
14
- data = client.content_type("category").entry("blt05056a2f5e0ebf76").fetch
17
+ data = category.fetch
15
18
  expect(data.class).to eq Contentstack::Entry
16
19
  end
17
20
 
18
21
  it 'has a method `get` to get attributes data' do
19
- uid = "blt05056a2f5e0ebf76"
20
- data = client.content_type("category").entry(uid).fetch
22
+ data = category.fetch
21
23
  expect(data.get('uid')).to eq uid
22
24
  end
25
+
26
+ it "should set locale the in the request entry" do
27
+ data = category.locale('en-us')
28
+ expect(data.query[:locale]).to eq 'en-us'
29
+ end
30
+
31
+ it "should get data using `only` method with string parameter" do
32
+ data = category.fetch
33
+ expect(data.fields[:title]).not_to be nil
34
+ expect(data.fields[:uid]).not_to be nil
35
+ end
36
+
37
+ it "should get data using `only` method with array parameter" do
38
+ data = category.only(["title"]).fetch
39
+ expect(data.fields[:title]).not_to be nil
40
+ expect(data.fields[:uid]).not_to be nil
41
+ end
42
+
43
+ it "should get data using `except` method with string parameter" do
44
+ data = category.except("category_tags").fetch
45
+ expect(data.fields[:category_tags]).to be nil
46
+ end
47
+
48
+ it "should get data using `except` method with array parameter" do
49
+ data = category.except(["description"]).fetch
50
+ expect(data.fields[:description]).to be nil
51
+ end
52
+
53
+ it "should get data using `only` method for reference fields" do
54
+ data = product.include_reference('categories').only("categories", ["title", "description"]).fetch
55
+ expect(data.fields[:categories][0][:title]).to eq "Smartphones"
56
+ end
57
+
58
+ it "should get data using `except` method for reference fields" do
59
+ data = product.include_reference('categories').except("categories", "title").fetch
60
+ expect(data.fields[:categories][0][:title]).to eq 'Smartphones'
61
+ end
62
+
63
+ it "should get data using `include_schema` method" do
64
+ data = category.include_schema.fetch
65
+ expect(data.schema).not_to be nil
66
+ end
67
+
68
+ it "should get data using `include_owner` method" do
69
+ data = product.include_owner.fetch
70
+ expect(data.fields[:_owner]).not_to be nil
71
+ end
72
+
73
+ it "should get data using `include_owner` method" do
74
+ data = product.include_fallback.fetch
75
+ expect(data.fields[:locale]).not_to be nil
76
+ end
77
+
78
+ it "should get data using `include_content_type` method" do
79
+ data = category.include_content_type.fetch
80
+ expect(data.content_type).not_to be nil
81
+ end
82
+
83
+ it "should get data using `include_reference` method" do
84
+ data = product.include_reference('categories').fetch
85
+ puts data.get("categories.title")
86
+ expect(data.fields[:categories][0][:title]).not_to be nil
87
+ end
88
+
89
+ it "should get data using `include_embedded_items` method" do
90
+ data = product.include_embedded_items().fetch
91
+ puts data.get("categories.title")
92
+ expect(data.fields[:categories][0][:title]).not_to be nil
93
+ end
23
94
  end