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

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

Instance Attribute Details

#code_nameObject

Returns the value of attribute code_name.



16
17
18
# File 'lib/delivery/client/delivery_query.rb', line 16

def code_name
  @code_name
end

Returns the value of attribute content_link_url_resolver.



16
17
18
# File 'lib/delivery/client/delivery_query.rb', line 16

def content_link_url_resolver
  @content_link_url_resolver
end

#content_typeObject

Returns the value of attribute content_type.



16
17
18
# File 'lib/delivery/client/delivery_query.rb', line 16

def content_type
  @content_type
end

#inline_content_item_resolverObject

Returns the value of attribute inline_content_item_resolver.



16
17
18
# File 'lib/delivery/client/delivery_query.rb', line 16

def inline_content_item_resolver
  @inline_content_item_resolver
end

#preview_keyObject

Returns the value of attribute preview_key.



16
17
18
# File 'lib/delivery/client/delivery_query.rb', line 16

def preview_key
  @preview_key
end

#project_idObject

Returns the value of attribute project_id.



16
17
18
# File 'lib/delivery/client/delivery_query.rb', line 16

def project_id
  @project_id
end

#query_stringObject

Returns the value of attribute query_string.



16
17
18
# File 'lib/delivery/client/delivery_query.rb', line 16

def query_string
  @query_string
end

#query_typeObject

Returns the value of attribute query_type.



16
17
18
# File 'lib/delivery/client/delivery_query.rb', line 16

def query_type
  @query_type
end

#secure_keyObject

Returns the value of attribute secure_key.



16
17
18
# File 'lib/delivery/client/delivery_query.rb', line 16

def secure_key
  @secure_key
end

#use_previewObject

Returns the value of attribute use_preview.



16
17
18
# File 'lib/delivery/client/delivery_query.rb', line 16

def use_preview
  @use_preview
end

#with_retry_policyObject

Returns the value of attribute with_retry_policy.



16
17
18
# File 'lib/delivery/client/delivery_query.rb', line 16

def with_retry_policy
  @with_retry_policy
end

Instance Method Details

#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



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

def depth(value)
  query_string.set_param('depth', value)
  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



177
178
179
180
# File 'lib/delivery/client/delivery_query.rb', line 177

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)


64
65
66
67
68
69
70
71
72
73
# 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) ⇒ 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



149
150
151
152
# File 'lib/delivery/client/delivery_query.rb', line 149

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



163
164
165
166
# 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]') ⇒ 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



122
123
124
125
# 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_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



216
217
218
219
220
# 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_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



203
204
205
206
# 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_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 developer.kenticocloud.com/v1/reference#listing-response-paging

  • Args:

    • value (integer) The number to skip by

  • Returns:

    • self



135
136
137
138
# File 'lib/delivery/client/delivery_query.rb', line 135

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

#url(url = nil) ⇒ Object

Setter for a custom URL.

  • Args:

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

  • Returns:

    • self



35
36
37
38
# 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) ⇒ 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



109
110
111
112
# File 'lib/delivery/client/delivery_query.rb', line 109

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



96
97
98
99
# File 'lib/delivery/client/delivery_query.rb', line 96

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