delivery-sdk-ruby 0.16.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,13 @@
1
1
  module KenticoCloud
2
2
  module Delivery
3
- # Holds pagination data from a DeliveryItemListingResponse
3
+ # Holds pagination data from listing responses
4
4
  class Pagination
5
5
  attr_accessor :skip, :limit, :count, :next_page
6
6
 
7
+ # Constructor.
8
+ #
9
+ # * *Args*:
10
+ # - *json* (+JSON+) The 'pagination' node of a listing reponse's JSON object
7
11
  def initialize(json)
8
12
  self.skip = json['skip']
9
13
  self.limit = json['limit']
@@ -1,7 +1,10 @@
1
1
  module KenticoCloud
2
2
  module Delivery
3
- # JSON data of a taxonomy group parsed as OpenStruct objects for dynamic use
4
3
  class TaxonomyGroup
4
+ # Parses the 'terms' JSON node as a dynamic OpenStruct object.
5
+ #
6
+ # * *Returns*:
7
+ # - +OpenStruct+ The terms of the taxonomy group as a dynamic object
5
8
  def terms
6
9
  @terms unless @terms.nil?
7
10
  @terms = JSON.parse(
@@ -10,6 +13,10 @@ module KenticoCloud
10
13
  )
11
14
  end
12
15
 
16
+ # Parses the 'system' JSON node as a dynamic OpenStruct object.
17
+ #
18
+ # * *Returns*:
19
+ # - +OpenStruct+ The system properties of the taxonomy group
13
20
  def system
14
21
  @system unless @system.nil?
15
22
  @system = JSON.parse(
@@ -18,6 +25,10 @@ module KenticoCloud
18
25
  )
19
26
  end
20
27
 
28
+ # Constructor.
29
+ #
30
+ # * *Args*:
31
+ # - *json* (+JSON+) A JSON node representing a taxonomy group
21
32
  def initialize(source)
22
33
  @source = source
23
34
  end
@@ -4,7 +4,14 @@ module KenticoCloud
4
4
  module Delivery
5
5
  module QueryParameters
6
6
  # Provides the base class for filter implementations.
7
+ # See https://developer.kenticocloud.com/v1/reference#content-filtering
7
8
  class Filter < ParameterBase
9
+ # Constructor.
10
+ #
11
+ # * *Args*:
12
+ # - *key* (+string+) The field to filter upon
13
+ # - *operator* (+string+) The Kentico Cloud filter being applied to the field, in brackets
14
+ # - *values* (+Object+) One or more values which will appear as the value of the query string parameter
8
15
  def initialize(key, operator, values)
9
16
  super(key, operator, values)
10
17
  end
@@ -19,6 +26,12 @@ class String
19
26
  # element or system attribute has a value that contains all the specified
20
27
  # values. This filter is applicable to array values only, such as sitemap
21
28
  # location or value of Linked Items, Taxonomy and Multiple choice content elements.
29
+ #
30
+ # * *Args*:
31
+ # - +Object+ One or more objects representing the values that must appear in the field
32
+ #
33
+ # * *Returns*:
34
+ # - KenticoCloud::Delivery::QueryParameters::Filter
22
35
  def all(*args)
23
36
  KenticoCloud::Delivery::QueryParameters::Filter.new(self, '[all]', *args)
24
37
  end
@@ -27,6 +40,12 @@ class String
27
40
  # element or system attribute has a value that contains any the specified
28
41
  # values. This filter is applicable to array values only, such as sitemap
29
42
  # location or value of Linked Items, Taxonomy and Multiple choice content elements.
43
+ #
44
+ # * *Args*:
45
+ # - +Object+ One or more objects representing the values that may appear in the field
46
+ #
47
+ # * *Returns*:
48
+ # - KenticoCloud::Delivery::QueryParameters::Filter
30
49
  def any(*args)
31
50
  KenticoCloud::Delivery::QueryParameters::Filter.new(self, '[any]', *args)
32
51
  end
@@ -35,12 +54,24 @@ class String
35
54
  # or system attribute has a value that contains the specified value.
36
55
  # This filter is applicable to array values only, such as sitemap location or value
37
56
  # of Linked Items, Taxonomy and Multiple choice content elements.
57
+ #
58
+ # * *Args*:
59
+ # - +Object+ An object representing the value that must appear in the field
60
+ #
61
+ # * *Returns*:
62
+ # - KenticoCloud::Delivery::QueryParameters::Filter
38
63
  def contains(*args)
39
64
  KenticoCloud::Delivery::QueryParameters::Filter.new(self, '[contains]', *args)
40
65
  end
41
66
 
42
67
  # Represents a filter that matches a content item if the specified
43
68
  # content element or system attribute has the specified value.
69
+ #
70
+ # * *Args*:
71
+ # - +Object+ An object representing the value that must equal the value in the field
72
+ #
73
+ # * *Returns*:
74
+ # - KenticoCloud::Delivery::QueryParameters::Filter
44
75
  def eq(*args)
45
76
  KenticoCloud::Delivery::QueryParameters::Filter.new(self, '', *args)
46
77
  end
@@ -48,6 +79,12 @@ class String
48
79
  # Represents a filter that matches a content item if the specified content
49
80
  # element or system attribute has a value that is greater than the
50
81
  # specified value.
82
+ #
83
+ # * *Args*:
84
+ # - +Object+ An object representing the lowest possible value of the field, non-inclusive
85
+ #
86
+ # * *Returns*:
87
+ # - KenticoCloud::Delivery::QueryParameters::Filter
51
88
  def gt(*args)
52
89
  KenticoCloud::Delivery::QueryParameters::Filter.new(self, '[gt]', *args)
53
90
  end
@@ -55,6 +92,12 @@ class String
55
92
  # Represents a filter that matches a content item if the specified content
56
93
  # element or system attribute has a value that is greater than or equal to
57
94
  # the specified value.
95
+ #
96
+ # * *Args*:
97
+ # - +Object+ An object representing the lowest possible value of the field
98
+ #
99
+ # * *Returns*:
100
+ # - KenticoCloud::Delivery::QueryParameters::Filter
58
101
  def gt_or_eq(*args)
59
102
  KenticoCloud::Delivery::QueryParameters::Filter.new(self, '[gte]', *args)
60
103
  end
@@ -62,6 +105,12 @@ class String
62
105
  # Represents a filter that matches a content item if the specified
63
106
  # content element or system attribute has a value that matches a
64
107
  # value in the specified list.
108
+ #
109
+ # * *Args*:
110
+ # - +Object+ One or more objects representing the required values of the field
111
+ #
112
+ # * *Returns*:
113
+ # - KenticoCloud::Delivery::QueryParameters::Filter
65
114
  def in(*args)
66
115
  KenticoCloud::Delivery::QueryParameters::Filter.new(self, '[in]', *args)
67
116
  end
@@ -69,6 +118,12 @@ class String
69
118
  # Represents a filter that matches a content item if the specified content
70
119
  # element or system attribute has a value that is less than the
71
120
  # specified value.
121
+ #
122
+ # * *Args*:
123
+ # - +Object+ An object representing the highest possible value of the field, non-inclusive
124
+ #
125
+ # * *Returns*:
126
+ # - KenticoCloud::Delivery::QueryParameters::Filter
72
127
  def lt(*args)
73
128
  KenticoCloud::Delivery::QueryParameters::Filter.new(self, '[lt]', *args)
74
129
  end
@@ -76,6 +131,12 @@ class String
76
131
  # Represents a filter that matches a content item if the specified content
77
132
  # element or system attribute has a value that is less than or equal to
78
133
  # the specified value.
134
+ #
135
+ # * *Args*:
136
+ # - +Object+ An object representing the highest possible value of the field
137
+ #
138
+ # * *Returns*:
139
+ # - KenticoCloud::Delivery::QueryParameters::Filter
79
140
  def lt_or_eq(*args)
80
141
  KenticoCloud::Delivery::QueryParameters::Filter.new(self, '[lte]', *args)
81
142
  end
@@ -83,6 +144,12 @@ class String
83
144
  # Represents a filter that matches a content item if the specified
84
145
  # content element or system attribute has a value that falls within
85
146
  # the specified range of values (both inclusive).
147
+ #
148
+ # * *Args*:
149
+ # - +Object+ An object representing the lowest and highest possible values of the field
150
+ #
151
+ # * *Returns*:
152
+ # - KenticoCloud::Delivery::QueryParameters::Filter
86
153
  def range(*args)
87
154
  KenticoCloud::Delivery::QueryParameters::Filter.new(self, '[range]', *args)
88
155
  end
@@ -1,14 +1,18 @@
1
1
  module KenticoCloud
2
2
  module Delivery
3
- # Contains static methods for adding parameters to a DeliveryQuery
4
- # as well as the Filter class.
5
3
  module QueryParameters
6
- # Base class for all parameters added to a DeliveryQuery using the
7
- # .parameters method. All parameters appear in the query string.
4
+ # Base class for all parameters added to a DeliveryQuery. All
5
+ # QueryParameters will appear in the query string.
8
6
  class ParameterBase
9
7
  attr_accessor :key
10
8
  SEPARATOR = CGI.escape(',')
11
9
 
10
+ # Constructor.
11
+ #
12
+ # * *Args*:
13
+ # - *key* (+string+) The field to filter upon
14
+ # - *operator* (+string+) The Kentico Cloud filter being applied to the field, in brackets
15
+ # - *values* (+Object+) One or more values which will appear as the value of the query string parameter
12
16
  def initialize(key, operator, values)
13
17
  self.key = key
14
18
  values = [values] unless values.respond_to? :each
@@ -16,6 +20,12 @@ module KenticoCloud
16
20
  @operator = operator
17
21
  end
18
22
 
23
+ # Converts the object into a valid query string parameter for use in
24
+ # a request to Delivery. The key, operator, and values are all escaped
25
+ # and if there are multiple values, they are joined with commas.
26
+ #
27
+ # * *Returns*:
28
+ # - +string+ A query string parameter without any additional characters (e.g. '&')
19
29
  def provide_query_string_parameter
20
30
  escaped_values = []
21
31
  @values.each { |n| escaped_values << CGI.escape(n.to_s) }
@@ -3,15 +3,18 @@ require 'delivery/query_parameters/parameter_base'
3
3
  module KenticoCloud
4
4
  module Delivery
5
5
  module QueryParameters
6
+ # Represents the entire query string for a request to Delivery.
6
7
  class QueryString
7
8
  def initialize
8
9
  @params = []
9
10
  end
10
11
 
11
12
  # Adds a parameter to the query string
12
- # @param [String] param Either a string representing the key for the parameter, or a complete ParameterBase object
13
- # @param [String] values A string or array of strings representing the values for the parameter
14
- # @param [String] operator Kentico Cloud filtering parameter, placed after the key, before the equal sign
13
+ #
14
+ # * *Args*:
15
+ # - *param* (+Object+) Either a string representing the key for the parameter, or a complete KenticoCloud::Delivery::QueryParameters::ParameterBase object
16
+ # - *values* (+string+) A string or array of strings representing the values for the parameter
17
+ # - *operator* (+string+) Kentico Cloud filtering parameter, placed after the key, before the equal sign
15
18
  def set_param(param, values = '', operator = '')
16
19
  parameter_base =
17
20
  if param.is_a? String
@@ -30,18 +33,40 @@ module KenticoCloud
30
33
  @params << parameter_base
31
34
  end
32
35
 
36
+ # Removes all parameters from the query string with a matching key.
37
+ #
38
+ # * *Args*:
39
+ # - *key* (+string+) Parameter key
33
40
  def remove_param(key)
34
41
  @params.delete_if { |i| i.key.eql? key }
35
42
  end
36
43
 
44
+ # Returns all parameters from the query string with a matching key.
45
+ #
46
+ # * *Args*:
47
+ # - *key* (+string+) Parameter key
48
+ #
49
+ # * *Returns*:
50
+ # - +Object+ One or more KenticoCloud::Delivery::QueryParameters::ParameterBase objects
37
51
  def param(key)
38
52
  @params.select { |p| p.key.eql? key }
39
53
  end
40
54
 
55
+ # Checks whether there are any parameters defined.
56
+ #
57
+ # * *Returns*:
58
+ # - +bool+ True if there are no parameters set.
41
59
  def empty?
42
60
  @params.empty?
43
61
  end
44
62
 
63
+ # Generates a full query string based on the set parameters, with the
64
+ # required '?' character at the start. Accomplished by calling the
65
+ # KenticoCloud::Delivery::QueryParameters::ParameterBase.provide_query_string_parameter
66
+ # method for each parameter.
67
+ #
68
+ # * *Returns*:
69
+ # - +string+ A complete query string
45
70
  def to_s
46
71
  '?' + @params.map(&:provide_query_string_parameter).join('&')
47
72
  end
@@ -4,15 +4,21 @@ module KenticoCloud
4
4
  module Delivery
5
5
  module Resolvers
6
6
  # Locates <a data-item-id=""> tags in content and calls a user-defined method
7
- # to supply the href for content item links
7
+ # to supply the href for content item links.
8
+ # See https://github.com/Kentico/delivery-sdk-ruby#resolving-links
8
9
  class ContentLinkResolver
9
10
  def initialize(callback = nil)
10
11
  @callback = callback
11
12
  end
12
13
 
13
- # Resolves all links in the content
14
- # @param [String] content The string value stored in the element
15
- # @param [Array] links The collection of source links from the JSON response
14
+ # Resolves all links in the content.
15
+ #
16
+ # * *Args*:
17
+ # - *content* (+string+) The string value stored in the element
18
+ # - *links* (+Array+) The collection of links from an element's 'links' JSON node
19
+ #
20
+ # * *Returns*:
21
+ # - +string+ The original content passed, with all links resolved
16
22
  def resolve(content, links)
17
23
  doc = Nokogiri::HTML.parse(content).xpath('//body')
18
24
  links = links.map { |link| ContentLink.new link }
@@ -26,7 +32,14 @@ module KenticoCloud
26
32
 
27
33
  # Accepts a tag found in the content and tries to locate matching
28
34
  # source link from JSON response. If found, resolves URL and returns
29
- # the tag with generated HREF
35
+ # the tag with generated HREF.
36
+ #
37
+ # * *Args*:
38
+ # - *tag* (+string+) A <a data-item-id=""> tag found in the content
39
+ # - *links* (+Array+) The collection of links from an element's 'links' JSON node, converted to KenticoCloud::Delivery::Resolvers::ContentLink objects
40
+ #
41
+ # * *Returns*:
42
+ # - +string+ The <a data-item-id=""> tag with an HREF generated by the +provide_url+ method
30
43
  def resolve_tag(tag, links)
31
44
  matches = links.select { |link| link.id == tag['data-item-id'].to_s }
32
45
  url = provide_url matches
@@ -34,7 +47,14 @@ module KenticoCloud
34
47
  tag
35
48
  end
36
49
 
37
- # Returns a url if a link was found in source links, otherwise returns 404
50
+ # Uses the +resolve_link+ method to generate a URL for a ContentLink
51
+ # object.
52
+ #
53
+ # * *Args*:
54
+ # - *matches* (+Array+) The ContentLink objects with an ID matching a particular <a data-item-id=""> tag
55
+ #
56
+ # * *Returns*:
57
+ # - +string+ A url if a link was found in source links, otherwise '/404'
38
58
  def provide_url(matches)
39
59
  if !matches.empty?
40
60
  if @callback.nil?
@@ -52,6 +72,10 @@ module KenticoCloud
52
72
  class ContentLink
53
73
  attr_accessor :code_name, :type, :url_slug, :id
54
74
 
75
+ # Constructor.
76
+ #
77
+ # * *Args*:
78
+ # - *link* (+JSON+) One link from an element's 'links' JSON node
55
79
  def initialize(link)
56
80
  self.id = link[0]
57
81
  self.code_name = link[1]['codename']
@@ -3,16 +3,22 @@ require 'nokogiri'
3
3
  module KenticoCloud
4
4
  module Delivery
5
5
  module Resolvers
6
- # Locates <object data-type="item"> tags in content and calls a user-defined method
7
- # to supply the HTML output for the content item
6
+ # Locates <object data-type="item"> tags in content and calls a user-defined
7
+ # method to supply the output for the content item.
8
+ # See https://github.com/Kentico/delivery-sdk-ruby#resolving-inline-content
8
9
  class InlineContentItemResolver
9
10
  def initialize(callback = nil)
10
11
  @callback = callback
11
12
  end
12
13
 
13
- # Resolves all inline content items in the content
14
- # @param [String] content The string value stored in the element
15
- # @param [Array] inline_items ContentItems referenced by the content
14
+ # Resolves all inline content items in the content.
15
+ #
16
+ # * *Args*:
17
+ # - *content* (+string+) The string value stored in the element
18
+ # - *inline_items* (+Array+) ContentItems referenced by the content from the 'modular_content' JSON node
19
+ #
20
+ # * *Returns*:
21
+ # - +string+ The original content passed, with all <object data-type="item"> replaced with custom output
16
22
  def resolve(content, inline_items)
17
23
  doc = Nokogiri::HTML.parse(content).xpath('//body')
18
24
  tags = doc.xpath('//object[@type="application/kenticocloud"][@data-type="item"]')
@@ -32,11 +38,26 @@ module KenticoCloud
32
38
 
33
39
  # Accepts a tag found in the content and tries to locate matching
34
40
  # ContentItem from JSON response.
41
+ #
42
+ # * *Args*:
43
+ # - *tag* (+string+) A <object data-type="item"> tag found in the content
44
+ # - *inline_items* (+Array+) ContentItems referenced by the content from the 'modular_content' JSON node
45
+ #
46
+ # * *Returns*:
47
+ # - +string+ The custom output generated by the +provide_output+ method
35
48
  def resolve_tag(tag, inline_items)
36
49
  matches = inline_items.select { |item| item.system.codename == tag['data-codename'].to_s }
37
50
  provide_output matches
38
51
  end
39
52
 
53
+ # Generates custom output for a content item using the +resolve_item+
54
+ # method.
55
+ #
56
+ # * *Args*:
57
+ # - *matches* (+Array+) The ContentItems from the 'modular_content' JSON node which match the code name of a particular <object data-type="item"> tag
58
+ #
59
+ # * *Returns*:
60
+ # - +string+ The custom output generated by the +resolve_item+ method
40
61
  def provide_output(matches)
41
62
  if !matches.empty?
42
63
  if @callback.nil?
@@ -1,29 +1,34 @@
1
1
  module KenticoCloud
2
2
  module Delivery
3
- # Resolves a content item by its codename
4
- # It contains the modular content of item/items response
5
- class LinkedItemResolver
6
- def initialize(modular_content, content_link_url_resolver, inline_content_item_resolver)
7
- @modular_content = modular_content
8
- @content_link_url_resolver = content_link_url_resolver
9
- @inline_content_item_resolver = inline_content_item_resolver
10
- @resolved_items = {}
11
- end
3
+ module Resolvers
4
+ # Resolves a content item by its codename. It contains the modular content
5
+ # of item/items response.
6
+ class LinkedItemResolver
7
+ def initialize(modular_content, content_link_url_resolver, inline_content_item_resolver)
8
+ @modular_content = modular_content
9
+ @content_link_url_resolver = content_link_url_resolver
10
+ @inline_content_item_resolver = inline_content_item_resolver
11
+ @resolved_items = {}
12
+ end
12
13
 
13
- # Resolves a content item
14
- # If the link for a codename was resolved before,
15
- # it returns the same instance of Content Item
16
- # @param [String] codename Codename of the content item
17
- # @return [ContentItem]
18
- def resolve(codename)
19
- @resolved_items[codename] ||= resolve_item(codename)
20
- end
14
+ # Resolves a content item. If the link for a codename was resolved
15
+ # before, it returns the same instance of ContentItem.
16
+ #
17
+ # * *Args*:
18
+ # - *codename* (+string+) Codename of the content item
19
+ #
20
+ # * *Return*:
21
+ # - KenticoCloud::Delivery::ContentItem
22
+ def resolve(codename)
23
+ @resolved_items[codename] ||= resolve_item(codename)
24
+ end
21
25
 
22
- private
26
+ private
23
27
 
24
- def resolve_item(codename)
25
- item = @modular_content.values.find { |i| i['system']['codename'] == codename }
26
- ContentItem.new JSON.parse(JSON.generate(item)), @content_link_url_resolver, @inline_content_item_resolver, self
28
+ def resolve_item(codename)
29
+ item = @modular_content.values.find { |i| i['system']['codename'] == codename }
30
+ ContentItem.new JSON.parse(JSON.generate(item)), @content_link_url_resolver, @inline_content_item_resolver, self
31
+ end
27
32
  end
28
33
  end
29
34
  end