delivery-sdk-ruby 1.0.5 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +49 -48
- data/lib/delivery/builders/image_transformation_builder.rb +239 -237
- data/lib/delivery/builders/url_builder.rb +95 -93
- data/lib/delivery/client/delivery_client.rb +137 -135
- data/lib/delivery/client/delivery_query.rb +222 -220
- data/lib/delivery/client/request_manager.rb +83 -81
- data/lib/delivery/models/content_item.rb +132 -130
- data/lib/delivery/models/content_type.rb +33 -31
- data/lib/delivery/models/pagination.rb +16 -14
- data/lib/delivery/models/taxonomy_group.rb +33 -31
- data/lib/delivery/query_parameters/filters.rb +36 -34
- data/lib/delivery/query_parameters/parameter_base.rb +37 -35
- data/lib/delivery/query_parameters/query_string.rb +65 -63
- data/lib/delivery/resolvers/content_link_resolver.rb +85 -83
- data/lib/delivery/resolvers/inline_content_item_resolver.rb +61 -59
- data/lib/delivery/resolvers/linked_item_resolver.rb +29 -27
- data/lib/delivery/responses/delivery_element_response.rb +25 -23
- data/lib/delivery/responses/delivery_item_listing_response.rb +41 -39
- data/lib/delivery/responses/delivery_item_response.rb +30 -28
- data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +33 -31
- data/lib/delivery/responses/delivery_taxonomy_response.rb +22 -20
- data/lib/delivery/responses/delivery_type_listing_response.rb +33 -31
- data/lib/delivery/responses/delivery_type_response.rb +22 -20
- data/lib/delivery/responses/response_base.rb +29 -27
- data/lib/delivery/tests/fake_responder.rb +50 -48
- data/lib/delivery/tests/filtering/items_gt.json +4 -4
- data/lib/delivery/tests/filtering/multiple.json +2 -2
- data/lib/delivery/tests/filtering/pagination_about_us.json +3 -3
- data/lib/delivery/tests/generic/items.json +25 -25
- data/lib/delivery/tests/generic/items/about_us.json +1 -1
- data/lib/delivery/tests/generic/items/aeropress_filters.json +1 -1
- data/lib/delivery/tests/generic/items/coffee_processing_techniques.json +1 -1
- data/lib/delivery/tests/generic/items/where_does_coffee_come_from_.json +4 -4
- data/lib/delivery/version.rb +5 -3
- data/lib/kontent-delivery-sdk-ruby.rb +19 -0
- metadata +38 -23
@@ -2,245 +2,247 @@ require 'delivery/builders/url_builder'
|
|
2
2
|
require 'delivery/query_parameters/query_string'
|
3
3
|
require 'delivery/version'
|
4
4
|
|
5
|
-
module
|
6
|
-
module
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
5
|
+
module Kentico
|
6
|
+
module Kontent
|
7
|
+
module Delivery
|
8
|
+
# Responsible for executing REST requests to Kentico Kontent.
|
9
|
+
class DeliveryQuery
|
10
|
+
ERROR_PREVIEW = 'Preview is enabled for the query, but the key is null. '\
|
11
|
+
'You can set the preview_key attribute of the query, or '\
|
12
|
+
'when you initialize the client. See '\
|
13
|
+
'https://github.com/Kentico/kontent-delivery-sdk-ruby#previewing-unpublished-content'.freeze
|
14
|
+
ERROR_PARAMS = 'Only filters may be passed in the .item or .items methods'\
|
15
|
+
'. See https://github.com/Kentico/kontent-delivery-sdk-ruby#filtering'.freeze
|
16
|
+
attr_accessor :use_preview,
|
17
|
+
:preview_key,
|
18
|
+
:project_id,
|
19
|
+
:code_name,
|
20
|
+
:secure_key,
|
21
|
+
:content_link_url_resolver,
|
22
|
+
:inline_content_item_resolver,
|
23
|
+
:query_type,
|
24
|
+
:query_string,
|
25
|
+
:content_type,
|
26
|
+
:with_retry_policy
|
27
|
+
|
28
|
+
# Setter for a custom URL.
|
29
|
+
#
|
30
|
+
# * *Args*:
|
31
|
+
# - *url* (+string+) _optional_ Custom URL to use for the query
|
32
|
+
#
|
33
|
+
# * *Returns*:
|
34
|
+
# - +self+
|
35
|
+
def url(url = nil)
|
36
|
+
@url = url unless url.nil?
|
37
|
+
self
|
38
|
+
end
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
40
|
+
# Constructor. Queries should not be instantiated using the constructor, but
|
41
|
+
# using one of the Kentico::Kontent::Delivery::DeliveryClient methods instead.
|
42
|
+
#
|
43
|
+
# * *Args*:
|
44
|
+
# - *config* (+Hash+) A hash in which each key automatically has its value paired with the corresponding attribute
|
45
|
+
def initialize(config)
|
46
|
+
@headers = {}
|
47
|
+
|
48
|
+
# Map each hash value to attr with corresponding key
|
49
|
+
# from https://stackoverflow.com/a/2681014/5656214
|
50
|
+
config.each do |k, v|
|
51
|
+
instance_variable_set("@#{k}", v) unless v.nil?
|
52
|
+
end
|
53
|
+
self.query_string = Kentico::Kontent::Delivery::QueryParameters::QueryString.new
|
54
|
+
return if config.fetch(:qp, nil).nil?
|
46
55
|
|
47
|
-
|
48
|
-
|
49
|
-
config.each do |k, v|
|
50
|
-
instance_variable_set("@#{k}", v) unless v.nil?
|
56
|
+
# Query parameters were passed, parse and validate
|
57
|
+
validate_params config.fetch(:qp)
|
51
58
|
end
|
52
|
-
self.query_string = KenticoCloud::Delivery::QueryParameters::QueryString.new
|
53
|
-
return if config.fetch(:qp, nil).nil?
|
54
|
-
|
55
|
-
# Query parameters were passed, parse and validate
|
56
|
-
validate_params config.fetch(:qp)
|
57
|
-
end
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
60
|
+
# Executes the REST request.
|
61
|
+
#
|
62
|
+
# * *Returns*:
|
63
|
+
# - Kentico::Kontent::Delivery::Responses::ResponseBase or a class extending it
|
64
|
+
def execute
|
65
|
+
headers = @headers.clone
|
66
|
+
headers['X-KC-SDKID'] = provide_sdk_header
|
67
|
+
headers['Authorization'] = "Bearer #{preview_key}" if should_preview
|
68
|
+
headers['Authorization'] = "Bearer #{secure_key}" if !should_preview && secure_key
|
69
|
+
|
70
|
+
resp = Kentico::Kontent::Delivery::RequestManager.start self, headers
|
71
|
+
yield resp if block_given?
|
72
|
+
resp
|
73
|
+
end
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
75
|
+
# Determines whether the query should use preview mode.
|
76
|
+
#
|
77
|
+
# * *Returns*:
|
78
|
+
# - +boolean+ Whether preview mode should be used for the query
|
79
|
+
#
|
80
|
+
# * *Raises*:
|
81
|
+
# - +StandardError+ if +use_preview+ is true, but +preview_key+ is +nil+
|
82
|
+
def should_preview
|
83
|
+
raise ERROR_PREVIEW if use_preview && preview_key.nil?
|
84
|
+
|
85
|
+
use_preview && !preview_key.nil?
|
86
|
+
end
|
86
87
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
88
|
+
# Sets a content link resolver to render links contained in rich text. See
|
89
|
+
# https://github.com/Kentico/kontent-delivery-sdk-ruby#resolving-links
|
90
|
+
#
|
91
|
+
# * *Args*:
|
92
|
+
# - *resolver* ( Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver ) The resolver. Replaces a resolver registered during +DeliveryClient+ instantiation, for this query only.
|
93
|
+
#
|
94
|
+
# * *Returns*:
|
95
|
+
# - +self+
|
96
|
+
def with_link_resolver(resolver)
|
97
|
+
self.content_link_url_resolver = resolver
|
98
|
+
self
|
99
|
+
end
|
99
100
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
101
|
+
# Sets an inline content itme to render content items and components in rich text.
|
102
|
+
# See https://github.com/Kentico/kontent-delivery-sdk-ruby#resolving-inline-content
|
103
|
+
#
|
104
|
+
# * *Args*:
|
105
|
+
# - *resolver* ( Kentico::Kontent::Delivery::Resolvers::InlineContentItemResolver ) The resolver. Replaces a resolver registered during +DeliveryClient+ instantiation, for this query only.
|
106
|
+
#
|
107
|
+
# * *Returns*:
|
108
|
+
# - +self+
|
109
|
+
def with_inline_content_item_resolver(resolver)
|
110
|
+
self.inline_content_item_resolver = resolver
|
111
|
+
self
|
112
|
+
end
|
112
113
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
114
|
+
# Sets the 'order' query string parameter
|
115
|
+
#
|
116
|
+
# * *Args*:
|
117
|
+
# - *value* (+string+) The value to order by
|
118
|
+
# - *sort* (+string+) _optional_ The direction of the order, surrounded by brackets. The default value is '[asc]'
|
119
|
+
#
|
120
|
+
# * *Returns*:
|
121
|
+
# - +self+
|
122
|
+
def order_by(value, sort = '[asc]')
|
123
|
+
query_string.set_param('order', value + sort)
|
124
|
+
self
|
125
|
+
end
|
125
126
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
127
|
+
# Sets the 'skip' query string parameter for paging results.
|
128
|
+
# See https://developer.kenticocloud.com/v1/reference#listing-response-paging
|
129
|
+
#
|
130
|
+
# * *Args*:
|
131
|
+
# - *value* (+integer+) The number to skip by
|
132
|
+
#
|
133
|
+
# * *Returns*:
|
134
|
+
# - +self+
|
135
|
+
def skip(value)
|
136
|
+
query_string.set_param('skip', value)
|
137
|
+
self
|
138
|
+
end
|
138
139
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
140
|
+
# Sets the 'language' query string parameter. Language fallbacks will be used
|
141
|
+
# if untranslated content items are found.
|
142
|
+
# See https://developer.kenticocloud.com/docs/localization#section-getting-localized-content-items
|
143
|
+
#
|
144
|
+
# * *Args*:
|
145
|
+
# - *value* (+string+) The code name of the desired language
|
146
|
+
#
|
147
|
+
# * *Returns*:
|
148
|
+
# - +self+
|
149
|
+
def language(value)
|
150
|
+
query_string.set_param('language', value)
|
151
|
+
self
|
152
|
+
end
|
152
153
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
154
|
+
# Sets the 'limit' query string parameter for paging results, or just to
|
155
|
+
# return a specific number of content items.
|
156
|
+
# See https://developer.kenticocloud.com/v1/reference#listing-response-paging
|
157
|
+
#
|
158
|
+
# * *Args*:
|
159
|
+
# - *value* (+integer+) The number of content items to return
|
160
|
+
#
|
161
|
+
# * *Returns*:
|
162
|
+
# - +self+
|
163
|
+
def limit(value)
|
164
|
+
query_string.set_param('limit', value)
|
165
|
+
self
|
166
|
+
end
|
166
167
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
168
|
+
# Sets the 'elements' query string parameter to limit the elements returned
|
169
|
+
# by the query.
|
170
|
+
# See https://developer.kenticocloud.com/v1/reference#projection
|
171
|
+
#
|
172
|
+
# * *Args*:
|
173
|
+
# - *value* (+Array+) A single string or array of strings specifying the desired elements, e.g. %w[price product_name image]
|
174
|
+
#
|
175
|
+
# * *Returns*:
|
176
|
+
# - +self+
|
177
|
+
def elements(value)
|
178
|
+
query_string.set_param('elements', value)
|
179
|
+
self
|
180
|
+
end
|
180
181
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
182
|
+
# Sets the 'depth' query string parameter to determine how many levels of
|
183
|
+
# linked content items should be returned. By default, only 1 level of depth
|
184
|
+
# is used.
|
185
|
+
# See https://developer.kenticocloud.com/v1/reference#linked-content
|
186
|
+
#
|
187
|
+
# * *Args*:
|
188
|
+
# - *value* (+integer+) Level of linked items to be returned
|
189
|
+
#
|
190
|
+
# * *Returns*:
|
191
|
+
# - +self+
|
192
|
+
def depth(value)
|
193
|
+
query_string.set_param('depth', value)
|
194
|
+
self
|
195
|
+
end
|
195
196
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
197
|
+
# Allows the request to bypass caching and return the latest content
|
198
|
+
# directly from Kentico Kontent.
|
199
|
+
# See https://github.com/Kentico/kontent-delivery-sdk-ruby#requesting-the-latest-content
|
200
|
+
#
|
201
|
+
# * *Returns*:
|
202
|
+
# - +self+
|
203
|
+
def request_latest_content
|
204
|
+
@headers['X-KC-Wait-For-Loading-New-Content'] = true
|
205
|
+
self
|
206
|
+
end
|
206
207
|
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
208
|
+
# Uses Kentico::Kontent::Delivery::Builders::UrlBuilder.provide_url to set
|
209
|
+
# the URL for the query. The +UrlBuilder+ also validates the URL.
|
210
|
+
#
|
211
|
+
# * *Raises*:
|
212
|
+
# - +UriFormatException+ if the URL is 65,519 characters or more
|
213
|
+
#
|
214
|
+
# * *Returns*:
|
215
|
+
# - +string+ The full URL for this query
|
216
|
+
def provide_url
|
217
|
+
@url = Kentico::Kontent::Delivery::Builders::UrlBuilder.provide_url self if @url.nil?
|
218
|
+
Kentico::Kontent::Delivery::Builders::UrlBuilder.validate_url @url
|
219
|
+
@url
|
220
|
+
end
|
220
221
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
222
|
+
private
|
223
|
+
|
224
|
+
# Initializes the +query_string+ attribute with the passed array of
|
225
|
+
# Kentico::Kontent::Delivery::QueryParameters::Filter objects.
|
226
|
+
#
|
227
|
+
# * *Raises*:
|
228
|
+
# - +ArgumentError+ if one the passed objects is not a +Filter+
|
229
|
+
def validate_params(query_parameters)
|
230
|
+
params = if query_parameters.is_a? Array
|
231
|
+
query_parameters
|
232
|
+
else
|
233
|
+
[query_parameters]
|
234
|
+
end
|
235
|
+
params.each do |p|
|
236
|
+
query_string.set_param p
|
237
|
+
unless p.is_a? Kentico::Kontent::Delivery::QueryParameters::Filter
|
238
|
+
raise ArgumentError, ERROR_PARAMS
|
239
|
+
end
|
238
240
|
end
|
239
241
|
end
|
240
|
-
end
|
241
242
|
|
242
|
-
|
243
|
-
|
243
|
+
def provide_sdk_header
|
244
|
+
"rubygems.org;kontent-delivery-sdk-ruby;#{Kentico::Kontent::Delivery::VERSION}"
|
245
|
+
end
|
244
246
|
end
|
245
247
|
end
|
246
248
|
end
|