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.
@@ -5,8 +5,7 @@ require 'delivery/version'
5
5
 
6
6
  module KenticoCloud
7
7
  module Delivery
8
- # Responsible for translating query parameters into the
9
- # corresponding REST request to Kentico Cloud.
8
+ # Responsible for executing REST requests to Kentico Cloud.
10
9
  class DeliveryQuery
11
10
  ERROR_PREVIEW = 'Preview is enabled for the query, but the key is null. '\
12
11
  'You can set the preview_key attribute of the query, or '\
@@ -25,14 +24,23 @@ module KenticoCloud
25
24
  :query_string,
26
25
  :content_type
27
26
 
28
- # Setter for url, returns self for chaining
29
- # .url represents *manually* configured urls, otherwise final url is
30
- # generated in .execute and this will return nil
27
+ # Setter for a custom URL.
28
+ #
29
+ # * *Args*:
30
+ # - *url* (+string+) _optional_ Custom URL to use for the query
31
+ #
32
+ # * *Returns*:
33
+ # - +self+
31
34
  def url(url = nil)
32
35
  @url = url unless url.nil?
33
36
  self
34
37
  end
35
38
 
39
+ # Constructor. Queries should not be instantiated using the constructor, but
40
+ # using one of the KenticoCloud::Delivery::DeliveryClient methods instead.
41
+ #
42
+ # * *Args*:
43
+ # - *config* (+Hash+) A hash in which each key automatically has its value paired with the corresponding attribute
36
44
  def initialize(config)
37
45
  @headers = {}
38
46
 
@@ -48,6 +56,10 @@ module KenticoCloud
48
56
  validate_params config.fetch(:qp)
49
57
  end
50
58
 
59
+ # Executes the REST request.
60
+ #
61
+ # * *Returns*:
62
+ # - KenticoCloud::Delivery::Responses::ResponseBase or a class extending it
51
63
  def execute
52
64
  provide_url
53
65
  begin
@@ -66,45 +78,120 @@ module KenticoCloud
66
78
  end
67
79
  end
68
80
 
81
+ # Sets a content link resolver to render links contained in rich text. See
82
+ # https://github.com/Kentico/delivery-sdk-ruby#resolving-links
83
+ #
84
+ # * *Args*:
85
+ # - *resolver* ( KenticoCloud::Delivery::Resolvers::ContentLinkResolver ) The resolver. Replaces a resolver registered during +DeliveryClient+ instantiation, for this query only.
86
+ #
87
+ # * *Returns*:
88
+ # - +self+
69
89
  def with_link_resolver(resolver)
70
90
  self.content_link_url_resolver = resolver
71
91
  self
72
92
  end
73
93
 
94
+ # Sets an inline content itme to render content items and components in rich text.
95
+ # See https://github.com/Kentico/delivery-sdk-ruby#resolving-inline-content
96
+ #
97
+ # * *Args*:
98
+ # - *resolver* ( KenticoCloud::Delivery::Resolvers::InlineContentItemResolver ) The resolver. Replaces a resolver registered during +DeliveryClient+ instantiation, for this query only.
99
+ #
100
+ # * *Returns*:
101
+ # - +self+
74
102
  def with_inline_content_item_resolver(resolver)
75
103
  self.inline_content_item_resolver = resolver
76
104
  self
77
105
  end
78
106
 
107
+ # Sets the 'order' query string parameter
108
+ #
109
+ # * *Args*:
110
+ # - *value* (+string+) The value to order by
111
+ # - *sort* (+string+) _optional_ The direction of the order, surrounded by brackets. The default value is '[asc]'
112
+ #
113
+ # * *Returns*:
114
+ # - +self+
79
115
  def order_by(value, sort = '[asc]')
80
116
  query_string.set_param('order', value + sort)
81
117
  self
82
118
  end
83
119
 
120
+ # Sets the 'skip' query string parameter for paging results.
121
+ # See https://developer.kenticocloud.com/v1/reference#listing-response-paging
122
+ #
123
+ # * *Args*:
124
+ # - *value* (+integer+) The number to skip by
125
+ #
126
+ # * *Returns*:
127
+ # - +self+
84
128
  def skip(value)
85
129
  query_string.set_param('skip', value)
86
130
  self
87
131
  end
88
132
 
133
+ # Sets the 'language' query string parameter. Language fallbacks will be used
134
+ # if untranslated content items are found.
135
+ # See https://developer.kenticocloud.com/docs/localization#section-getting-localized-content-items
136
+ #
137
+ # * *Args*:
138
+ # - *value* (+string+) The code name of the desired language
139
+ #
140
+ # * *Returns*:
141
+ # - +self+
89
142
  def language(value)
90
143
  query_string.set_param('language', value)
91
144
  end
92
145
 
146
+ # Sets the 'limit' query string parameter for paging results, or just to
147
+ # return a specific number of content items.
148
+ # See https://developer.kenticocloud.com/v1/reference#listing-response-paging
149
+ #
150
+ # * *Args*:
151
+ # - *value* (+integer+) The number of content items to return
152
+ #
153
+ # * *Returns*:
154
+ # - +self+
93
155
  def limit(value)
94
156
  query_string.set_param('limit', value)
95
157
  self
96
158
  end
97
159
 
160
+ # Sets the 'elements' query string parameter to limit the elements returned
161
+ # by the query.
162
+ # See https://developer.kenticocloud.com/v1/reference#projection
163
+ #
164
+ # * *Args*:
165
+ # - *value* (+Array+) A single string or array of strings specifying the desired elements, e.g. %w[price product_name image]
166
+ #
167
+ # * *Returns*:
168
+ # - +self+
98
169
  def elements(value)
99
170
  query_string.set_param('elements', value)
100
171
  self
101
172
  end
102
173
 
174
+ # Sets the 'depth' query string parameter to determine how many levels of
175
+ # linked content items should be returned. By default, only 1 level of depth
176
+ # is used.
177
+ # See https://developer.kenticocloud.com/v1/reference#linked-content
178
+ #
179
+ # * *Args*:
180
+ # - *value* (+integer+) Level of linked items to be returned
181
+ #
182
+ # * *Returns*:
183
+ # - +self+
103
184
  def depth(value)
104
185
  query_string.set_param('depth', value)
105
186
  self
106
187
  end
107
188
 
189
+ # Allows the request to bypass caching and return the latest content
190
+ # directly from Kentico Cloud.
191
+ # See https://github.com/Kentico/delivery-sdk-ruby#requesting-the-latest-content
192
+ #
193
+ # * *Returns*:
194
+ # - +self+
108
195
  def request_latest_content
109
196
  @headers['X-KC-Wait-For-Loading-New-Content'] = true
110
197
  self
@@ -112,17 +199,27 @@ module KenticoCloud
112
199
 
113
200
  private
114
201
 
202
+ # Uses KenticoCloud::Delivery::Builders::UrlBuilder.provide_url to set
203
+ # the URL for the query. The +UrlBuilder+ also validates the URL.
204
+ #
205
+ # * *Raises*:
206
+ # - +UriFormatException+ if the URL is 65,519 characters or more
115
207
  def provide_url
116
208
  @url = KenticoCloud::Delivery::Builders::UrlBuilder.provide_url self if @url.nil?
117
209
  KenticoCloud::Delivery::Builders::UrlBuilder.validate_url @url
118
210
  end
119
211
 
212
+ # Initializes the +query_string+ attribute with the passed array of
213
+ # KenticoCloud::Delivery::QueryParameters::Filter objects.
214
+ #
215
+ # * *Raises*:
216
+ # - +ArgumentError+ if one the passed objects is not a +Filter+
120
217
  def validate_params(query_parameters)
121
218
  params = if query_parameters.is_a? Array
122
- query_parameters
123
- else
124
- [query_parameters]
125
- end
219
+ query_parameters
220
+ else
221
+ [query_parameters]
222
+ end
126
223
  params.each do |p|
127
224
  query_string.set_param p
128
225
  unless p.is_a? KenticoCloud::Delivery::QueryParameters::Filter
@@ -131,14 +228,23 @@ module KenticoCloud
131
228
  end
132
229
  end
133
230
 
134
- # Returns true if this query should use preview mode. Raises an error if
135
- # preview is enabled, but the key is nil
231
+ # Determines whether the query should use preview mode.
232
+ #
233
+ # * *Returns*:
234
+ # - +boolean+ Whether preview mode should be used for the query
235
+ #
236
+ # * *Raises*:
237
+ # - +StandardError+ if +use_preview+ is true, but +preview_key+ is +nil+
136
238
  def should_preview
137
239
  raise ERROR_PREVIEW if use_preview && preview_key.nil?
138
240
 
139
241
  use_preview && !preview_key.nil?
140
242
  end
141
243
 
244
+ # Executes the REST request using the +rest-client+ gem.
245
+ #
246
+ # * *Returns*:
247
+ # - +Object+ The response from the server
142
248
  def execute_rest
143
249
  headers = @headers.clone
144
250
 
@@ -153,38 +259,54 @@ module KenticoCloud
153
259
  "rubygems.org;delivery-sdk-ruby;#{KenticoCloud::Delivery::VERSION}"
154
260
  end
155
261
 
262
+ # Converts a standard REST response based on the type of query.
263
+ #
264
+ # * *Returns*:
265
+ # - An object derived from the KenticoCloud::Delivery::Responses::ResponseBase class
156
266
  def make_response(response)
157
267
  case query_type
158
268
  when KenticoCloud::Delivery::QUERY_TYPE_ITEMS
159
- if code_name.nil?
160
- KenticoCloud::Delivery::Responses::DeliveryItemListingResponse.new(
161
- JSON.parse(response),
162
- content_link_url_resolver,
163
- inline_content_item_resolver
164
- )
165
- else
166
- KenticoCloud::Delivery::Responses::DeliveryItemResponse.new(
167
- JSON.parse(response),
168
- content_link_url_resolver,
169
- inline_content_item_resolver
170
- )
171
- end
269
+ respond_item response
172
270
  when KenticoCloud::Delivery::QUERY_TYPE_TYPES
173
- if code_name.nil?
174
- KenticoCloud::Delivery::Responses::DeliveryTypeListingResponse.new JSON.parse(response)
175
- else
176
- KenticoCloud::Delivery::Responses::DeliveryTypeResponse.new JSON.parse(response)
177
- end
271
+ respond_type response
178
272
  when KenticoCloud::Delivery::QUERY_TYPE_TAXONOMIES
179
- if code_name.nil?
180
- KenticoCloud::Delivery::Responses::DeliveryTaxonomyListingResponse.new JSON.parse(response)
181
- else
182
- KenticoCloud::Delivery::Responses::DeliveryTaxonomyResponse.new JSON.parse(response)
183
- end
273
+ respond_taxonomy response
184
274
  when KenticoCloud::Delivery::QUERY_TYPE_ELEMENT
185
275
  KenticoCloud::Delivery::Responses::DeliveryElementResponse.new JSON.parse(response)
186
276
  end
187
277
  end
278
+
279
+ def respond_type(response)
280
+ if code_name.nil?
281
+ KenticoCloud::Delivery::Responses::DeliveryTypeListingResponse.new JSON.parse(response)
282
+ else
283
+ KenticoCloud::Delivery::Responses::DeliveryTypeResponse.new JSON.parse(response)
284
+ end
285
+ end
286
+
287
+ def respond_taxonomy(response)
288
+ if code_name.nil?
289
+ KenticoCloud::Delivery::Responses::DeliveryTaxonomyListingResponse.new JSON.parse(response)
290
+ else
291
+ KenticoCloud::Delivery::Responses::DeliveryTaxonomyResponse.new JSON.parse(response)
292
+ end
293
+ end
294
+
295
+ def respond_item(response)
296
+ if code_name.nil?
297
+ KenticoCloud::Delivery::Responses::DeliveryItemListingResponse.new(
298
+ JSON.parse(response),
299
+ content_link_url_resolver,
300
+ inline_content_item_resolver
301
+ )
302
+ else
303
+ KenticoCloud::Delivery::Responses::DeliveryItemResponse.new(
304
+ JSON.parse(response),
305
+ content_link_url_resolver,
306
+ inline_content_item_resolver
307
+ )
308
+ end
309
+ end
188
310
  end
189
311
  end
190
312
  end
@@ -3,11 +3,14 @@ require 'nokogiri'
3
3
 
4
4
  module KenticoCloud
5
5
  module Delivery
6
- # JSON data of a content item parsed as OpenStruct objects for dynamic use
7
6
  class ContentItem
8
7
  attr_accessor :content_link_url_resolver,
9
8
  :inline_content_item_resolver
10
9
 
10
+ # Parses the 'elements' JSON object as a dynamic OpenStruct object.
11
+ #
12
+ # * *Returns*:
13
+ # - +OpenStruct+ The elements of the content item
11
14
  def elements
12
15
  @elements unless @elements.nil?
13
16
  @elements = JSON.parse(
@@ -16,6 +19,10 @@ module KenticoCloud
16
19
  )
17
20
  end
18
21
 
22
+ # Parses the 'system' JSON object as a dynamic OpenStruct object.
23
+ #
24
+ # * *Returns*:
25
+ # - +OpenStruct+ The system properties of the content item
19
26
  def system
20
27
  @system unless @system.nil?
21
28
  @system = JSON.parse(
@@ -24,6 +31,13 @@ module KenticoCloud
24
31
  )
25
32
  end
26
33
 
34
+ # Constructor.
35
+ #
36
+ # * *Args*:
37
+ # - *source* (+JSON+) The response from a REST request for content items. The item may be on the root or under the 'item' node
38
+ # - *content_link_url_resolver* ( KenticoCloud::Delivery::Resolvers::ContentLinkResolver )
39
+ # - *inline_content_item_resolver* ( KenticoCloud::Delivery::Resolvers::InlineContentItemResolver )
40
+ # - *linked_items_resolver* ( KenticoCloud::Delivery::Resolvers::LinkedItemResolver )
27
41
  def initialize(source, content_link_url_resolver, inline_content_item_resolver, linked_items_resolver)
28
42
  @source =
29
43
  if source['item'].nil?
@@ -36,6 +50,16 @@ module KenticoCloud
36
50
  self.inline_content_item_resolver = inline_content_item_resolver
37
51
  end
38
52
 
53
+ # Gets a string representation of the data stored in the element. Using this
54
+ # method instead of directly accessing the +elements+ collection causes
55
+ # the content to be resolved using the resolvers passed during instantiation.
56
+ # See https://github.com/Kentico/delivery-sdk-ruby#resolving-links
57
+ #
58
+ # * *Args*:
59
+ # - *code_name* (+string+) The code name of the desired element
60
+ #
61
+ # * *Returns*:
62
+ # - +string+ The data converted to a string, resolved if the element is a 'rich_text' element
39
63
  def get_string(code_name)
40
64
  element = get_element code_name
41
65
  content = element['value']
@@ -48,21 +72,40 @@ module KenticoCloud
48
72
  content.to_s
49
73
  end
50
74
 
51
- # Returns an array of assets inserted into the specified element
75
+ # Returns an array of assets inserted into the specified element of the
76
+ # 'asset' type.
77
+ #
78
+ # * *Args*:
79
+ # - *code_name* (+string+) The code name of the desired element
80
+ #
81
+ # * *Returns*:
82
+ # - +Array+ The element's assets parsed as +OpenStruct+ objects
52
83
  def get_assets(code_name)
53
84
  element = get_element code_name
54
85
  element['value'].map { |n| OpenStruct.new(n) }
55
86
  end
56
87
 
57
- # Returns an array of ContentItems by comparing code names stored in the
58
- # element with items from request's modular_content
88
+ # Returns an array of ContentItems that are linked in a 'modular_content'
89
+ # element.
90
+ #
91
+ # * *Args*:
92
+ # - *code_name* (+string+) The code name of the desired element
93
+ #
94
+ # * *Returns*:
95
+ # - +Array+ The element's linked items parsed as +ContentItem+ objects
59
96
  def get_links(code_name)
60
97
  element = get_element code_name
61
98
  get_linked_items element['value']
62
99
  end
63
100
 
64
- # Returns an array of ContentItems by comparing code names stored in the
65
- # modular_content object with items from request's modular_content
101
+ # Returns an array of ContentItems that are inserted as inline content
102
+ # items or componenets of a 'rich_text' element.
103
+ #
104
+ # * *Args*:
105
+ # - *code_name* (+string+) The code name of the desired element
106
+ #
107
+ # * *Returns*:
108
+ # - +Array+ The element's inline content items parsed as +ContentItem+ objects
66
109
  def get_inline_items(code_name)
67
110
  element = get_element code_name
68
111
  get_linked_items element['modular_content']
@@ -78,6 +121,16 @@ module KenticoCloud
78
121
  !element['modular_content'].nil? && !inline_content_item_resolver.nil?
79
122
  end
80
123
 
124
+ # Gets the JSON object from the 'elements' collection with the specified key
125
+ #
126
+ # * *Args*:
127
+ # - *code_name* (+string+) The code name of the desired element
128
+ #
129
+ # * *Returns*:
130
+ # - +JSON+ The element as a JSON object
131
+ #
132
+ # * *Raises*:
133
+ # - +ArgumentError+ if +code_name+ is +nil+ or not a +string+
81
134
  def get_element(code_name)
82
135
  raise ArgumentError, "Argument 'code_name' cannot be null" if code_name.nil?
83
136
  raise ArgumentError, "Argument 'code_name' is not a string" unless code_name.is_a? String
@@ -2,8 +2,11 @@ require 'ostruct'
2
2
 
3
3
  module KenticoCloud
4
4
  module Delivery
5
- # JSON data of a content type parsed as OpenStruct objects for dynamic use
6
5
  class ContentType
6
+ # Parses the 'elements' JSON object as a dynamic OpenStruct object.
7
+ #
8
+ # * *Returns*:
9
+ # - +OpenStruct+ The elements of the content type
7
10
  def elements
8
11
  @elements unless @elements.nil?
9
12
  @elements = JSON.parse(
@@ -12,6 +15,10 @@ module KenticoCloud
12
15
  )
13
16
  end
14
17
 
18
+ # Parses the 'system' JSON object as a dynamic OpenStruct object.
19
+ #
20
+ # * *Returns*:
21
+ # - +OpenStruct+ The system properties of the content type
15
22
  def system
16
23
  @system unless @system.nil?
17
24
  @system = JSON.parse(
@@ -20,6 +27,10 @@ module KenticoCloud
20
27
  )
21
28
  end
22
29
 
30
+ # Constructor.
31
+ #
32
+ # * *Args*:
33
+ # - *source* (+JSON+) The response from a REST request for content types
23
34
  def initialize(source)
24
35
  @source = source
25
36
  end