loading
Generated 2021-08-31T10:23:02+05:30

All Files ( 96.71% covered at 75.08 hits/line )

20 files in total.
791 relevant lines, 765 lines covered and 26 lines missed. ( 96.71% )
File % covered Lines Relevant Lines Lines covered Lines missed Avg. Hits / Line
lib/contentstack.rb 100.00 % 32 11 11 0 1.00
lib/contentstack/api.rb 100.00 % 64 38 38 0 26.61
lib/contentstack/asset.rb 100.00 % 67 17 17 0 8.18
lib/contentstack/asset_collection.rb 100.00 % 26 12 12 0 2.00
lib/contentstack/client.rb 100.00 % 65 28 28 0 20.89
lib/contentstack/content_type.rb 100.00 % 52 25 25 0 21.20
lib/contentstack/entry.rb 100.00 % 207 65 65 0 24.58
lib/contentstack/entry_collection.rb 87.50 % 43 24 21 3 21.08
lib/contentstack/query.rb 93.33 % 630 150 140 10 3.22
lib/contentstack/region.rb 100.00 % 6 4 4 0 1.00
lib/contentstack/sync_result.rb 73.91 % 30 23 17 6 1.04
lib/contentstack/version.rb 100.00 % 3 2 2 0 1.00
lib/util.rb 87.50 % 107 56 49 7 964.02
spec/asset_spec.rb 100.00 % 48 32 32 0 1.22
spec/content_type_spec.rb 100.00 % 81 47 47 0 1.28
spec/contentstack_spec.rb 100.00 % 39 23 23 0 1.13
spec/entry_collection_spec.rb 100.00 % 42 28 28 0 1.21
spec/entry_spec.rb 100.00 % 94 60 60 0 1.77
spec/query_spec.rb 100.00 % 198 124 124 0 1.58
spec/sync_spec.rb 100.00 % 27 22 22 0 1.09

lib/contentstack.rb

100.0% lines covered

11 relevant lines. 11 lines covered and 0 lines missed.
    
  1. 1 $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
  2. 1 require "contentstack/version"
  3. 1 require "contentstack/client"
  4. 1 require "contentstack/region"
  5. 1 require "contentstack_utils"
  6. 1 require "util"
  7. # == Contentstack - Ruby SDK
  8. # Contentstack is a content management system that facilitates the process of publication by separating the content from site-related programming and design.
  9. # == Installation
  10. # gem install contentstack
  11. # == Initialize the Stack
  12. # @stack = Contentstack::Client.new("site_api_key", "delivery_token", "enviroment_name")
  13. # == Initialize the Stack for EU region
  14. # @stack = Contentstack::Client.new("site_api_key", "delivery_token", "enviroment_name", {"region": Contentstack::Region::EU })
  15. # == Initialize the Stack for custom host
  16. # @stack = Contentstack::Client.new("site_api_key", "delivery_token", "enviroment_name", {"host": "https://custom-cdn.contentstack.com" })
  17. # == Usage
  18. # ==== Get single entry
  19. # @stack.content_type('blog').entry('<entry_uid_here>').fetch
  20. # ==== Query entries
  21. # @stack.content_type('blog').query.regex('title', '.*hello.*').fetch
  22. 1 module Contentstack
  23. 1 def self.render_content(content, options)
  24. 1 ContentstackUtils.render_content(content, options)
  25. end
  26. 1 def self.json_to_html(content, options)
  27. 1 ContentstackUtils.json_to_html(content, options)
  28. end
  29. end

lib/contentstack/api.rb

100.0% lines covered

38 relevant lines. 38 lines covered and 0 lines missed.
    
  1. 1 require 'uri'
  2. 1 require 'net/http'
  3. 1 require 'active_support'
  4. 1 require 'active_support/json'
  5. 1 require 'open-uri'
  6. 1 module Contentstack
  7. 1 class API
  8. 1 def self.init_api(api_key, delivery_token, environment,host)
  9. 81 @host = host
  10. 81 @api_version = '/v3'
  11. 81 @environment = environment
  12. 81 @api_key = api_key
  13. 81 @access_token = delivery_token
  14. 81 @headers = {environment: @environment}
  15. end
  16. 1 def self.fetch_content_types(uid="")
  17. 9 if !uid.nil? && !uid.empty?
  18. 1 path = "/content_types/#{uid}"
  19. else
  20. 8 path = "/content_types"
  21. end
  22. 9 send_request(path, {})
  23. end
  24. 1 def self.fetch_entries(content_type, query)
  25. 48 path = "/content_types/#{content_type}/entries"
  26. 48 send_request(path, query)
  27. end
  28. 1 def self.fetch_entry(content_type, entry_uid, query)
  29. 14 path = "/content_types/#{content_type}/entries/#{entry_uid}"
  30. 14 send_request(path, query)
  31. end
  32. 1 def self.get_assets(asset_uid=nil)
  33. 9 path = "/assets"
  34. 9 path += "/#{asset_uid}" if !asset_uid.nil?
  35. 9 send_request(path)
  36. end
  37. 1 def self.get_sync_items(query)
  38. 2 path = "/stacks/sync"
  39. 2 send_request(path, query)
  40. end
  41. 1 private
  42. 1 def self.send_request(path, q=nil)
  43. 82 q ||= {}
  44. 82 q.merge!(@headers)
  45. 82 query = "?" + q.to_query
  46. # puts "Request URL:- #{@host}#{@api_version}#{path}#{query} \n\n"
  47. 82 ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}",
  48. "api_key" => @api_key,
  49. "access_token"=> @access_token,
  50. "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}",
  51. "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}").read)
  52. end
  53. end
  54. end

lib/contentstack/asset.rb

100.0% lines covered

17 relevant lines. 17 lines covered and 0 lines missed.
    
  1. 1 module Contentstack
  2. # Asset class to fetch file details on Conentstack server.
  3. 1 class Asset
  4. 1 attr_reader :uid, :content_type, :filename, :file_size, :tags, :url
  5. # @!attribute [r] uid
  6. # Contentstack Asset UID for this asset
  7. # @!attribute [r] content_type
  8. # Content Type for the asset. image/png, image/jpeg, application/pdf, video/mp4 etc.
  9. # @!attribute [r] filename
  10. # Name of the asset.
  11. # @!attribute [r] file_size
  12. # Size of the asset.
  13. # @!attribute [r] tags
  14. # Array of tags assigned to the asset.
  15. # @!attribute [r] url
  16. # URL to fetch/render the asset
  17. # Create instance of an Asset. Accepts either a uid of asset (String) or a complete asset JSON
  18. # @param [String/Hash] attrs
  19. # Usage for String parameter
  20. # @asset = @stack.asset("some_asset_uid")
  21. # @asset.fetch
  22. #
  23. # Usage for Hash parameter
  24. # @asset = @stack.asset({
  25. # :uid => "some_asset_uid",
  26. # :content_type => "file_type", # image/png, image/jpeg, application/pdf, video/mp4 etc.
  27. # :filename => "some_file_name",
  28. # :file_size => "some_file_size",
  29. # :tags => ["tag1", "tag2", "tag3"],
  30. # :url => "file_url"
  31. # })
  32. # @asset.fetch
  33. 1 def initialize(attrs)
  34. 18 if attrs.class == String
  35. 7 @uid = attrs
  36. else
  37. 11 attrs = attrs.symbolize_keys
  38. 11 @uid = attrs[:uid]
  39. 11 @content_type = attrs[:content_type]
  40. 11 @filename = attrs[:filename]
  41. 11 @file_size = attrs[:file_size]
  42. 11 @tags = attrs[:tags]
  43. 11 @url = attrs[:url]
  44. end
  45. 18 self
  46. end
  47. # Fetch a particular asset using uid.
  48. # @asset = @stack.asset('some_asset_uid')
  49. # @asset.fetch
  50. # puts @asset.url
  51. 1 def fetch
  52. 7 json = API.get_assets(@uid)
  53. # puts "json -- #{json}"
  54. 7 self.class.new(json["asset"])
  55. end
  56. end
  57. end

lib/contentstack/asset_collection.rb

100.0% lines covered

12 relevant lines. 12 lines covered and 0 lines missed.
    
  1. 1 require 'contentstack/asset'
  2. 1 module Contentstack
  3. # Asset class to fetch details of files on Conentstack server.
  4. 1 class AssetCollection
  5. 1 attr_reader :assets
  6. 1 def initialize(assets_array=nil)
  7. 4 if assets_array.nil?
  8. 2 @assets = []
  9. 2 return self
  10. else
  11. 6 @assets = assets_array.collect{|a| Asset.new(a)}
  12. end
  13. end
  14. # Fetch assets uploaded to Contentstack
  15. #
  16. # Example:
  17. # @assets = @stack.assets.fetch
  18. 1 def fetch
  19. 2 json = API.get_assets
  20. 2 self.class.new(json["assets"])
  21. end
  22. end
  23. end

lib/contentstack/client.rb

100.0% lines covered

28 relevant lines. 28 lines covered and 0 lines missed.
    
  1. 1 require 'contentstack/api'
  2. 1 require 'contentstack/content_type'
  3. 1 require 'contentstack/asset_collection'
  4. 1 require 'contentstack/sync_result'
  5. 1 module Contentstack
  6. 1 class Client
  7. 1 attr_reader :region, :host
  8. # Initialize "Contentstack" Client instance
  9. 1 def initialize(api_key, delivery_token, environment, options={})
  10. 81 @region = options[:region].nil? ? Contentstack::Region::US : options[:region]
  11. 81 @host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host]
  12. 81 API.init_api(api_key, delivery_token, environment, @host)
  13. end
  14. 1 def content_types
  15. 8 ContentType.all
  16. end
  17. 1 def content_type(uid)
  18. 69 ContentType.new({uid: uid})
  19. end
  20. 1 def assets
  21. 2 AssetCollection.new
  22. end
  23. 1 def asset(uid)
  24. 7 Asset.new(uid)
  25. end
  26. # Syncs your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates
  27. #
  28. # Stack.sync({'init': true}) // For initializing sync
  29. #
  30. # Stack.sync({'init': true, 'locale': 'en-us'}) //For initializing sync with entries of a specific locale
  31. #
  32. # Stack.sync({'init': true, 'start_date': '2018-10-22'}) //For initializing sync with entries published after a specific date
  33. #
  34. # Stack.sync({'init': true, 'content_type_uid': 'session'}) //For initializing sync with entries of a specific content type
  35. #
  36. # 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'.
  37. #
  38. # Stack.sync({'pagination_token': '<btlsomething>'}) // For fetching the next batch of entries using pagination token
  39. #
  40. # Stack.sync({'sync_token': '<btlsomething>'}) // For performing subsequent sync after initial sync
  41. #
  42. # @param params [Hash] params is an object that supports ‘locale’, ‘start_date’, ‘content_type_uid’, and ‘type’ queries.
  43. 1 def sync(params)
  44. 2 sync_result = API.get_sync_items(params)
  45. 2 SyncResult.new(sync_result)
  46. end
  47. 1 private
  48. 1 def get_default_region_hosts(region='us')
  49. 79 case region
  50. when "us"
  51. 77 host = "https://cdn.contentstack.io"
  52. when "eu"
  53. 2 host = "https://eu-cdn.contentstack.com"
  54. end
  55. 79 host
  56. end
  57. end
  58. end

lib/contentstack/content_type.rb

100.0% lines covered

25 relevant lines. 25 lines covered and 0 lines missed.
    
  1. 1 require 'contentstack/query'
  2. 1 module Contentstack
  3. 1 class ContentType
  4. 1 [:title, :uid, :created_at, :updated_at, :attributes].each do |method_name|
  5. 5 if [:created_at, :updated_at].include?(method_name)
  6. 2 define_method method_name do
  7. 10 return Time.parse(@attributes[method_name]) if @attributes[method_name] && !@attributes[method_name].nil?
  8. end
  9. 3 elsif :attributes == method_name
  10. 1 define_method :attributes do
  11. {
  12. 3 title: self.title,
  13. uid: self.uid,
  14. created_at: self.created_at,
  15. updated_at: self.updated_at,
  16. schema: @attributes[:schema]
  17. }
  18. end
  19. else
  20. 2 define_method method_name do
  21. 81 return @attributes[method_name]
  22. end
  23. end
  24. end
  25. 1 def initialize(object)
  26. 91 @attributes = object.symbolize_keys
  27. end
  28. 1 def query
  29. 53 Query.new(self.uid)
  30. end
  31. 1 def entry(entry_uid)
  32. 15 Entry.new({uid: entry_uid}, self.uid)
  33. end
  34. 1 def self.all
  35. 8 content_types = API.fetch_content_types["content_types"]
  36. 8 content_types.map do |content_type|
  37. 224 ContentType.new(content_type.inject({}){|clone,(k,v)| clone[k.to_sym] = v; clone})
  38. end
  39. end
  40. 1 def fetch
  41. 1 content_type = API.fetch_content_types(uid)["content_type"]
  42. 14 ContentType.new(content_type.inject({}){|clone,(k,v)| clone[k.to_sym] = v; clone})
  43. end
  44. end
  45. end

lib/contentstack/entry.rb

100.0% lines covered

65 relevant lines. 65 lines covered and 0 lines missed.
    
  1. 1 require 'active_support/core_ext'
  2. 1 module Contentstack
  3. 1 class Entry
  4. 1 attr_reader :fields, :content_type, :uid, :owner, :query, :schema, :content_type
  5. 1 def initialize(attrs, content_type_uid=nil)
  6. 229 setup(attrs, content_type_uid)
  7. end
  8. # Get entries from the specified locale.
  9. #
  10. # @param [String] code The locale code of the entry
  11. #
  12. # Example
  13. # @entry = @stack.content_type('category').entry(entry_uid)
  14. # @entry.locale('en-us')
  15. #
  16. # @return [Contentstack::Entry]
  17. 1 def locale(code)
  18. 1 @query[:locale] = code
  19. 1 self
  20. end
  21. # Specifies an array of 'only' keys in BASE object that would be 'included' in the response.
  22. #
  23. # @param [Array] fields Array of the 'only' reference keys to be included in response.
  24. # @param [Array] fields_with_base Can be used to denote 'only' fields of the reference class
  25. #
  26. # Example
  27. # # Include only title and description field in response
  28. # @entry = @stack.content_type('category').entry(entry_uid)
  29. # @entry.only(['title', 'description'])
  30. #
  31. # # Query product and include only the title and description from category reference
  32. # @entry = @stack.content_type('product').entry(entry_uid)
  33. # @entry.include_reference('category')
  34. # .only('category', ['title', 'description'])
  35. #
  36. # @return [Contentstack::Entry]
  37. 1 def only(fields, fields_with_base=nil)
  38. 2 q = {}
  39. 2 if [Array, String].include?(fields_with_base.class)
  40. 1 fields_with_base = [fields_with_base] if fields_with_base.class == String
  41. 1 q[fields.to_sym] = fields_with_base
  42. else
  43. 1 fields = [fields] if fields.class == String
  44. 1 q = {BASE: fields}
  45. end
  46. 2 @query[:only] = q
  47. 2 self
  48. end
  49. # Specifies list of field uids that would be 'excluded' from the response.
  50. #
  51. # @param [Array] fields Array of field uid which get 'excluded' from the response.
  52. # @param [Array] fields_with_base Can be used to denote 'except' fields of the reference class
  53. #
  54. # Example
  55. # # Exclude 'description' field in response
  56. # @entry = @stack.content_type('category').entry(entry_uid)
  57. # @entry.except(['description'])
  58. #
  59. # # Query product and exclude the 'description' from category reference
  60. # @entry = @stack.content_type('product').entry(entry_uid)
  61. # @entry.include_reference('category')
  62. # .except('category', ['description'])
  63. #
  64. # @return [Contentstack::Entry]
  65. 1 def except(fields, fields_with_base=nil)
  66. 3 q = {}
  67. 3 if [Array, String].include?(fields_with_base.class)
  68. 1 fields_with_base = [fields_with_base] if fields_with_base.class == String
  69. 1 q[fields.to_sym] = fields_with_base
  70. else
  71. 2 fields = [fields] if fields.class == String
  72. 2 q = {BASE: fields}
  73. end
  74. 3 @query[:except] = q
  75. 3 self
  76. end
  77. # Add a constraint that requires a particular reference key details.
  78. #
  79. # @param [String/Array] reference_field_uids Pass string or array of reference fields that must be included in the response
  80. #
  81. # Example
  82. #
  83. # # Include reference of 'category'
  84. # @entry = @stack.content_type('product').entry(entry_uid)
  85. # @entry.include_reference('category')
  86. #
  87. # # Include reference of 'category' and 'reviews'
  88. # @entry = @stack.content_type('product').entry(entry_uid)
  89. # @entry.include_reference(['category', 'reviews'])
  90. #
  91. # @return [Contentstack::Entry]
  92. 1 def include_reference(reference_field_uids)
  93. 3 self.include(reference_field_uids)
  94. end
  95. # Include schemas of all returned objects along with objects themselves.
  96. #
  97. # Example
  98. #
  99. # @entry = @stack.content_type('product').entry(entry_uid)
  100. # @entry.include_schema
  101. #
  102. # @return [Contentstack::Entry]
  103. 1 def include_schema(flag=true)
  104. 1 @query[:include_schema] = flag
  105. 1 self
  106. end
  107. # Include object owner's profile in the objects data.
  108. #
  109. # Example
  110. #
  111. # @entry = @stack.content_type('product').entry(entry_uid)
  112. # @entry.include_owner
  113. #
  114. # @return [Contentstack::Entry]
  115. 1 def include_owner(flag=true)
  116. 1 @query[:include_owner] = flag
  117. 1 self
  118. end
  119. # Include object's content_type in response
  120. #
  121. # Example
  122. #
  123. # @entry = @stack.content_type('product').entry(entry_uid)
  124. # @entry.include_content_type
  125. #
  126. # @return [Contentstack::Entry]
  127. 1 def include_content_type(flag=true)
  128. 1 @query[:include_content_type] = flag
  129. 1 self
  130. end
  131. # Include the fallback locale publish content, if specified locale content is not publish.
  132. #
  133. # Example
  134. #
  135. # @entry = @stack.content_type('product').entry(entry_uid)
  136. # @entry.include_fallback
  137. #
  138. # @return [Contentstack::Entry]
  139. 1 def include_fallback(flag=true)
  140. 1 @query[:include_fallback] = flag
  141. 1 self
  142. end
  143. # Include Embedded Objects (Entries and Assets) along with entry/entries details.
  144. #
  145. # Example
  146. #
  147. # @entry = @stack.content_type('product').entry(entry_uid)
  148. # @entry.include_embedded_items
  149. #
  150. # @return [Contentstack::Query]
  151. 1 def include_embedded_items()
  152. 1 @query[:include_embedded_items] = ['BASE']
  153. 1 self
  154. end
  155. #
  156. # @return [Contentstack::Query]
  157. 1 def include(field_uids)
  158. 3 field_uids = [field_uids] if field_uids.class == String
  159. 3 @query[:include] ||= []
  160. 3 @query[:include] = @query[:include] | field_uids
  161. 3 self
  162. end
  163. # Execute entry
  164. #
  165. # Example
  166. #
  167. # @entry = @stack.content_type('product').entry(entry_uid)
  168. # @entry.fetch
  169. #
  170. # @return [Contentstack::EntryCollection]
  171. 1 def fetch
  172. 14 entry = API.fetch_entry(@content_type, self.fields[:uid], @query)
  173. 14 setup(entry["entry"])
  174. 14 @schema = entry["schema"].symbolize_keys if entry["schema"]
  175. 14 @content_type = entry["content_type"].symbolize_keys if entry["content_type"]
  176. 14 self
  177. end
  178. 1 def get(field_uid)
  179. 4 raise Contentstack::Error("Please send a valid Field UID") if field_uid.class != String
  180. 4 @fields[field_uid.to_sym]
  181. end
  182. 1 private
  183. 1 def setup(attrs, content_type_uid=nil)
  184. 243 @fields = attrs.symbolize_keys
  185. 243 @content_type = content_type_uid if !content_type_uid.blank?
  186. 243 @owner = attrs[:_owner] if attrs[:_owner]
  187. 243 @uid = attrs[:uid]
  188. 243 @query = {}
  189. end
  190. end
  191. end

lib/contentstack/entry_collection.rb

87.5% lines covered

24 relevant lines. 21 lines covered and 3 lines missed.
    
  1. 1 require 'contentstack/entry'
  2. 1 module Contentstack
  3. 1 class EntryCollection
  4. 1 attr_reader :entries, :count, :content_type, :schema
  5. 1 def initialize(json, content_type_uid=nil)
  6. 48 @count = json["count"] if json["count"]
  7. 262 @entries = json["entries"].collect{|entry| Entry.new(entry, content_type_uid) }
  8. 48 @schema = json["schema"].symbolize_keys if json["schema"]
  9. 48 @content_type = json["content_type"].symbolize_keys if json["content_type"]
  10. 48 self
  11. end
  12. 1 def each &block
  13. @entries.map{|e| block.call(e)}
  14. end
  15. 1 def map &block
  16. self.each(&block)
  17. end
  18. 1 def collect &block
  19. self.each(&block)
  20. end
  21. 1 def length
  22. 20 @entries.length
  23. end
  24. 1 def first
  25. 17 @entries.first
  26. end
  27. 1 def last
  28. 2 @entries.last
  29. end
  30. 1 def get(index)
  31. 1 @entries[index]
  32. end
  33. end
  34. end

lib/contentstack/query.rb

93.33% lines covered

150 relevant lines. 140 lines covered and 10 lines missed.
    
  1. 1 require 'contentstack/entry_collection'
  2. 1 module Contentstack
  3. # A class that defines a query that is used to query for Entry instance.
  4. 1 class Query
  5. # @!attribute [r] query
  6. # Attribute which has all the information about the query which will be executed against Contentstack API
  7. # @!attribute [r] content_type
  8. # Denotes which `content_type` should the query be executed for
  9. 1 attr_reader :query, :content_type
  10. # Initialize the Query instance
  11. # @param [String] content_type
  12. #
  13. # Example:
  14. # @query = @stack.content_type('blog').query
  15. # @entries = @query.where('author', 'John Doe').fetch
  16. #
  17. # @return [Contentstack::Query]
  18. 1 def initialize(content_type)
  19. 53 @content_type = content_type
  20. @query = {
  21. 53 query: "{}",
  22. include_count: false,
  23. skip: 0,
  24. count: 10,
  25. desc: 'created_at'
  26. }
  27. end
  28. # Add a custom query against specified key.
  29. # @param [String] field_uid
  30. # @param [String/Number/Boolean/Hash] value
  31. #
  32. # Example:
  33. # @query = @stack.content_type('blog').query
  34. # @query.add_query('author', "Jane Doe")
  35. #
  36. # @return [Contentstack::Query]
  37. 1 def add_query(field_uid, value)
  38. add_query_hash({:"#{field_uid}" => value})
  39. end
  40. # Remove provided query key from custom query if exist.
  41. # @param [String] field_uid
  42. #
  43. # Example:
  44. # @query = @stack.content_type('blog').query
  45. # @query.remove_query('author')
  46. #
  47. # @return [Contentstack::Query]
  48. 1 def remove_query(field_uid)
  49. q = ActiveSupport::JSON.decode(@query[:query])
  50. q.delete(field_uid)
  51. @query[:query] = ActiveSupport::JSON.encode(q)
  52. self
  53. end
  54. # Add a constraint to fetch all entries that contains given value against specified key.
  55. # @param [Hash] query_hash
  56. #
  57. # Example:
  58. # @query = @stack.content_type('blog').query
  59. # @query.where({:author => "Jane Doe"})
  60. #
  61. # @return [Contentstack::Query]
  62. 1 def where(query_hash)
  63. 2 add_query_hash(query_hash)
  64. end
  65. # Add a regular expression constraint for finding string values that match the provided regular expression. This may be slow for large data sets.
  66. # @param [String] field_uid The key to be constrained.
  67. # @param [String] pattern The regular expression pattern to match.
  68. # @param [String] options Regex options
  69. #
  70. # Example:
  71. # @query = @stack.content_type('product').query
  72. # @query.regex('title', '.*Mobile.*', 'i') # Search without case sensitivity
  73. #
  74. # @return [Contentstack::Query]
  75. 1 def regex(field_uid, pattern, options="")
  76. hash = {
  77. 4 "#{field_uid}" => {
  78. "$regex": pattern
  79. }
  80. }
  81. 4 hash["#{field_uid}"]["$options"] = options if !options.empty? || !options.nil?
  82. 4 add_query_hash(hash)
  83. end
  84. # Add a constraint that requires, a specified key exists in response.
  85. # @param [String] field_uid The key to be constrained.
  86. #
  87. # Example:
  88. # @query = @stack.content_type('product').query
  89. # @query.exists?('product_image') # only fetch products which have a `product_image`
  90. #
  91. # @return [Contentstack::Query]
  92. 1 def exists?(field_uid)
  93. 1 add_query_hash({:"#{field_uid}" => {"$exists" => true}})
  94. end
  95. # Add a constraint that requires, a specified key does not exists in response.
  96. # @param [String] field_uid The key to be constrained.
  97. #
  98. # Example:
  99. # @query = @stack.content_type('product').query
  100. # @query.not_exists?('product_image') # only fetch products which do not have a `product_image`
  101. #
  102. # @return [Contentstack::Query]
  103. 1 def not_exists?(field_uid)
  104. 1 add_query_hash({:"#{field_uid}" => {"$exists" => false}})
  105. 1 self
  106. end
  107. # Combines all the queries together using AND operator.
  108. #
  109. # @param [Array] queries Array of instances of the Query class
  110. #
  111. # Each query should be an instance of the Contentstack::Query class, and belong to the same `content_type`
  112. # Example:
  113. # @query1 = @stack.content_type('category').query
  114. # @query1.where('title', 'Electronics')
  115. #
  116. # @query2 = @stack.content_type('category').query
  117. # @query2.regex('description', '.*Electronics.*')
  118. #
  119. # query_array = [@query1, @query2]
  120. #
  121. # @query = @stack.content_type('category').query
  122. # @query.and(query_array)
  123. #
  124. # @return [Contentstack::Query]
  125. 1 def and(queries)
  126. 1 add_query_hash({"$and" => concat_queries(queries)})
  127. 1 self
  128. end
  129. # Combines all the queries together using OR operator.
  130. #
  131. # @param [Array] queries Array of instances of the Query class
  132. #
  133. # Each query should be an instance of the Contentstack::Query class, and belong to the same `content_type`
  134. # Example:
  135. # @query1 = @stack.content_type('category').query
  136. # @query1.where('title', 'Electronics')
  137. #
  138. # @query2 = @stack.content_type('category').query
  139. # @query2.where('title', 'Apparel')
  140. #
  141. # query_array = [@query1, @query2]
  142. #
  143. # @query = @stack.content_type('category').query
  144. # @query.or(query_array)
  145. #
  146. # @return [Contentstack::Query]
  147. 1 def or(queries)
  148. 1 add_query_hash({"$or" => concat_queries(queries)})
  149. 1 self
  150. end
  151. # Add a constraint to the query that requires a particular key entry to be less than the provided value.
  152. #
  153. # @param [String] field_uid UID of the field for which query should be executed
  154. #
  155. # @param [String/Number] value Value that provides an upper bound
  156. #
  157. # Example
  158. # @query = @stack.content_type('product').query
  159. # @query.less_than('price', '100')
  160. #
  161. # @return [Contentstack::Query]
  162. 1 def less_than(field_uid, value)
  163. 1 add_query_hash({:"#{field_uid}" => {"$lt" => value}})
  164. 1 self
  165. end
  166. # Add a constraint to the query that requires a particular key entry to be less than or equal to the provided value.
  167. #
  168. # @param [String] field_uid UID of the field for which query should be executed
  169. #
  170. # @param [String/Number] value Value that provides an upper bound
  171. #
  172. # Example
  173. # @query = @stack.content_type('product').query
  174. # @query.less_than_or_equal('price', '100')
  175. #
  176. # @return [Contentstack::Query]
  177. 1 def less_than_or_equal(field_uid, value)
  178. 1 add_query_hash({:"#{field_uid}" => {"$lte" => value}})
  179. 1 self
  180. end
  181. # Add a constraint to the query that requires a particular key entry to be greater than the provided value.
  182. #
  183. # @param [String] field_uid UID of the field for which query should be executed
  184. #
  185. # @param [String/Number] value Value that provides a lower bound
  186. #
  187. # Example
  188. # @query = @stack.content_type('product').query
  189. # @query.greater_than('price', '100')
  190. #
  191. # @return [Contentstack::Query]
  192. 1 def greater_than(field_uid, value)
  193. 1 add_query_hash({:"#{field_uid}" => {"$gt" => value}})
  194. 1 self
  195. end
  196. # Add a constraint to the query that requires a particular key entry to be greater than or equal to the provided value.
  197. #
  198. # @param [String] field_uid UID of the field for which query should be executed
  199. #
  200. # @param [String/Number] value Value that provides a lower bound
  201. #
  202. # Example
  203. # @query = @stack.content_type('product').query
  204. # @query.greater_than_or_equal('price', '100')
  205. #
  206. # @return [Contentstack::Query]
  207. 1 def greater_than_or_equal(field_uid, value)
  208. 1 add_query_hash({:"#{field_uid}" => {"$gte" => value}})
  209. 1 self
  210. end
  211. # Add a constraint to the query that requires a particular key's entry to be not equal to the provided value.
  212. #
  213. # @param [String] field_uid UID of the field for which query should be executed
  214. # @param [String] value The object that must not be equaled.
  215. #
  216. # Example
  217. # @query = @stack.content_type('product').query
  218. # @query.not_equal_to('price', '100')
  219. #
  220. # @return [Contentstack::Query]
  221. 1 def not_equal_to(field_uid, value)
  222. 1 add_query_hash({:"#{field_uid}" => {"$ne" => value}})
  223. 1 self
  224. end
  225. # Add a constraint to the query that requires a particular key's entry to be contained in the provided array.
  226. #
  227. # @param [String] field_uid UID of the field for which query should be executed
  228. # @param [String] values The possible values for the key's object
  229. #
  230. # Example
  231. # @query = @stack.content_type('category').query
  232. # @query.contained_in("title", ["Electronics", "Apparel"])
  233. #
  234. # @return [Contentstack::Query]
  235. 1 def contained_in(field_uid, values)
  236. 2 add_query_hash({:"#{field_uid}" => {"$in" => values}})
  237. 2 self
  238. end
  239. # Add a constraint to the query that requires a particular key entry's value not be contained in the provided array.
  240. #
  241. # @param [String] field_uid UID of the field for which query should be executed
  242. # @param [String] values The possible values for the key's object
  243. #
  244. # Example
  245. # @query = @stack.content_type('category').query
  246. # @query.not_contained_in("title", ["Electronics", "Apparel"])
  247. #
  248. # @return [Contentstack::Query]
  249. 1 def not_contained_in(field_uid, values)
  250. 2 add_query_hash({:"#{field_uid}" => {"$nin" => values}})
  251. 2 self
  252. end
  253. # The number of objects to skip before returning any.
  254. #
  255. # @param [Number] count of objects to skip from resulset.
  256. #
  257. # Example
  258. # @query = @stack.content_type('category').query
  259. # @query.skip(50)
  260. #
  261. # @return [Contentstack::Query]
  262. 1 def skip(count)
  263. 1 @query[:skip] = count
  264. 1 self
  265. end
  266. # This method provides only the entries matching the specified value.
  267. #
  268. # @param [String] text value used to match or compare
  269. #
  270. # Example
  271. # @query = @stack.content_type('product').query
  272. # @query.search("This is an awesome product")
  273. #
  274. # @return [Contentstack::Query]
  275. 1 def search(text)
  276. 1 @query[:typeahead] = text
  277. 1 self
  278. end
  279. # A limit on the number of objects to return.
  280. #
  281. # @param [Number] count of objects to limit in resulset.
  282. #
  283. # Example
  284. # @query = @stack.content_type('category').query
  285. # @query.limit(50)
  286. #
  287. # @return [Contentstack::Query]
  288. 1 def limit(count=10)
  289. 1 @query[:limit] = count
  290. 1 self
  291. end
  292. # Retrieve only count of entries in result.
  293. #
  294. # Example
  295. # @query = @stack.content_type('category').query
  296. # @query.count
  297. #
  298. # @return [Integer]
  299. 1 def count
  300. 1 include_count
  301. 1 fetch.count
  302. end
  303. # Retrieve count and data of objects in result.
  304. #
  305. # Example
  306. # @query = @stack.content_type('category').query
  307. # @query.include_count
  308. #
  309. # @return [Contentstack::Query]
  310. 1 def include_count(flag=true)
  311. 8 @query[:include_count] = flag
  312. 8 self
  313. end
  314. # Sort the results in ascending order with the given key.
  315. # Sort the returned entries in ascending order of the provided key.
  316. #
  317. # @param [String] field_uid The key to order by
  318. #
  319. # Example
  320. # @query = @stack.content_type('category').query
  321. # @query.ascending
  322. #
  323. # @return [Contentstack::Query]
  324. 1 def ascending(field_uid)
  325. 1 @query.delete(:desc)
  326. 1 @query[:asc] = field_uid
  327. 1 self
  328. end
  329. # Sort the results in descending order with the given key.
  330. # Sort the returned entries in descending order of the provided key.
  331. #
  332. # @param [String] field_uid The key to order by
  333. #
  334. # Example
  335. # @query = @stack.content_type('category').query
  336. # @query.descending
  337. #
  338. # @return [Contentstack::Query]
  339. 1 def descending(field_uid)
  340. 1 @query.delete(:asc)
  341. 1 @query[:desc] = field_uid
  342. 1 self
  343. end
  344. # Get entries from the specified locale.
  345. #
  346. # @param [String] code The locale code of the entry
  347. #
  348. # Example
  349. # @query = @stack.content_type('category').query
  350. # @query.locale('en-us')
  351. #
  352. # @return [Contentstack::Query]
  353. 1 def locale(code)
  354. 1 @query[:locale] = code
  355. 1 self
  356. end
  357. # Specifies an array of 'only' keys in BASE object that would be 'included' in the response.
  358. #
  359. # @param [Array] fields Array of the 'only' reference keys to be included in response.
  360. # @param [Array] fields_with_base Can be used to denote 'only' fields of the reference class
  361. #
  362. # Example
  363. # # Include only title and description field in response
  364. # @query = @stack.content_type('category').query
  365. # @query.only(['title', 'description'])
  366. #
  367. # # Query product and include only the title and description from category reference
  368. # @query = @stack.content_type('product').query
  369. # @query.include_reference('category')
  370. # .only('category', ['title', 'description'])
  371. #
  372. # @return [Contentstack::Query]
  373. 1 def only(fields, fields_with_base=nil)
  374. 3 q = {}
  375. 3 if [Array, String].include?(fields_with_base.class)
  376. 1 fields_with_base = [fields_with_base] if fields_with_base.class == String
  377. 1 q[fields.to_sym] = fields_with_base
  378. else
  379. 2 fields = [fields] if fields.class == String
  380. 2 q = {BASE: fields}
  381. end
  382. 3 @query[:only] = q
  383. 3 self
  384. end
  385. # Specifies list of field uids that would be 'excluded' from the response.
  386. #
  387. # @param [Array] fields Array of field uid which get 'excluded' from the response.
  388. # @param [Array] fields_with_base Can be used to denote 'except' fields of the reference class
  389. #
  390. # Example
  391. # # Exclude 'description' field in response
  392. # @query = @stack.content_type('category').query
  393. # @query.except(['description'])
  394. #
  395. # # Query product and exclude the 'description' from category reference
  396. # @query = @stack.content_type('product').query
  397. # @query.include_reference('category')
  398. # .except('category', ['description'])
  399. #
  400. # @return [Contentstack::Query]
  401. 1 def except(fields, fields_with_base=nil)
  402. 3 q = {}
  403. 3 if [Array, String].include?(fields_with_base.class)
  404. 1 fields_with_base = [fields_with_base] if fields_with_base.class == String
  405. 1 q[fields.to_sym] = fields_with_base
  406. else
  407. 2 fields = [fields] if fields.class == String
  408. 2 q = {BASE: fields}
  409. end
  410. 3 @query[:except] = q
  411. 3 self
  412. end
  413. # Add a constraint that requires a particular reference key details.
  414. #
  415. # @param [String/Array] reference_field_uids Pass string or array of reference fields that must be included in the response
  416. #
  417. # Example
  418. #
  419. # # Include reference of 'category'
  420. # @query = @stack.content_type('product').query
  421. # @query.include_reference('category')
  422. #
  423. # # Include reference of 'category' and 'reviews'
  424. # @query = @stack.content_type('product').query
  425. # @query.include_reference(['category', 'reviews'])
  426. #
  427. # @return [Contentstack::Query]
  428. 1 def include_reference(reference_field_uids)
  429. 3 self.include(reference_field_uids)
  430. end
  431. # Include schemas of all returned objects along with objects themselves.
  432. #
  433. # Example
  434. #
  435. # @query = @stack.content_type('product').query
  436. # @query.include_schema
  437. #
  438. # @return [Contentstack::Query]
  439. 1 def include_schema(flag=true)
  440. 1 @query[:include_schema] = flag
  441. 1 self
  442. end
  443. # Include object owner's profile in the objects data.
  444. #
  445. # Example
  446. #
  447. # @query = @stack.content_type('product').query
  448. # @query.include_owner
  449. #
  450. # @return [Contentstack::Query]
  451. 1 def include_owner(flag=true)
  452. 1 @query[:include_owner] = flag
  453. 1 self
  454. end
  455. # Include object's content_type in response
  456. #
  457. # Example
  458. #
  459. # @query = @stack.content_type('product').query
  460. # @query.include_content_type
  461. #
  462. # @return [Contentstack::Query]
  463. 1 def include_content_type(flag=true)
  464. 1 @query[:include_content_type] = flag
  465. 1 self
  466. end
  467. # Include the fallback locale publish content, if specified locale content is not publish.
  468. #
  469. # Example
  470. #
  471. # @query = @stack.content_type('product').query
  472. # @query.include_fallback
  473. #
  474. # @return [Contentstack::Query]
  475. 1 def include_fallback(flag=true)
  476. 1 @query[:include_fallback] = flag
  477. 1 self
  478. end
  479. # Include Embedded Objects (Entries and Assets) along with entry/entries details.
  480. #
  481. # Example
  482. #
  483. # @query = @stack.content_type('product').query
  484. # @query.include_embedded_items
  485. #
  486. # @return [Contentstack::Query]
  487. 1 def include_embedded_items()
  488. @query[:include_embedded_items] = ['BASE']
  489. self
  490. end
  491. # Include objects in 'Draft' mode in response
  492. #
  493. # Example
  494. #
  495. # @query = @stack.content_type('product').query
  496. # @query.include_draft
  497. #
  498. # @return [Contentstack::Query]
  499. 1 def include_draft(flag=true)
  500. 1 @query[:include_draft] = flag
  501. 1 self
  502. end
  503. #
  504. # @return [Contentstack::Query]
  505. 1 def include(field_uids)
  506. 3 field_uids = [field_uids] if field_uids.class == String
  507. 3 @query[:include] ||= []
  508. 3 @query[:include] = @query[:include] | field_uids
  509. 3 self
  510. end
  511. # Include tags with which to search entries.
  512. #
  513. # @param [Array] tags_array Array of tags using which search must be performed
  514. #
  515. # Example
  516. #
  517. # @query = @stack.content_type('product').query
  518. # @query.tags(["tag1", "tag2"])
  519. #
  520. # @return [Contentstack::Query]
  521. 1 def tags(tags_array)
  522. 1 @query[:tags] = tags_array
  523. 1 self
  524. end
  525. # Execute query
  526. #
  527. # Example
  528. #
  529. # @query = @stack.content_type('product').query
  530. # @query.tags(["tag1", "tag2"])
  531. # .fetch
  532. #
  533. # @return [Contentstack::EntryCollection]
  534. 1 def fetch
  535. 48 entries = API.fetch_entries(@content_type, @query)
  536. 48 EntryCollection.new(entries, @content_type)
  537. end
  538. # Execute a Query and get the single matching object
  539. #
  540. # Example
  541. #
  542. # @query = @stack.content_type('product').query
  543. # @query.tags(["tag1", "tag2"])
  544. # .find_one
  545. #
  546. # @return [Contentstack::Entry]
  547. 1 def find_one
  548. limit 1
  549. fetch.first
  550. end
  551. 1 alias_method :find, :fetch
  552. 1 alias_method :in, :contained_in
  553. 1 alias_method :not_in, :not_contained_in
  554. 1 private
  555. 1 def add_query_hash(query_hash)
  556. 19 q = ActiveSupport::JSON.decode(@query[:query])
  557. 19 q.merge!(query_hash)
  558. 19 @query[:query] = ActiveSupport::JSON.encode(q)
  559. 19 self
  560. end
  561. 1 def concat_queries(queries)
  562. 2 this_queries = []
  563. 2 this_query = ActiveSupport::JSON.decode(@query[:query])
  564. 2 if this_query.keys.length > 0
  565. this_queries = [this_query]
  566. end
  567. 2 if queries.class == Array
  568. 2 queries.map do |query_object|
  569. 4 if query_object.class == Contentstack::Query && query_object.content_type == @content_type
  570. 4 q = ActiveSupport::JSON.decode(query_object.query[:query])
  571. 4 this_queries.push(q.symbolize_keys)
  572. end
  573. end
  574. end
  575. 2 this_queries
  576. end
  577. end
  578. end

lib/contentstack/region.rb

100.0% lines covered

4 relevant lines. 4 lines covered and 0 lines missed.
    
  1. 1 module Contentstack
  2. 1 class Region
  3. 1 EU='eu'
  4. 1 US='us'
  5. end
  6. end

lib/contentstack/sync_result.rb

73.91% lines covered

23 relevant lines. 17 lines covered and 6 lines missed.
    
  1. 1 require 'active_support/core_ext'
  2. 1 module Contentstack
  3. 1 class SyncResult
  4. 1 attr_reader :items
  5. 1 attr_reader :sync_token
  6. 1 attr_reader :pagination_token
  7. 1 attr_reader :total_count
  8. 1 attr_reader :skip
  9. 1 attr_reader :limit
  10. 1 def initialize(sync_result)
  11. 2 if sync_result.nil?
  12. @items = []
  13. @sync_token = nil
  14. @pagination_token = nil
  15. @total_count = 0
  16. @skip = 0
  17. @limit = 100
  18. else
  19. 2 @items = sync_result["items"] || []
  20. 2 @sync_token = sync_result["sync_token"] || nil
  21. 2 @pagination_token = sync_result["pagination_token"] || nil
  22. 2 @total_count = sync_result["total_count"] || 0
  23. 2 @skip = sync_result["skip"] || 0
  24. 2 @limit = sync_result["limit"] || 100
  25. end
  26. end
  27. end
  28. end

lib/contentstack/version.rb

100.0% lines covered

2 relevant lines. 2 lines covered and 0 lines missed.
    
  1. 1 module Contentstack
  2. 1 VERSION = "0.4.1"
  3. end

lib/util.rb

87.5% lines covered

56 relevant lines. 49 lines covered and 7 lines missed.
    
  1. 1 class Hash
  2. 1 def to_query(namespace = nil)
  3. 93 collect do |key, value|
  4. 365 value.to_query(namespace ? "#{namespace}[#{key}]" : key)
  5. end.sort * '&'
  6. end
  7. 1 def symbolize_keys
  8. 3628 new_hash = {}
  9. 3628 self.each do |key,value|
  10. 18741 if [Hash, Array].include?(value.class)
  11. 3709 new_hash[key.to_sym] = value.symbolize_keys
  12. else
  13. 15032 new_hash[key.to_sym] = value
  14. end
  15. end
  16. 3628 new_hash
  17. end
  18. end
  19. 1 class Array
  20. 1 def to_query(key)
  21. 19 prefix = "#{key}[]"
  22. 40 collect { |value| value.to_query(prefix) }.join '&'
  23. end
  24. 1 def symbolize_keys
  25. 1487 collect do |entry|
  26. 1197 if entry.class == Hash
  27. 958 entry.symbolize_keys
  28. else
  29. 239 entry
  30. end
  31. end
  32. end
  33. end
  34. 1 class String
  35. 1 def to_query(key)
  36. 201 require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
  37. 201 "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
  38. end
  39. 1 def to_param
  40. 222 to_s
  41. end
  42. end
  43. 1 class Symbol
  44. 1 def to_query(key)
  45. to_s.to_query(key)
  46. end
  47. 1 def to_param
  48. 335 to_s
  49. end
  50. end
  51. 1 class NilClass
  52. 1 def to_query(key)
  53. 80 to_s.to_query(key)
  54. end
  55. 1 def to_param
  56. to_s
  57. end
  58. end
  59. 1 class TrueClass
  60. 1 def to_query(key)
  61. to_s.to_query(key)
  62. end
  63. 1 def to_query(val)
  64. 18 "#{CGI.escape(val.to_param)}=#{CGI.escape(to_s)}"
  65. end
  66. end
  67. 1 class FalseClass
  68. 1 def to_query(key)
  69. to_s.to_query(key)
  70. end
  71. 1 def to_query(val)
  72. 40 "#{CGI.escape(val.to_param)}=#{CGI.escape(to_s)}"
  73. end
  74. end
  75. 1 class Integer
  76. 1 def to_query(key)
  77. to_s.to_query(key)
  78. end
  79. 1 def to_query(val)
  80. 97 "#{CGI.escape(val.to_param)}=#{CGI.escape(to_s)}"
  81. end
  82. end
  83. 1 class Numeric
  84. 1 def to_query(key)
  85. to_s.to_query(key)
  86. end
  87. 1 def to_query(val)
  88. "#{CGI.escape(val.to_param)}=#{CGI.escape(to_s)}"
  89. end
  90. end

spec/asset_spec.rb

100.0% lines covered

32 relevant lines. 32 lines covered and 0 lines missed.
    
  1. 1 require 'spec_helper'
  2. 1 require_relative '../lib/contentstack.rb'
  3. 1 describe Contentstack::Asset do
  4. 8 let(:client) { create_client }
  5. 1 it "has attribute called `uid`" do
  6. 1 @uid = "blt3ca1a3470787ba63"
  7. 1 @asset = client.asset(@uid).fetch
  8. 1 expect(@asset.uid).not_to be nil
  9. end
  10. 1 it "should match uid" do
  11. 1 @uid = "blt3ca1a3470787ba63"
  12. 1 @asset = client.asset(@uid).fetch
  13. 1 expect(@asset.uid).to eq @uid
  14. end
  15. 1 it "has attribute called `url`" do
  16. 1 @uid = "blt3ca1a3470787ba63"
  17. 1 @asset = client.asset(@uid).fetch
  18. 1 expect(@asset.url).not_to be nil
  19. end
  20. 1 it "has attribute called `tags`" do
  21. 1 @uid = "blt3ca1a3470787ba63"
  22. 1 @asset = client.asset(@uid).fetch
  23. 1 expect(@asset.tags).not_to be nil
  24. end
  25. 1 it "has attribute called `file_size`" do
  26. 1 @uid = "blt3ca1a3470787ba63"
  27. 1 @asset = client.asset(@uid).fetch
  28. 1 expect(@asset.file_size).not_to be nil
  29. end
  30. 1 it "has attribute called `filename`" do
  31. 1 @uid = "blt3ca1a3470787ba63"
  32. 1 @asset = client.asset(@uid).fetch
  33. 1 expect(@asset.filename).not_to be nil
  34. end
  35. 1 it "has attribute called `content_type`" do
  36. 1 @uid = "blt3ca1a3470787ba63"
  37. 1 @asset = client.asset(@uid).fetch
  38. 1 expect(@asset.content_type).not_to be nil
  39. end
  40. end

spec/content_type_spec.rb

100.0% lines covered

47 relevant lines. 47 lines covered and 0 lines missed.
    
  1. 1 require 'spec_helper'
  2. 1 require_relative '../lib/contentstack.rb'
  3. 1 describe Contentstack::ContentType do
  4. 8 let(:client) { create_client }
  5. 2 let(:eu_client) { create_client('DELIVERY_TOKEN', 'API_KEY', 'STACK_ENV', {region: Contentstack::Region::EU}) }
  6. 2 let(:custom_host_client) { create_client('DELIVERY_TOKEN', 'API_KEY', 'STACK_ENV', {host: "https://custom-cdn.contentstack.com"}) }
  7. 1 describe "Fetch data from API" do
  8. 1 it "has class as Contentstack::ContentType" do
  9. 1 @data = client.content_types.first
  10. 1 expect(@data.class).to eq Contentstack::ContentType
  11. end
  12. 1 it "has method called title with data" do
  13. 1 @data = client.content_types.first
  14. 1 expect(@data.title).not_to be nil
  15. end
  16. 1 it "has method called title with data from eu" do
  17. 1 @data = eu_client.content_types.first
  18. 1 expect(@data.title).not_to be nil
  19. end
  20. 1 it "has method called title with data from custom client" do
  21. 1 @data = custom_host_client.content_types.first
  22. 1 expect(@data.title).not_to be nil
  23. end
  24. 1 it "has method called uid with data" do
  25. 1 @data = client.content_types.first
  26. 1 expect(@data.uid).not_to be nil
  27. end
  28. 1 it "has method called created_at with data" do
  29. 1 @data = client.content_types.first
  30. 1 expect(@data.created_at).not_to be nil
  31. end
  32. 1 it "has method called updated_at with data" do
  33. 1 @data = client.content_types.first
  34. 1 expect(@data.updated_at).not_to be nil
  35. end
  36. 1 it "has method called attributes with data" do
  37. 1 @data = client.content_types.first
  38. 1 expect(@data.attributes).not_to be nil
  39. end
  40. 1 it "Should get content type from uid" do
  41. 1 @data = client.content_type("category").fetch
  42. 1 expect(@data.attributes).not_to be nil
  43. end
  44. end
  45. 1 describe "Initialized using class" do
  46. 1 before(:each) do
  47. 5 @data = Contentstack::ContentType.new({uid: "DummyUID"})
  48. end
  49. 1 it "has method called title without data" do
  50. 1 expect(@data.title).to be nil
  51. end
  52. 1 it "has method called uid with data" do
  53. 1 expect(@data.uid).not_to be nil
  54. end
  55. 1 it "has method called created_at without data" do
  56. 1 expect(@data.created_at).to be nil
  57. end
  58. 1 it "has method called updated_at without data" do
  59. 1 expect(@data.updated_at).to be nil
  60. end
  61. 1 it "has method called attributes without data" do
  62. 1 expect(@data.attributes).not_to be nil
  63. end
  64. end
  65. end

spec/contentstack_spec.rb

100.0% lines covered

23 relevant lines. 23 lines covered and 0 lines missed.
    
  1. 1 require 'spec_helper'
  2. 1 require_relative '../lib/contentstack.rb'
  3. 1 describe Contentstack do
  4. 2 let(:client) { create_client }
  5. 2 let(:eu_client) { create_client('DELIVERY_TOKEN_TOKEN', 'API_KEY', 'STACK_ENV', {region: Contentstack::Region::EU}) }
  6. 2 let(:custom_host_client) { create_client('DELIVERY_TOKEN_TOKEN', 'API_KEY', 'STACK_ENV', {host: "https://custom-cdn.contentstack.com"}) }
  7. 1 it "has a version number" do
  8. 1 expect(Contentstack::VERSION).not_to be nil
  9. end
  10. 1 it "has region data" do
  11. 1 expect(Contentstack::Region::EU).not_to be 'eu'
  12. 1 expect(Contentstack::Region::US).not_to be 'us'
  13. end
  14. 1 it "has default host and region" do
  15. 1 expect(client.region).to eq Contentstack::Region::US
  16. 1 expect(client.host).to eq 'https://cdn.contentstack.io'
  17. end
  18. 1 it "has custom region with region host" do
  19. 1 expect(eu_client.region).to eq Contentstack::Region::EU
  20. 1 expect(eu_client.host).to eq 'https://eu-cdn.contentstack.com'
  21. end
  22. 1 it "has custom host" do
  23. 1 expect(custom_host_client.host).to eq 'https://custom-cdn.contentstack.com'
  24. end
  25. 1 it "JSON to HTML" do
  26. 1 expect(Contentstack::json_to_html({}, ContentstackUtils::Model::Options.new())).to eq ''
  27. end
  28. 1 it "JSON to HTML" do
  29. 1 expect(Contentstack::render_content('', ContentstackUtils::Model::Options.new())).to eq ''
  30. end
  31. end

spec/entry_collection_spec.rb

100.0% lines covered

28 relevant lines. 28 lines covered and 0 lines missed.
    
  1. 1 require 'spec_helper'
  2. 1 require_relative '../lib/contentstack.rb'
  3. 1 describe Contentstack::EntryCollection do
  4. 7 let(:client) { create_client }
  5. 1 it "has no instance variable `count`" do
  6. 1 @data = client.content_type("category").query.fetch
  7. 1 @countdata = client.content_type("category").query.include_count.fetch
  8. 1 expect(@data.count).to be nil
  9. end
  10. 1 it "has instance variable `count`" do
  11. 1 @data = client.content_type("category").query.fetch
  12. 1 @countdata = client.content_type("category").query.include_count.fetch
  13. 1 expect(@countdata.count).not_to be nil
  14. end
  15. 1 it "has instance variable `entries`" do
  16. 1 @data = client.content_type("category").query.fetch
  17. 1 @countdata = client.content_type("category").query.include_count.fetch
  18. 1 expect(@data.entries).not_to be nil
  19. end
  20. 1 it "has the same entry using `first` method" do
  21. 1 @data = client.content_type("category").query.fetch
  22. 1 @countdata = client.content_type("category").query.include_count.fetch
  23. 1 expect(@data.entries[0].uid).to eq @data.first.uid
  24. end
  25. 1 it "has the same entry using `last` method" do
  26. 1 @data = client.content_type("category").query.fetch
  27. 1 @countdata = client.content_type("category").query.include_count.fetch
  28. 1 expect(@data.entries[-1].uid).to eq @data.last.uid
  29. end
  30. 1 it "has the same entry using `get` method" do
  31. 1 @data = client.content_type("category").query.fetch
  32. 1 @countdata = client.content_type("category").query.include_count.fetch
  33. 1 expect(@data.entries[3].uid).to eq @data.get(3).uid
  34. end
  35. end

spec/entry_spec.rb

100.0% lines covered

60 relevant lines. 60 lines covered and 0 lines missed.
    
  1. 1 require 'spec_helper'
  2. 1 require_relative '../lib/contentstack.rb'
  3. 1 describe Contentstack::Entry do
  4. 17 let(:client) { create_client }
  5. 16 let(:uid) { "blt05056a2f5e0ebf76" }
  6. 10 let(:category) {client.content_type("category").entry(uid)}
  7. 7 let(:product) {client.content_type("product").entry(uid)}
  8. 1 it "Contentstack::EntryCollection should have Contentstack::Entry instance" do
  9. 1 data = client.content_type("category").query.fetch
  10. 1 expect(data.class).to eq Contentstack::EntryCollection
  11. 1 expect(data.first.class).to eq Contentstack::Entry
  12. end
  13. 1 it "is an instance of Contentstack::Entry if single entry is fetched" do
  14. 1 data = category.fetch
  15. 1 expect(data.class).to eq Contentstack::Entry
  16. end
  17. 1 it 'has a method `get` to get attributes data' do
  18. 1 data = category.fetch
  19. 1 expect(data.get('uid')).to eq uid
  20. end
  21. 1 it "should set locale the in the request entry" do
  22. 1 data = category.locale('en-us')
  23. 1 expect(data.query[:locale]).to eq 'en-us'
  24. end
  25. 1 it "should get data using `only` method with string parameter" do
  26. 1 data = category.fetch
  27. 1 expect(data.fields[:title]).not_to be nil
  28. 1 expect(data.fields[:uid]).not_to be nil
  29. end
  30. 1 it "should get data using `only` method with array parameter" do
  31. 1 data = category.only(["title"]).fetch
  32. 1 expect(data.fields[:title]).not_to be nil
  33. 1 expect(data.fields[:uid]).not_to be nil
  34. end
  35. 1 it "should get data using `except` method with string parameter" do
  36. 1 data = category.except("category_tags").fetch
  37. 1 expect(data.fields[:category_tags]).to be nil
  38. end
  39. 1 it "should get data using `except` method with array parameter" do
  40. 1 data = category.except(["description"]).fetch
  41. 1 expect(data.fields[:description]).to be nil
  42. end
  43. 1 it "should get data using `only` method for reference fields" do
  44. 1 data = product.include_reference('categories').only("categories", ["title", "description"]).fetch
  45. 1 expect(data.fields[:categories][0][:title]).to eq "Smartphones"
  46. end
  47. 1 it "should get data using `except` method for reference fields" do
  48. 1 data = product.include_reference('categories').except("categories", "title").fetch
  49. 1 expect(data.fields[:categories][0][:title]).to eq 'Smartphones'
  50. end
  51. 1 it "should get data using `include_schema` method" do
  52. 1 data = category.include_schema.fetch
  53. 1 expect(data.schema).not_to be nil
  54. end
  55. 1 it "should get data using `include_owner` method" do
  56. 1 data = product.include_owner.fetch
  57. 1 expect(data.fields[:_owner]).not_to be nil
  58. end
  59. 1 it "should get data using `include_owner` method" do
  60. 1 data = product.include_fallback.fetch
  61. 1 expect(data.fields[:locale]).not_to be nil
  62. end
  63. 1 it "should get data using `include_content_type` method" do
  64. 1 data = category.include_content_type.fetch
  65. 1 expect(data.content_type).not_to be nil
  66. end
  67. 1 it "should get data using `include_reference` method" do
  68. 1 data = product.include_reference('categories').fetch
  69. 1 puts data.get("categories.title")
  70. 1 expect(data.fields[:categories][0][:title]).not_to be nil
  71. end
  72. 1 it "should get data using `include_embedded_items` method" do
  73. 1 data = product.include_embedded_items().fetch
  74. 1 puts data.get("categories.title")
  75. 1 expect(data.fields[:categories][0][:title]).not_to be nil
  76. end
  77. end

spec/query_spec.rb

100.0% lines covered

124 relevant lines. 124 lines covered and 0 lines missed.
    
  1. 1 require 'spec_helper'
  2. 1 require_relative '../lib/contentstack.rb'
  3. 1 describe Contentstack::Query do
  4. 37 let(:client) { create_client }
  5. 23 let(:category_query) {client.content_type("category").query}
  6. 15 let(:product_query) {client.content_type("product").query}
  7. 1 it "should get data using `where` method" do
  8. 1 data = category_query.where({title: "Electronics"}).fetch
  9. 1 expect(data.length).to eq 5
  10. end
  11. 1 it "should get data using `regex` method" do
  12. 1 data = category_query.regex("title", "App.*").fetch
  13. 1 expect(data.length).to eq 5
  14. end
  15. 1 it "should get data using `less_than` method" do
  16. 1 data = product_query.less_than("price", 150).fetch
  17. 1 expect(data.length).to eq 3
  18. end
  19. 1 it "should get data using `less_than_or_equal` method" do
  20. 1 data = product_query.less_than_or_equal("price", 166).fetch
  21. 1 expect(data.length).to eq 3
  22. end
  23. 1 it "should get data using `greater_than` method" do
  24. 1 data = product_query.greater_than("price", 120).fetch
  25. 1 expect(data.length).to eq 3
  26. end
  27. 1 it "should get data using `greater_than_or_equal` method" do
  28. 1 data = product_query.greater_than_or_equal("price", 166).fetch
  29. 1 expect(data.length).to eq 3
  30. end
  31. 1 it "should get data using `not_equal_to` method" do
  32. 1 data = product_query.not_equal_to("price", 166).fetch
  33. 1 expect(data.length).to eq 3
  34. end
  35. 1 it "should get data using `limit` method" do
  36. 1 data = category_query.limit(2).fetch
  37. 1 expect(data.length).to eq 5
  38. end
  39. 1 it "should get data using `skip` method" do
  40. 1 data = category_query.skip(5).fetch
  41. 1 expect(data.length).to eq 5
  42. end
  43. 1 it "should get data using `include_count` method" do
  44. 1 data = category_query.include_count.fetch
  45. 1 expect(data.count).not_to be nil
  46. 1 expect(data.count).to eq 5
  47. end
  48. 1 it "should get data using `only` method with string parameter" do
  49. 1 data = category_query.only("title").fetch
  50. 1 expect(data.first.fields[:title]).not_to be nil
  51. 1 expect(data.first.fields[:uid]).not_to be nil
  52. end
  53. 1 it "should get data using `only` method with array parameter" do
  54. 1 data = category_query.only(["title"]).fetch
  55. 1 expect(data.first.fields[:title]).not_to be nil
  56. 1 expect(data.first.fields[:uid]).not_to be nil
  57. end
  58. 1 it "should get data using `except` method with string parameter" do
  59. 1 data = category_query.except("category_tags").fetch
  60. 1 expect(data.first.fields[:category_tags]).to be nil
  61. end
  62. 1 it "should get data using `except` method with array parameter" do
  63. 1 data = category_query.except(["description"]).fetch
  64. 1 expect(data.first.fields[:description]).to be nil
  65. end
  66. 1 it "should get data using `tags` method" do
  67. 1 data = category_query.tags(["tag1"]).fetch
  68. 1 expect(data.length).to eq 5
  69. end
  70. 1 it "should get data using `contained_in` method" do
  71. 1 data = category_query.contained_in("title", ["Electronics", "Apparel"]).fetch
  72. 1 expect(data.length).to eq 5
  73. 1 expect(data.first.fields[:title]).to eq "Home & Appliances"
  74. 1 expect(data.last.fields[:title]).to eq "Headphones"
  75. end
  76. 1 it "should get data using `not_contained_in` method" do
  77. 1 data = category_query.not_contained_in("title", ["Electronics", "Apparel"]).fetch
  78. 1 expect(data.length).to eq 5
  79. end
  80. 1 it "should get data using `in` method" do
  81. 1 data = category_query.contained_in("title", ["Electronics", "Apparel"]).fetch
  82. 1 expect(data.length).to eq 5
  83. end
  84. 1 it "should get data using `not_in` method" do
  85. 1 data = category_query.not_contained_in("title", ["Electronics", "Apparel"]).fetch
  86. 1 expect(data.length).to eq 5
  87. end
  88. 1 it "should get data using `ascending` method" do
  89. 1 data = product_query.ascending("price").fetch
  90. 1 expect(data.first.fields[:title]).to eq "Motorola Moto X4"
  91. end
  92. 1 it "should get data using `descending` method" do
  93. 1 data = product_query.descending("price").fetch
  94. 1 expect(data.first.fields[:title]).to eq "Motorola Moto X4"
  95. end
  96. 1 it "should get data using `only` method for reference fields" do
  97. 1 data = product_query.include_reference('categories').only("categories", ["title", "description"]).fetch
  98. 1 expect(data.first.fields[:categories][0][:title]).to eq "Smartphones"
  99. end
  100. 1 it "should get data using `except` method for reference fields" do
  101. 1 data = product_query.include_reference('categories').except("categories", "title").fetch
  102. 1 expect(data.first.fields[:categories][0][:title]).to eq 'Smartphones'
  103. end
  104. 1 it "should get data using `include_schema` method" do
  105. 1 data = category_query.include_schema.fetch
  106. 1 expect(data.schema).not_to be nil
  107. end
  108. 1 it "should get data using `include_content_type` method" do
  109. 1 data = category_query.include_content_type.fetch
  110. 1 expect(data.content_type).not_to be nil
  111. end
  112. 1 it "should get data using `include_reference` method" do
  113. 1 data = product_query.include_reference('categories').fetch
  114. 1 puts data.first.get("categories.title")
  115. 1 expect(data.first.fields[:categories][0][:title]).not_to be nil
  116. end
  117. 1 it "should get data using `include_owner` method" do
  118. 1 data = product_query.include_owner.fetch
  119. 1 expect(data.first.fields[:_owner]).not_to be nil
  120. end
  121. 1 it "should get data using `include_owner` method" do
  122. 1 data = product_query.include_fallback.fetch
  123. 1 expect(data.first.fields[:locale]).not_to be nil
  124. end
  125. 1 it "should get data using `include_draft` method" do
  126. 1 data = category_query.include_draft.fetch
  127. 1 expect(data.length).to eq 5
  128. end
  129. 1 it "should get data using `search` method" do
  130. 1 data = category_query.search("App").fetch
  131. 1 expect(data.length).to eq 5
  132. end
  133. 1 it "should get data using `count` method" do
  134. 1 data = category_query.count
  135. 1 expect(data).to eq 5
  136. end
  137. 1 it "should get data using `exists` method" do
  138. 1 data = category_query.exists?('description').fetch
  139. 1 expect(data.length).to eq 5
  140. end
  141. 1 it "should get data using `not_exists` method" do
  142. 1 data = product_query.not_exists?('banner_image').fetch
  143. 1 expect(data.length).to eq 3
  144. end
  145. 1 it "should get data using `and` method" do
  146. 1 q1 = client.content_type("category").query.regex("title", "App.*")
  147. 1 q2 = client.content_type("category").query.where({"description"=>"Appliances Category"})
  148. 1 data = category_query.and([q1,q2]).fetch
  149. 1 expect(data.length).to eq 5
  150. end
  151. 1 it "should get data using `or` method" do
  152. 1 q1 = client.content_type("category").query.regex("title", "App.*")
  153. 1 q2 = client.content_type("category").query.regex("description", "App*")
  154. 1 data = category_query.or([q1,q2]).fetch
  155. 1 expect(data.length).to eq 5
  156. end
  157. 1 it "should set locale the in the request query" do
  158. 1 data = product_query.locale('en-us')
  159. 1 expect(data.query[:locale]).to eq 'en-us'
  160. end
  161. end

spec/sync_spec.rb

100.0% lines covered

22 relevant lines. 22 lines covered and 0 lines missed.
    
  1. 1 require 'spec_helper'
  2. 1 require_relative '../lib/contentstack.rb'
  3. 1 pagination_token = "token"
  4. 1 describe Contentstack::SyncResult do
  5. 3 let(:client) { create_client }
  6. 1 it "initial sync for Stack" do
  7. 1 @result = client.sync({init: true})
  8. 1 expect(@result.items.length).to be 100
  9. 1 expect(@result.skip).to be 0
  10. 1 expect(@result.limit).to be 100
  11. 1 expect(@result.total_count).to be 123
  12. 1 expect(@result.pagination_token).not_to be nil
  13. 1 expect(@result.sync_token).to be nil
  14. 1 pagination_token = @result.pagination_token
  15. end
  16. 1 it "next paginated sync for Stack" do
  17. 1 @result = client.sync({pagination_token: pagination_token})
  18. 1 expect(@result.items.length).to be 100
  19. 1 expect(@result.skip).to be 0
  20. 1 expect(@result.limit).to be 100
  21. 1 expect(@result.total_count).to be 123
  22. 1 expect(@result.pagination_token).not_to be nil
  23. 1 expect(@result.sync_token).to be nil
  24. end
  25. end