delivery-sdk-ruby 1.0.9 → 2.0.2

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