Class: Kentico::Kontent::Delivery::DeliveryQuery

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

Overview

Responsible for executing REST requests to Kentico Kontent.

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/Kentico/kontent-delivery-sdk-ruby#previewing-unpublished-content'.freeze
ERROR_PARAMS =
'Only filters may be passed in the .item or .items methods'\
'. See https://github.com/Kentico/kontent-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_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 Kentico::Kontent::Delivery::DeliveryClient methods instead.

  • Args:

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



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

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 = Kentico::Kontent::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.



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

def code_name
  @code_name
end

Returns the value of attribute content_link_url_resolver.



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

def content_link_url_resolver
  @content_link_url_resolver
end

#content_typeObject

Returns the value of attribute content_type.



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

def content_type
  @content_type
end

#inline_content_item_resolverObject

Returns the value of attribute inline_content_item_resolver.



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

def inline_content_item_resolver
  @inline_content_item_resolver
end

#preview_keyObject

Returns the value of attribute preview_key.



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

def preview_key
  @preview_key
end

#project_idObject

Returns the value of attribute project_id.



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

def project_id
  @project_id
end

#query_stringObject

Returns the value of attribute query_string.



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

def query_string
  @query_string
end

#query_typeObject

Returns the value of attribute query_type.



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

def query_type
  @query_type
end

#secure_keyObject

Returns the value of attribute secure_key.



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

def secure_key
  @secure_key
end

#use_previewObject

Returns the value of attribute use_preview.



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

def use_preview
  @use_preview
end

#with_retry_policyObject

Returns the value of attribute with_retry_policy.



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

def with_retry_policy
  @with_retry_policy
end

Instance Method Details

#continuation_exists?Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/delivery/client/delivery_query.rb', line 249

def continuation_exists?
  !continuation_token.nil?
end

#continuation_tokenObject



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

def continuation_token
  @headers[HEADER_CONTINUATION]
end

#custom_headers(headers) ⇒ Object

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

  • Args:

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

  • Returns:

    • self



239
240
241
242
# File 'lib/delivery/client/delivery_query.rb', line 239

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 developer.kenticocloud.com/v1/reference#linked-content

  • Args:

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

  • Returns:

    • self



201
202
203
204
# File 'lib/delivery/client/delivery_query.rb', line 201

def depth(value)
  query_string.set_param('depth', value) unless query_type.eql? Kentico::Kontent::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 developer.kenticocloud.com/v1/reference#projection

  • Args:

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

  • Returns:

    • self



186
187
188
189
# File 'lib/delivery/client/delivery_query.rb', line 186

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

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

Executes the REST request.

  • Returns:

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

Yields:

  • (resp)


67
68
69
70
71
# File 'lib/delivery/client/delivery_query.rb', line 67

def execute
  resp = Kentico::Kontent::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



92
93
94
95
# File 'lib/delivery/client/delivery_query.rb', line 92

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 developer.kenticocloud.com/docs/localization#section-getting-localized-content-items

  • Args:

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

  • Returns:

    • self



158
159
160
161
# File 'lib/delivery/client/delivery_query.rb', line 158

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 developer.kenticocloud.com/v1/reference#listing-response-paging

  • Args:

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

  • Returns:

    • self



172
173
174
175
# File 'lib/delivery/client/delivery_query.rb', line 172

def limit(value)
  query_string.set_param('limit', value) unless query_type.eql? Kentico::Kontent::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



131
132
133
134
# File 'lib/delivery/client/delivery_query.rb', line 131

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

#provide_urlObject

Uses Kentico::Kontent::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



225
226
227
228
229
# File 'lib/delivery/client/delivery_query.rb', line 225

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

#request_latest_contentObject

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

  • Returns:

    • self



212
213
214
215
# File 'lib/delivery/client/delivery_query.rb', line 212

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:



80
81
82
83
84
# File 'lib/delivery/client/delivery_query.rb', line 80

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 developer.kenticocloud.com/v1/reference#listing-response-paging

  • Args:

    • value (integer) The number to skip by

  • Returns:

    • self



144
145
146
147
# File 'lib/delivery/client/delivery_query.rb', line 144

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

#update_continuation(token) ⇒ Object



244
245
246
247
# File 'lib/delivery/client/delivery_query.rb', line 244

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



38
39
40
41
# File 'lib/delivery/client/delivery_query.rb', line 38

def url(url = nil)
  @url = url unless url.nil?
  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/Kentico/kontent-delivery-sdk-ruby#resolving-inline-content

  • Args:

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

  • Returns:

    • self



118
119
120
121
# File 'lib/delivery/client/delivery_query.rb', line 118

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/Kentico/kontent-delivery-sdk-ruby#resolving-links

  • Args:

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

  • Returns:

    • self



105
106
107
108
# File 'lib/delivery/client/delivery_query.rb', line 105

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