class Kentico::Kontent::Delivery::DeliveryQuery

Responsible for executing REST requests to Kentico Kontent.

Constants

ERROR_PARAMS
ERROR_PREVIEW

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 45
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

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 192
def depth(value)
  query_string.set_param('depth', value)
  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 177
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 64
def execute
  headers = @headers.clone
  headers['X-KC-SDKID'] = provide_sdk_header
  headers['Authorization'] = "Bearer #{preview_key}" if should_preview
  headers['Authorization'] = "Bearer #{secure_key}" if !should_preview && secure_key

  resp = Kentico::Kontent::Delivery::RequestManager.start self, headers
  yield resp if block_given?
  resp
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 149
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 163
def limit(value)
  query_string.set_param('limit', value)
  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 122
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 216
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 203
def request_latest_content
  @headers['X-KC-Wait-For-Loading-New-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 82
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 135
def skip(value)
  query_string.set_param('skip', value)
  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 35
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 109
def with_inline_content_item_resolver(resolver)
  self.inline_content_item_resolver = resolver
  self
end

Private Instance Methods

provide_sdk_header() click to toggle source
# File lib/delivery/client/delivery_query.rb, line 243
def provide_sdk_header
  "rubygems.org;kontent-delivery-sdk-ruby;#{Kentico::Kontent::Delivery::VERSION}"
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 229
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