Class: Kentico::Kontent::Delivery::QueryParameters::QueryString

Inherits:
Object
  • Object
show all
Defined in:
lib/delivery/query_parameters/query_string.rb

Overview

Represents the entire query string for a request to Delivery.

Instance Method Summary collapse

Constructor Details

#initializeQueryString

Returns a new instance of QueryString.



9
10
11
# File 'lib/delivery/query_parameters/query_string.rb', line 9

def initialize
  @params = []
end

Instance Method Details

#empty?Boolean

Checks whether there are any parameters defined.

  • Returns:

    • bool True if there are no parameters set.

Returns:

  • (Boolean)


60
61
62
# File 'lib/delivery/query_parameters/query_string.rb', line 60

def empty?
  @params.empty?
end

#param(key) ⇒ Object

Returns all parameters from the query string with a matching key.

  • Args:

    • key (string) Parameter key

  • Returns:

    • Object One or more Kentico::Kontent::Delivery::QueryParameters::ParameterBase objects



52
53
54
# File 'lib/delivery/query_parameters/query_string.rb', line 52

def param(key)
  @params.select { |p| p.key.eql? key }
end

#remove_param(key) ⇒ Object

Removes all parameters from the query string with a matching key.

  • Args:

    • key (string) Parameter key



41
42
43
# File 'lib/delivery/query_parameters/query_string.rb', line 41

def remove_param(key)
  @params.delete_if { |i| i.key.eql? key }
end

#set_param(param, values = '', operator = '') ⇒ Object

Adds a parameter to the query string

  • Args:

    • param (Object) Either a string representing the key for the parameter, or a complete Kentico::Kontent::Delivery::QueryParameters::ParameterBase object

    • values (string) A string or array of strings representing the values for the parameter

    • operator (string) Kentico Kontent filtering parameter, placed after the key, before the equal sign



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/delivery/query_parameters/query_string.rb', line 19

def set_param(param, values = '', operator = '')
  parameter_base =
    if param.is_a? String
      Kentico::Kontent::Delivery::QueryParameters::ParameterBase.new(
        param,
        operator,
        values
      )
    else
      param
    end
  # Ensure we have a ParameterBase object
  return unless parameter_base.respond_to? 'provide_query_string_parameter'

  remove_param parameter_base.key
  @params << parameter_base
end

#to_sObject

Generates a full query string based on the set parameters, with the required ‘?’ character at the start. Accomplished by calling the Kentico::Kontent::Delivery::QueryParameters::ParameterBase.provide_query_string_parameter method for each parameter.

  • Returns:

    • string A complete query string



71
72
73
# File 'lib/delivery/query_parameters/query_string.rb', line 71

def to_s
  '?' + @params.map(&:provide_query_string_parameter).join('&')
end