class Kentico::Kontent::Delivery::DeliveryQuery

Responsible for executing REST requests to Kentico Kontent.

Constants

ERROR_PARAMS
ERROR_PREVIEW
HEADER_CONTINUATION
HEADER_SDK_ID
HEADER_WAIT_FOR_CONTENT

Attributes

code_name[RW]
content_type[RW]
inline_content_item_resolver[RW]
preview_key[RW]
project_id[RW]
query_string[RW]
query_type[RW]
secure_key[RW]
use_preview[RW]
with_retry_policy[RW]

Public Class Methods

new(config) click to toggle source

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

# 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

Public Instance Methods

continuation_exists?() click to toggle source
# File lib/delivery/client/delivery_query.rb, line 249
def continuation_exists?
  !continuation_token.nil?
end
continuation_token() click to toggle source
# File lib/delivery/client/delivery_query.rb, line 253
def continuation_token
  @headers[HEADER_CONTINUATION]
end
custom_headers(headers) click to toggle source

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

# File lib/delivery/client/delivery_query.rb, line 239
def custom_headers(headers)
  @custom_headers = headers
  self
end
depth(value) click to toggle source

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

# 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) click to toggle source

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

# File lib/delivery/client/delivery_query.rb, line 186
def elements(value)
  query_string.set_param('elements', value)
  self
end
execute() { |resp| ... } click to toggle source

Executes the REST request.

# 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_count() click to toggle source

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

# File lib/delivery/client/delivery_query.rb, line 92
def include_total_count
  query_string.set_param('includeTotalCount', 1)
  self
end
language(value) click to toggle source

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

# File lib/delivery/client/delivery_query.rb, line 158
def language(value)
  query_string.set_param('language', value)
  self
end
limit(value) click to toggle source

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

# 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]') click to toggle source

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

# 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_url() click to toggle source

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

# 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_content() click to toggle source

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

# File lib/delivery/client/delivery_query.rb, line 212
def request_latest_content
  @headers[HEADER_WAIT_FOR_CONTENT] = true
  self
end
should_preview() click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source
# File lib/delivery/client/delivery_query.rb, line 244
def update_continuation(token)
  @headers[HEADER_CONTINUATION] = token
  self
end
url(url = nil) click to toggle source

Setter for a custom URL.

  • Args:

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

  • Returns:

    • self

# 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) click to toggle source

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

# File lib/delivery/client/delivery_query.rb, line 118
def with_inline_content_item_resolver(resolver)
  self.inline_content_item_resolver = resolver
  self
end

Private Instance Methods

headers() click to toggle source

Returns request headers that are extended with custom headers. Custom headers do not override existing headers.

  • Returns

    • Hash

# File lib/delivery/client/delivery_query.rb, line 264
def headers
  headers = @headers.clone
  headers[HEADER_SDK_ID] = provide_sdk_header
  headers['Authorization'] = "Bearer #{preview_key}" if should_preview
  headers['Authorization'] = "Bearer #{secure_key}" if !should_preview && secure_key

  if @custom_headers
    headers.merge!(@custom_headers) { |key, v1, v2| v1 }
  end

  headers
end
provide_sdk_header() click to toggle source
# File lib/delivery/client/delivery_query.rb, line 296
def provide_sdk_header
  'rubygems.org;kontent-delivery-sdk-ruby;'
end
validate_params(query_parameters) click to toggle source

Initializes the query_string attribute with the passed array of Kentico::Kontent::Delivery::QueryParameters::Filter objects.

  • Raises:

    • ArgumentError if one the passed objects is not a Filter

# File lib/delivery/client/delivery_query.rb, line 282
def validate_params(query_parameters)
  params = if query_parameters.is_a? Array
            query_parameters
          else
            [query_parameters]
          end
  params.each do |p|
    query_string.set_param p
    unless p.is_a? Kentico::Kontent::Delivery::QueryParameters::Filter
      raise ArgumentError, ERROR_PARAMS
    end
  end
end