contentstack 0.0.3 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 1ba9cac23e4e20c7af32bb25d788cd226f489bb9
4
- data.tar.gz: '0903ec191fca15f8664db5c9de793453657b3636'
2
+ SHA256:
3
+ metadata.gz: ed2009d779c953b80883a4d8c8d267cca918c3a784450513643f308ae79645c1
4
+ data.tar.gz: 4a48fc25a61c332e8f338941828b87836db709eb0c7f819b5e926f1d264ae5d7
5
5
  SHA512:
6
- metadata.gz: '068954e3aac1861e09fe135fbcea77ac2fd528e8e295285bed5286026a1202fbd10f78f9418a7c1c5a8af14f8a9bfa107a7568567b9cb907c09374dae93415c3'
7
- data.tar.gz: b38b4b0320cf45944edd04c6c0aed62578d9b029141f1b159f0dcf51e778a852623a4e06b270f11fdf5dd21613cdc41386614102c987426da2f9e02c2a202f1b
6
+ metadata.gz: 1c74b89fabfe984e5d068cc6a1c1dcf40b8d098730959677572e71f7f6c0f989bd82d131b87f23f66f83a80c73104197fa008d7ba3685baaf491f2fe214f829a
7
+ data.tar.gz: e3d42fd8f4fc573a08dae949fe85d0576a7bc9290e724941468439f8afe4093f979b7f992c0572667e0942fe7a9c262ca8b3c42d13f5a7d8c762c9e02695f39a
data/.gitignore CHANGED
@@ -5,3 +5,7 @@ doc
5
5
  spec-integration
6
6
  coverage
7
7
  \.yardoc
8
+ .DS_Store
9
+ .bundle/
10
+ */rspec_results.html
11
+ vendor/
data/CHANGELOG.md ADDED
@@ -0,0 +1,54 @@
1
+ ## CHANGELOG
2
+ ------------------------------------------------
3
+
4
+ ## Version 0.3.0
5
+ ### Date: 17th-Mar-2021
6
+ ### New Features
7
+ - Sync API module support added
8
+
9
+ ------------------------------------------------
10
+ ## Version 0.2.0
11
+
12
+ ### New Features
13
+ - Entry
14
+ - locale - function for passing locale is added
15
+ - only - function for getting only specified field
16
+ - except - function for getting field except specified field
17
+ - include_reference - function for including reference in entry
18
+ - include_schema - function for including schema along with entry added
19
+ - include_content_type - function for including content type details along with entry added
20
+ - include_owner - function for getting owner of entry
21
+ - include_fallback - function for getting published fallback locale content, if specified locale content is not published
22
+
23
+ - Query
24
+ - include_fallback - function for getting published fallback locale content, if specified locale content is not published
25
+
26
+
27
+ ------------------------------------------------
28
+
29
+ ## Version 0.1.0
30
+
31
+ ### Bug
32
+ - Stack Tokens Moved to Headers
33
+
34
+ ------------------------------------------------
35
+
36
+ ## Version 0.0.4
37
+
38
+ ### Enhancement
39
+ - Region support added
40
+
41
+ ------------------------------------------------
42
+
43
+ ## Version 0.0.3
44
+
45
+ ### Enhancement
46
+ - Query - function for passing locale is added.
47
+
48
+ ------------------------------------------------
49
+
50
+ ## Version 0.0.1
51
+
52
+ - Initial release
53
+
54
+ ------------------------------------------------
data/CODE_OF_CONDUCT.md CHANGED
@@ -22,8 +22,7 @@ include:
22
22
 
23
23
  Examples of unacceptable behavior by participants include:
24
24
 
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or advances
27
26
  * Trolling, insulting/derogatory comments, and personal or political attacks
28
27
  * Public or private harassment
29
28
  * Publishing others' private information, such as a physical or electronic
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ gem "rspec"
5
+ gem "webmock"
6
+ gem "simplecov"
7
+ gem "activesupport"
8
+ gem "yard"
data/Gemfile.lock ADDED
@@ -0,0 +1,61 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ activesupport (6.0.3.1)
5
+ concurrent-ruby (~> 1.0, >= 1.0.2)
6
+ i18n (>= 0.7, < 2)
7
+ minitest (~> 5.1)
8
+ tzinfo (~> 1.1)
9
+ zeitwerk (~> 2.2, >= 2.2.2)
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ concurrent-ruby (1.1.6)
13
+ crack (0.4.3)
14
+ safe_yaml (~> 1.0.0)
15
+ diff-lcs (1.3)
16
+ docile (1.3.2)
17
+ hashdiff (1.0.1)
18
+ i18n (1.8.2)
19
+ concurrent-ruby (~> 1.0)
20
+ minitest (5.14.1)
21
+ public_suffix (4.0.4)
22
+ rspec (3.9.0)
23
+ rspec-core (~> 3.9.0)
24
+ rspec-expectations (~> 3.9.0)
25
+ rspec-mocks (~> 3.9.0)
26
+ rspec-core (3.9.1)
27
+ rspec-support (~> 3.9.1)
28
+ rspec-expectations (3.9.1)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.9.0)
31
+ rspec-mocks (3.9.1)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.9.0)
34
+ rspec-support (3.9.2)
35
+ safe_yaml (1.0.5)
36
+ simplecov (0.18.5)
37
+ docile (~> 1.1)
38
+ simplecov-html (~> 0.11)
39
+ simplecov-html (0.12.2)
40
+ thread_safe (0.3.6)
41
+ tzinfo (1.2.7)
42
+ thread_safe (~> 0.1)
43
+ webmock (3.8.3)
44
+ addressable (>= 2.3.6)
45
+ crack (>= 0.3.2)
46
+ hashdiff (>= 0.4.0, < 2.0.0)
47
+ yard (0.9.25)
48
+ zeitwerk (2.3.0)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ activesupport
55
+ rspec
56
+ simplecov
57
+ webmock
58
+ yard
59
+
60
+ BUNDLED WITH
61
+ 2.1.4
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2016 amite
3
+ Copyright (c) 2012-2021 Contentstack. All Rights Reserved
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -19,8 +19,16 @@ Or you can run this command in your terminal (you might need administrator privi
19
19
  gem install contentstack
20
20
 
21
21
  To start using the SDK in your application, you will need to initialize the stack by providing the values for the keys given in the code snippet below.
22
-
23
- client = Contentstack::Client.new("site_api_key", "access_token", "enviroment_name")
22
+ ```ruby
23
+ # with default region
24
+ client = Contentstack::Client.new("api_key", "delivery_token", "enviroment_name")
25
+
26
+ # with specific region
27
+ client = Contentstack::Client.new("api_key", "delivery_token", "enviroment_name",{"region": Contentstack::Region::EU})
28
+
29
+ # with custom host
30
+ client = Contentstack::Client.new("api_key", "delivery_token", "enviroment_name",{"host": "https://custom-cdn.contentstack.com"})
31
+ ```
24
32
 
25
33
  ## **Key Concepts for using Contentstack**
26
34
 
@@ -46,23 +54,44 @@ A publishing environment corresponds to one or more deployment servers or a cont
46
54
 
47
55
  ## **Contentstack Ruby SDK: 5-minute Quickstart**
48
56
 
49
- ### **Initializing your SDK **
57
+ ### **Initializing your SDK**
50
58
 
51
59
  To initialize the SDK, you need to provide values for the keys given in the snippet below:
52
-
53
- stack = Contentstack::Client.new("site_api_key", "access_token", "enviroment_name")
60
+ ```ruby
61
+ @stack = Contentstack::Client.new("api_key", "delivery_token", "enviroment_name")
62
+ ```
54
63
 
55
64
  To get the API credentials mentioned above, log in to your Contentstack account and then in your top panel navigation, go to Settings > Stack to view the API Key and Access Token.
56
65
 
57
66
  ### **Querying content from your stack**
58
67
 
59
68
  To fetch all entries of of a content type, use the query given below:
60
-
61
- entry = stack.content_type(<<CONTENT_TYPE_UID>>).query();
69
+ ```ruby
70
+ @entry = stack.content_type(<<CONTENT_TYPE_UID>>).query();
71
+ ```
62
72
 
63
73
  To fetch a specific entry from a content type, use the following query:
64
-
65
- entry = stack.content_type(<<CONTENT_TYPE_UID>>).entry(<<ENTRY_UID>>);
74
+ ```ruby
75
+ @entry = stack.content_type(<<CONTENT_TYPE_UID>>).entry(<<ENTRY_UID>>);
76
+ ```
77
+
78
+ ### Get Multiple Entries
79
+ To retrieve multiple entries of a content type, specify the content type UID. You can also specify search parameters to filter results:
80
+ ```ruby
81
+ @query = @stack.content_type('blog').query
82
+ @entries = @query.where('title', 'welcome')
83
+ .include_schema
84
+ .include_count
85
+ .fetch
86
+ puts "Total Entries -- #{@entries.count}"
87
+ @entries.each{|entry| puts "#{entry.get('title')}" }
88
+ ```
89
+
90
+ To retrieve localized versions of entries, you can use the query attribute:
91
+ ```ruby
92
+ entry = @stack.content_type('content_type_uid').query.locale('locale_code').fetch()
93
+ ```
94
+ > Note: Currently, the above query works in case of retrieving localized versions of multiple entries only.
66
95
 
67
96
  ## **Advanced Queries**
68
97
 
@@ -70,6 +99,19 @@ You can query for content types, entries, assets and more using our Ruby API Ref
70
99
 
71
100
  [Ruby API Reference Doc](http://www.rubydoc.info/gems/contentstack)
72
101
 
102
+ ### Paginating Responses
103
+ In a single instance, the [Get Multiple Entries](https://www.contentstack.com/docs/developers/ruby/get-started-with-ruby-sdk/#get-multiple-entries) query will retrieve only the first 100 items of the specified content type. You can paginate and retrieve the rest of the items in batches using the [skip](https://www.rubydoc.info/gems/contentstack/Contentstack/Query#skip-instance_method) and [limit](https://www.rubydoc.info/gems/contentstack/Contentstack/Query#limit-instance_method) parameters in subsequent requests.
104
+
105
+ ```ruby
106
+ @stack = Contentstack::Client.new("api_key", "delivery_token", "environment")
107
+ @entries = @stack.content_type('category').query
108
+ .limit(20)
109
+ .skip(50)
110
+ .fetch
111
+ ```
112
+
113
+ > Note: Currently, the Ruby SDK does not support multiple content types referencing in a single query. For more information on how to query entries and assets, refer the Queries section of our Content Delivery API documentation.
114
+
73
115
  ## **Working with Images**
74
116
 
75
117
  We have introduced Image Delivery APIs that let you retrieve images and then manipulate and optimize them for your digital properties. It lets you perform a host of other actions such as crop, trim, resize, rotate, overlay, and so on.
@@ -78,8 +120,63 @@ For example, if you want to crop an image (with width as 300 and height as 400),
78
120
 
79
121
  [Read Image Delivery API documentation](https://www.contentstack.com/docs/apis/image-delivery-api/).
80
122
 
81
- SDK functions for Image Delivery API coming soon.
82
123
 
124
+ ## **Using the Sync API with Ruby SDK**
125
+
126
+ The Sync API takes care of syncing your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates. Contentstack’s Ruby SDK supports Sync API, which you can use to build powerful apps. Read through to understand how to use the Sync API with Contentstack Ruby SDK.
127
+ [Read Sync API documentation](https://www.contentstack.com/docs/platforms/ruby#using-the-sync-api-with-ruby-sdk).
128
+
129
+ ### Initial sync
130
+
131
+ The Initial Sync process performs a complete sync of your app data. It returns all the published entries and assets of the specified stack in response.
132
+
133
+ To start the Initial Sync process, use the syncStack method.
134
+
135
+ ```ruby
136
+ @sync_result = @stack.sync({init: true})
137
+ # @sync_result.items: contains sync data
138
+ # @sync_result.pagination_token: for fetching the next batch of entries using pagination token
139
+ # @sync_result.sync_token: for performing subsequent sync after initial sync
140
+ ```
141
+
142
+ The response also contains a sync token, which you need to store, since this token is used to get subsequent delta updates later, as shown in the Subsequent Sync section below.
143
+
144
+ You can also fetch custom results in initial sync by using advanced sync queries.
145
+
146
+ ### Sync pagination
147
+
148
+ If the result of the initial sync (or subsequent sync) contains more than 100 records, the response would be paginated. It provides pagination token in the response. You will need to use this token to get the next batch of data.
149
+
150
+ ```ruby
151
+ @sync_result = @stack.sync({pagination_token : "<pagination_token>"})
152
+ # @sync_result.items: contains sync data
153
+ # @sync_result.pagination_token: For fetching the next batch of entries using pagination token
154
+ # @sync_result.sync_token: For performing subsequent sync after initial sync
155
+ ```
156
+
157
+ ### Subsequent sync
158
+
159
+ You can use the sync token (that you receive after initial sync) to get the updated content next time. The sync token fetches only the content that was added after your last sync, and the details of the content that was deleted or updated.
160
+
161
+ ```ruby
162
+ @sync_result = @stack.sync({sync_token : "<sync_token>"})
163
+ # @sync_result.items: contains sync data
164
+ # @sync_result.pagination_token: For fetching the next batch of entries using pagination token
165
+ # @sync_result.sync_token: For performing subsequent sync after initial sync
166
+ ```
167
+
168
+ ### Advanced sync queries
169
+
170
+ You can use advanced sync queries to fetch custom results while performing initial sync.
171
+ [Read advanced sync queries documentation](https://www.contentstack.com/docs/guide/synchronization/using-the-sync-api-with-ruby-sdk#advanced-sync-queries)
172
+
173
+
174
+ ```ruby
175
+ @sync_result = @stack.sync({init : true, locale: "<locale>" content_type_uid: "<content_type_uid>"})
176
+ # @sync_result.items: contains sync data
177
+ # @sync_result.pagination_token: For fetching the next batch of entries using pagination token
178
+ # @sync_result.sync_token: For performing subsequent sync after initial sync
179
+ ```
83
180
  ## **Helpful Links**
84
181
 
85
182
  * [Contentstack Website](https://www.contentstack.com)
@@ -90,7 +187,7 @@ SDK functions for Image Delivery API coming soon.
90
187
 
91
188
  ## **The MIT License (MIT)**
92
189
 
93
- Copyright © 2012-2017 [Built.io](https://www.built.io/). All Rights Reserved
190
+ Copyright © 2012-2021 [Contentstack](https://www.contentstack.com). All Rights Reserved
94
191
 
95
192
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
96
193
 
data/contentstack.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.required_ruby_version = '>= 2.0'
13
13
 
14
14
  s.license = "MIT"
15
- s.homepage = "https://github.com/builtio-contentstack/contentstack-ruby"
15
+ s.homepage = "https://github.com/contentstack/contentstack-ruby"
16
16
 
17
17
  s.summary = %q{Contentstack Ruby client for the Content Delivery API}
18
18
  s.description = %q{Contentstack Ruby client for the Content Delivery API}
data/lib/contentstack.rb CHANGED
@@ -2,15 +2,20 @@ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
2
 
3
3
  require "contentstack/version"
4
4
  require "contentstack/client"
5
+ require "contentstack/region"
5
6
  require "util"
6
7
 
7
8
 
8
- # == Built.io Contentstack - Ruby SDK
9
- # Built.io Contentstack is a content management system that facilitates the process of publication by separating the content from site-related programming and design.
9
+ # == Contentstack - Ruby SDK
10
+ # Contentstack is a content management system that facilitates the process of publication by separating the content from site-related programming and design.
10
11
  # == Installation
11
12
  # gem install contentstack
12
13
  # == Initialize the Stack
13
- # @stack = Contentstack::Client.new("site_api_key", "access_token", "enviroment_name")
14
+ # @stack = Contentstack::Client.new("site_api_key", "delivery_token", "enviroment_name")
15
+ # == Initialize the Stack for EU region
16
+ # @stack = Contentstack::Client.new("site_api_key", "delivery_token", "enviroment_name", {"region": Contentstack::Region::EU })
17
+ # == Initialize the Stack for custom host
18
+ # @stack = Contentstack::Client.new("site_api_key", "delivery_token", "enviroment_name", {"host": "https://custom-cdn.contentstack.com" })
14
19
  # == Usage
15
20
  # ==== Get single entry
16
21
  # @stack.content_type('blog').entry('<entry_uid_here>').fetch
@@ -6,11 +6,13 @@ require 'open-uri'
6
6
 
7
7
  module Contentstack
8
8
  class API
9
- def self.init_api(api_key, access_token, environment)
10
- @host = "https://cdn.contentstack.io"
9
+ def self.init_api(api_key, delivery_token, environment,host)
10
+ @host = host
11
11
  @api_version = '/v3'
12
12
  @environment = environment
13
- @headers = {api_key: api_key, access_token: access_token, user_agent: "ruby-sdk/#{Contentstack::VERSION}", environment: @environment}
13
+ @api_key = api_key
14
+ @access_token = delivery_token
15
+ @headers = {environment: @environment}
14
16
  end
15
17
 
16
18
  def self.fetch_content_types(uid="")
@@ -27,9 +29,9 @@ module Contentstack
27
29
  send_request(path, query)
28
30
  end
29
31
 
30
- def self.fetch_entry(content_type, entry_uid)
32
+ def self.fetch_entry(content_type, entry_uid, query)
31
33
  path = "/content_types/#{content_type}/entries/#{entry_uid}"
32
- send_request(path)
34
+ send_request(path, query)
33
35
  end
34
36
 
35
37
  def self.get_assets(asset_uid=nil)
@@ -38,17 +40,25 @@ module Contentstack
38
40
  send_request(path)
39
41
  end
40
42
 
43
+ def self.get_sync_items(query)
44
+ path = "/stacks/sync"
45
+ send_request(path, query)
46
+ end
47
+
41
48
  private
42
49
  def self.send_request(path, q=nil)
43
50
  q ||= {}
44
51
 
45
52
  q.merge!(@headers)
46
- q.merge!({timestamp: Time.now.to_i})
47
53
 
48
54
  query = "?" + q.to_query
49
55
  # puts "Request URL:- #{@host}#{@api_version}#{path}#{query} \n\n"
50
56
 
51
- ActiveSupport::JSON.decode(open("#{@host}#{@api_version}#{path}#{query}").read)
57
+ ActiveSupport::JSON.decode(open("#{@host}#{@api_version}#{path}#{query}",
58
+ "api_key" => @api_key,
59
+ "access_token"=> @access_token,
60
+ "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}",
61
+ "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}").read)
52
62
  end
53
63
  end
54
- end
64
+ end
@@ -1,14 +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
- # Initialize "Built.io Contentstack" Client instance
8
- def initialize(api_key, access_token, environment)
9
- API.init_api(api_key, access_token, environment)
7
+ attr_reader :region, :host
8
+ # Initialize "Contentstack" Client instance
9
+ def initialize(api_key, delivery_token, environment, options={})
10
+ @region = options[:region].nil? ? Contentstack::Region::US : options[:region]
11
+ @host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host]
12
+ API.init_api(api_key, delivery_token, environment, @host)
10
13
  end
11
-
12
14
 
13
15
  def content_types
14
16
  ContentType.all
@@ -25,5 +27,39 @@ module Contentstack
25
27
  def asset(uid)
26
28
  Asset.new(uid)
27
29
  end
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
+
54
+ private
55
+ def get_default_region_hosts(region='us')
56
+ case region
57
+ when "us"
58
+ host = "https://cdn.contentstack.io"
59
+ when "eu"
60
+ host = "https://eu-cdn.contentstack.com"
61
+ end
62
+ host
63
+ end
28
64
  end
29
65
  end