Class: Kontent::Ai::Delivery::DeliveryQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/delivery/client/delivery_query.rb

Overview

Responsible for executing REST requests to Kontent.ai.

Constant Summary collapse

ERROR_PREVIEW =
'Preview is enabled for the query, but the key is null. '\
'You can set the preview_key attribute of the query, or '\
'when you initialize the client. See '\
'https://github.com/kontent-ai/delivery-sdk-ruby#previewing-unpublished-content'.freeze
ERROR_PARAMS =
'Only filters may be passed in the .item or .items methods'\
'. See https://github.com/kontent-ai/delivery-sdk-ruby#filtering'.freeze
HEADER_WAIT_FOR_CONTENT =
'X-KC-Wait-For-Loading-New-Content'.freeze
HEADER_SDK_ID =
'X-KC-SDKID'.freeze
HEADER_SDK_VALUE =
'rubygems.org;kontent-ai-delivery;3.0.2'.freeze
HEADER_CONTINUATION =
'X-Continuation'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DeliveryQuery

Constructor. Queries should not be instantiated using the constructor, but using one of the Kontent::Ai::Delivery::DeliveryClient methods instead.

  • Args:

    • config (Hash) A hash in which each key automatically has its value paired with the corresponding attribute



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/delivery/client/delivery_query.rb', line 50

def initialize(config)
  @headers = {}

  # Map each hash value to attr with corresponding key
  # from https://stackoverflow.com/a/2681014/5656214
  config.each do |k, v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
  self.query_string = Kontent::Ai::Delivery::QueryParameters::QueryString.new
  return if config.fetch(:qp, nil).nil?

  # Query parameters were passed, parse and validate
  validate_params config.fetch(:qp)
end

Instance Attribute Details

#code_nameObject

Returns the value of attribute code_name.



20
21
22
# File 'lib/delivery/client/delivery_query.rb', line 20

def code_name
  @code_name
end

Returns the value of attribute content_link_url_resolver.



20
21
22
# File 'lib/delivery/client/delivery_query.rb', line 20

def content_link_url_resolver
  @content_link_url_resolver
end

#content_typeObject

Returns the value of attribute content_type.



20
21
22
# File 'lib/delivery/client/delivery_query.rb', line 20

def content_type
  @content_type
end

#default_rendition_presetObject

Returns the value of attribute default_rendition_preset.



20
21
22
# File 'lib/delivery/client/delivery_query.rb', line 20

def default_rendition_preset
  @default_rendition_preset
end

#inline_content_item_resolverObject

Returns the value of attribute inline_content_item_resolver.



20
21
22
# File 'lib/delivery/client/delivery_query.rb', line 20

def inline_content_item_resolver
  @inline_content_item_resolver
end

#preview_keyObject

Returns the value of attribute preview_key.



20
21
22
# File 'lib/delivery/client/delivery_query.rb', line 20

def preview_key
  @preview_key
end

#project_idObject

Returns the value of attribute project_id.



20
21
22
# File 'lib/delivery/client/delivery_query.rb', line 20

def project_id
  @project_id
end

#query_stringObject

Returns the value of attribute query_string.



20
21
22
# File 'lib/delivery/client/delivery_query.rb', line 20

def query_string
  @query_string
end

#query_typeObject

Returns the value of attribute query_type.



20
21
22
# File 'lib/delivery/client/delivery_query.rb', line 20

def query_type
  @query_type
end

#secure_keyObject

Returns the value of attribute secure_key.



20
21
22
# File 'lib/delivery/client/delivery_query.rb', line 20

def secure_key
  @secure_key
end

#use_previewObject

Returns the value of attribute use_preview.



20
21
22
# File 'lib/delivery/client/delivery_query.rb', line 20

def use_preview
  @use_preview
end

#with_retry_policyObject

Returns the value of attribute with_retry_policy.



20
21
22
# File 'lib/delivery/client/delivery_query.rb', line 20

def with_retry_policy
  @with_retry_policy
end

Instance Method Details

#continuation_exists?Boolean

Returns:

  • (Boolean)


256
257
258
# File 'lib/delivery/client/delivery_query.rb', line 256

def continuation_exists?
  !continuation_token.nil?
end

#continuation_tokenObject



260
261
262
# File 'lib/delivery/client/delivery_query.rb', line 260

def continuation_token
  @headers[HEADER_CONTINUATION]
end

#custom_headers(headers) ⇒ Object

Allows providing custom headers for client requests. See github.com/kontent-ai/delivery-sdk-ruby#providing-custom-headers

  • Args:

    • headers (Hash) A hash that corresponds to provided headers

  • Returns:

    • self



246
247
248
249
# File 'lib/delivery/client/delivery_query.rb', line 246

def custom_headers(headers)
  @custom_headers = headers
  self
end

#depth(value) ⇒ Object

Sets the ‘depth’ query string parameter to determine how many levels of linked content items should be returned. By default, only 1 level of depth is used. See kontent.ai/learn/reference/delivery-api/#tag/Linked-content-and-components/linked-content-depth

  • Args:

    • value (integer) Level of linked items to be returned

  • Returns:

    • self



208
209
210
211
# File 'lib/delivery/client/delivery_query.rb', line 208

def depth(value)
  query_string.set_param('depth', value) unless query_type.eql? Kontent::Ai::Delivery::QUERY_TYPE_ITEMS_FEED
  self
end

#elements(value) ⇒ Object

Sets the ‘elements’ query string parameter to limit the elements returned by the query. See kontent.ai/learn/reference/delivery-api/#tag/Projection

  • Args:

    • value (Array) A single string or array of strings specifying the desired elements, e.g. %w[price product_name image]

  • Returns:

    • self



193
194
195
196
# File 'lib/delivery/client/delivery_query.rb', line 193

def elements(value)
  query_string.set_param('elements', value)
  self
end

#execute {|resp| ... } ⇒ Object

Executes the REST request.

  • Returns:

    • Kontent::Ai::Delivery::Responses::ResponseBase or a class extending it

Yields:

  • (resp)


69
70
71
72
73
# File 'lib/delivery/client/delivery_query.rb', line 69

def execute
  resp = Kontent::Ai::Delivery::RequestManager.start self, headers
  yield resp if block_given?
  resp
end

#include_total_countObject

Enables the total_count attribute of the pagination object, which specifies the total number of items returned by the query regardless of paging. See docs.kontent.ai/reference/delivery-api#operation/list-content-items

  • Returns:

    • self



94
95
96
97
# File 'lib/delivery/client/delivery_query.rb', line 94

def include_total_count
  query_string.set_param('includeTotalCount', 1)
  self
end

#language(value) ⇒ Object

Sets the ‘language’ query string parameter. Language fallbacks will be used if untranslated content items are found. See kontent.ai/learn/tutorials/develop-apps/get-content/localized-content-items/

  • Args:

    • value (string) The code name of the desired language

  • Returns:

    • self



165
166
167
168
# File 'lib/delivery/client/delivery_query.rb', line 165

def language(value)
  query_string.set_param('language', value)
  self
end

#limit(value) ⇒ Object

Sets the ‘limit’ query string parameter for paging results, or just to return a specific number of content items. See kontent.ai/learn/reference/delivery-api/#operation/list-content-items

  • Args:

    • value (integer) The number of content items to return

  • Returns:

    • self



179
180
181
182
# File 'lib/delivery/client/delivery_query.rb', line 179

def limit(value)
  query_string.set_param('limit', value) unless query_type.eql? Kontent::Ai::Delivery::QUERY_TYPE_ITEMS_FEED
  self
end

#order_by(value, sort = '[asc]') ⇒ Object

Sets the ‘order’ query string parameter

  • Args:

    • value (string) The value to order by

    • sort (string) optional The direction of the order, surrounded by brackets. The default value is ‘[asc]’

  • Returns:

    • self



138
139
140
141
# File 'lib/delivery/client/delivery_query.rb', line 138

def order_by(value, sort = '[asc]')
  query_string.set_param('order', value + sort)
  self
end

#provide_urlObject

Uses Kontent::Ai::Delivery::Builders::UrlBuilder.provide_url to set the URL for the query. The UrlBuilder also validates the URL.

  • Raises:

    • UriFormatException if the URL is 65,519 characters or more

  • Returns:

    • string The full URL for this query



232
233
234
235
236
# File 'lib/delivery/client/delivery_query.rb', line 232

def provide_url
  @url = Kontent::Ai::Delivery::Builders::UrlBuilder.provide_url self if @url.nil?
  Kontent::Ai::Delivery::Builders::UrlBuilder.validate_url @url
  @url
end

#request_latest_contentObject

Allows the request to bypass caching and return the latest content directly from Kontent.ai. See github.com/kontent-ai/delivery-sdk-ruby#requesting-the-latest-content

  • Returns:

    • self



219
220
221
222
# File 'lib/delivery/client/delivery_query.rb', line 219

def request_latest_content
  @headers[HEADER_WAIT_FOR_CONTENT] = true
  self
end

#should_previewObject

Determines whether the query should use preview mode.

  • Returns:

    • boolean Whether preview mode should be used for the query

  • Raises:

    • StandardError if use_preview is true, but preview_key is nil

Raises:



82
83
84
85
86
# File 'lib/delivery/client/delivery_query.rb', line 82

def should_preview
  raise ERROR_PREVIEW if use_preview && preview_key.nil?

  use_preview && !preview_key.nil?
end

#skip(value) ⇒ Object

Sets the ‘skip’ query string parameter for paging results. See kontent.ai/learn/reference/delivery-api/#operation/list-content-items

  • Args:

    • value (integer) The number to skip by

  • Returns:

    • self



151
152
153
154
# File 'lib/delivery/client/delivery_query.rb', line 151

def skip(value)
  query_string.set_param('skip', value) unless query_type.eql? Kontent::Ai::Delivery::QUERY_TYPE_ITEMS_FEED
  self
end

#update_continuation(token) ⇒ Object



251
252
253
254
# File 'lib/delivery/client/delivery_query.rb', line 251

def update_continuation(token)
  @headers[HEADER_CONTINUATION] = token
  self
end

#url(url = nil) ⇒ Object

Setter for a custom URL.

  • Args:

    • url (string) optional Custom URL to use for the query

  • Returns:

    • self



40
41
42
43
# File 'lib/delivery/client/delivery_query.rb', line 40

def url(url = nil)
  @url = url unless url.nil?
  self
end

#with_default_rendition_preset(rendition) ⇒ Object



125
126
127
128
# File 'lib/delivery/client/delivery_query.rb', line 125

def with_default_rendition_preset(rendition)
  self.default_rendition_preset = rendition
  self
end

#with_inline_content_item_resolver(resolver) ⇒ Object

Sets an inline content itme to render content items and components in rich text. See github.com/kontent-ai/delivery-sdk-ruby#resolving-inline-content

  • Args:

    • resolver ( Kontent::Ai::Delivery::Resolvers::InlineContentItemResolver ) The resolver. Replaces a resolver registered during DeliveryClient instantiation, for this query only.

  • Returns:

    • self



120
121
122
123
# File 'lib/delivery/client/delivery_query.rb', line 120

def with_inline_content_item_resolver(resolver)
  self.inline_content_item_resolver = resolver
  self
end

Sets a content link resolver to render links contained in rich text. See github.com/kontent-ai/delivery-sdk-ruby#resolving-links

  • Args:

    • resolver ( Kontent::Ai::Delivery::Resolvers::ContentLinkResolver ) The resolver. Replaces a resolver registered during DeliveryClient instantiation, for this query only.

  • Returns:

    • self



107
108
109
110
# File 'lib/delivery/client/delivery_query.rb', line 107

def with_link_resolver(resolver)
  self.content_link_url_resolver = resolver
  self
end