kontent-ai-delivery 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +21 -0
  3. data/README.md +617 -0
  4. data/bin/console +14 -0
  5. data/bin/setup +8 -0
  6. data/lib/delivery/builders/image_transformation_builder.rb +272 -0
  7. data/lib/delivery/builders/url_builder.rb +123 -0
  8. data/lib/delivery/client/delivery_client.rb +184 -0
  9. data/lib/delivery/client/delivery_query.rb +308 -0
  10. data/lib/delivery/client/request_manager.rb +127 -0
  11. data/lib/delivery/models/content_item.rb +153 -0
  12. data/lib/delivery/models/content_type.rb +41 -0
  13. data/lib/delivery/models/language.rb +29 -0
  14. data/lib/delivery/models/pagination.rb +22 -0
  15. data/lib/delivery/models/taxonomy_group.rb +39 -0
  16. data/lib/delivery/query_parameters/filters.rb +201 -0
  17. data/lib/delivery/query_parameters/parameter_base.rb +56 -0
  18. data/lib/delivery/query_parameters/query_string.rb +78 -0
  19. data/lib/delivery/resolvers/content_link_resolver.rb +102 -0
  20. data/lib/delivery/resolvers/inline_content_item_resolver.rb +75 -0
  21. data/lib/delivery/resolvers/linked_item_resolver.rb +43 -0
  22. data/lib/delivery/responses/delivery_element_response.rb +34 -0
  23. data/lib/delivery/responses/delivery_item_listing_response.rb +54 -0
  24. data/lib/delivery/responses/delivery_item_response.rb +40 -0
  25. data/lib/delivery/responses/delivery_items_feed_response.rb +58 -0
  26. data/lib/delivery/responses/delivery_language_listing_response.rb +44 -0
  27. data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +47 -0
  28. data/lib/delivery/responses/delivery_taxonomy_response.rb +33 -0
  29. data/lib/delivery/responses/delivery_type_listing_response.rb +46 -0
  30. data/lib/delivery/responses/delivery_type_response.rb +32 -0
  31. data/lib/delivery/responses/response_base.rb +39 -0
  32. data/lib/delivery/tests/401.json +6 -0
  33. data/lib/delivery/tests/429.json +5 -0
  34. data/lib/delivery/tests/fake_responder.rb +99 -0
  35. data/lib/delivery/tests/filtering/items_with_count.json +5385 -0
  36. data/lib/delivery/tests/filtering/pagination.json +762 -0
  37. data/lib/delivery/tests/generic/items/about_us.json +277 -0
  38. data/lib/delivery/tests/generic/items/aeropress_filters.json +156 -0
  39. data/lib/delivery/tests/generic/items/coffee_processing_techniques.json +566 -0
  40. data/lib/delivery/tests/generic/items/empty_rich_text.json +24 -0
  41. data/lib/delivery/tests/generic/items/rich_text_complex_tables.json +81 -0
  42. data/lib/delivery/tests/generic/items/where_does_coffee_come_from_.json +599 -0
  43. data/lib/delivery/tests/generic/items.json +5384 -0
  44. data/lib/delivery/tests/generic/languages.json +24 -0
  45. data/lib/delivery/tests/generic/taxonomies/manufacturer.json +30 -0
  46. data/lib/delivery/tests/generic/taxonomies.json +204 -0
  47. data/lib/delivery/tests/generic/types/brewer/elements/product_status.json +6 -0
  48. data/lib/delivery/tests/generic/types/brewer.json +89 -0
  49. data/lib/delivery/tests/generic/types.json +836 -0
  50. data/lib/delivery/tests/items_feed/articles_feed_1.json +39 -0
  51. data/lib/delivery/tests/items_feed/articles_feed_2.json +78 -0
  52. data/lib/delivery/tests/items_feed/articles_feed_3.json +104 -0
  53. data/lib/kontent-ai-delivery.rb +22 -0
  54. metadata +221 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 96700708a1abcfb9087e8ec1af64ea2bfa572adf8f6305e636d9afe13600b1cd
4
+ data.tar.gz: 80354b89c9d509e8e29ffba6aa34064a9502eceec078bb03aa6db10ed5b33f64
5
+ SHA512:
6
+ metadata.gz: f088f298574e4129e87ff6a35f04f9ed532a1fc23ba825e1ac907da47963581bcb23348935a122ca4766aa7edec5031e0889e8d61b69c5f2e3d3e7ee01c8c361
7
+ data.tar.gz: 1aad94652fdd59042e1dd171c615fc32119a0937fd2abfa964b74f1dd2c53040b5462d1635c88aaf859cfd011a93774d7b85f623ad7311217298e580d2dbb801
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Kontent s.r.o.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,617 @@
1
+ # Kontent.ai Delivery Ruby SDK
2
+
3
+ ![build](https://github.com/kontent-ai/delivery-sdk-ruby/actions/workflows/build.yml/badge.svg)
4
+ [![Version](https://img.shields.io/gem/v/kontent-ai-delivery.svg?style=flat)](https://rubygems.org/gems/kontent-ai-delivery)
5
+
6
+ [![Stack Overflow](https://img.shields.io/badge/Stack%20Overflow-ASK%20NOW-FE7A16.svg?logo=stackoverflow&logoColor=white)](https://stackoverflow.com/tags/kontent-ai)
7
+ [![Discord](https://img.shields.io/discord/821885171984891914?color=%237289DA&label=Kontent.ai%20Discord&logo=discord)](https://discord.gg/SKCxwPtevJ)
8
+
9
+ ![Banner](/banner.png)
10
+
11
+ The Delivery Ruby SDK can be used in Ruby/Rails projects to retrieve content from Kontent.ai. This is a community project and not an official Kontent.ai SDK. If you find a bug in the SDK or have a feature request, please submit a GitHub issue.
12
+
13
+ See [How to setup a development environment on Windows](./docs/How-to-setup-development-environment-on-Windows.md) for local development, and check out the [Kontent.ai Blog](https://kontent.ai/blog/creating-a-kentico-cloud-ruby-on-rails-application) for a tutorial on creating a Rails application.
14
+
15
+ ## Table of contents
16
+
17
+ - [Kontent.ai Delivery Ruby SDK](#kontentai-delivery-ruby-sdk)
18
+ - [Table of contents](#table-of-contents)
19
+ - [Installation](#installation)
20
+ - [Creating a client](#creating-a-client)
21
+ - [Previewing unpublished content](#previewing-unpublished-content)
22
+ - [Making secure requests](#making-secure-requests)
23
+ - [Retry policy](#retry-policy)
24
+ - [Custom URLs](#custom-urls)
25
+ - [Listing items](#listing-items)
26
+ - [Filtering](#filtering)
27
+ - [Parameters](#parameters)
28
+ - [Responses](#responses)
29
+ - [Requesting the latest content](#requesting-the-latest-content)
30
+ - [Providing custom headers](#providing-custom-headers)
31
+ - [Pagination](#pagination)
32
+ - [Working with content items](#working-with-content-items)
33
+ - [Assets](#assets)
34
+ - [Linked items](#linked-items)
35
+ - [Resolving links](#resolving-links)
36
+ - [Resolving inline content](#resolving-inline-content)
37
+ - [Items feed](#items-feed)
38
+ - [Retrieving content types](#retrieving-content-types)
39
+ - [Retrieving taxonomy](#retrieving-taxonomy)
40
+ - [Retrieving content type elements](#retrieving-content-type-elements)
41
+ - [Retrieving languages](#retrieving-languages)
42
+ - [Image transformation](#image-transformation)
43
+ - [Feedback & Contributing](#feedback--contributing)
44
+ - [License](#license)
45
+ - [Code of Conduct](#code-of-conduct)
46
+ - [Wall of Fame](#wall-of-fame)
47
+
48
+ ## Installation
49
+
50
+ To use the SDK in your own project, add the gem to your Gemfile:
51
+
52
+ ```ruby
53
+ gem 'delivery-sdk-ruby'
54
+ ```
55
+
56
+ Then run `bundle install`. You can also download the gem from [RubyGems.org](https://rubygems.org/gems/delivery-sdk-ruby). To use the SDK in an `.rb` file, you need to require it:
57
+
58
+ ```ruby
59
+ require 'delivery-sdk-ruby'
60
+ ```
61
+
62
+ ## Creating a client
63
+
64
+ You will use `Kontent::Ai::Delivery::DeliveryClient` to obtain content from Kontent.ai. Create an instance of the client and pass your project ID:
65
+
66
+ ```ruby
67
+ delivery_client = Kontent::Ai::Delivery::DeliveryClient.new project_id: '<your-project-id>'
68
+ ```
69
+
70
+ :gem: **Pro tip:** You can alias namespaces to make them shorter, e.g.
71
+
72
+ ```ruby
73
+ KA = Kontent::Ai::Delivery
74
+ delivery_client = KA::DeliveryClient.new project_id: '<your-project-id>'
75
+ ```
76
+
77
+ ### Previewing unpublished content
78
+
79
+ To [enable preview](https://kontent.ai/learn/tutorials/develop-apps/build-strong-foundation/set-up-preview "See how to configure your app and Kontent.ai project to enable content preview"), pass the Preview API Key to the constructor:
80
+
81
+ ```ruby
82
+ delivery_client = Kontent::Ai::Delivery::DeliveryClient.new project_id: '<your-project-id>',
83
+ preview_key: '<your-preview-key>'
84
+ ```
85
+
86
+ This enables preview, but you can toggle preview at any time by setting the `use_preview` attribute of `DeliveryClient` which is propagated to all queries created by the client, _or_ per-query by setting its `use_preview` attribute:
87
+
88
+ ```ruby
89
+ # For all queries created by client
90
+ delivery_client.use_preview = false
91
+
92
+ # Per-query
93
+ query = delivery_client.items
94
+ query.use_preview = false
95
+ query.execute do |response|
96
+ # Do something
97
+ end
98
+ ```
99
+
100
+ ### Making secure requests
101
+
102
+ If you've [secured access](https://kontent.ai/learn/tutorials/develop-apps/build-strong-foundation/restrict-public-access "See how to enable secured access for your Kontent.ai project") to your project, you need to provide the `DeliveryClient` with either the primary or secondary key:
103
+
104
+ ```ruby
105
+ Kontent::Ai::Delivery::DeliveryClient.new project_id: '<your-project-id>',
106
+ secure_key: '<your-secure-key>'
107
+ ```
108
+
109
+ You can then securely request published content in your project. Be sure to not expose the key if the file(s) it appears in are publicly available.
110
+
111
+ ### Retry policy
112
+
113
+ By default, the SDK uses a retry policy, asking for requested content again in case of an error. The default policy retries the HTTP request if the following status codes are returned:
114
+
115
+ - 408 - `RequestTimeout`
116
+ - 429 - `TooManyRequests`
117
+ - 500 - `InternalServerError`
118
+ - 502 - `BadGateway`
119
+ - 503 - `ServiceUnavailable`
120
+ - 504 - `GatewayTimeout`
121
+
122
+ The SDK will perform a total of 6 attempts at a maximum of 30 seconds to retrieve content before returning a `ResponseBase` object containing the error. The consecutive attempts are delayed with [exponential backoff and jitter](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/).
123
+
124
+ To disable the retry policy, you can use the `with_retry_policy` argument:
125
+
126
+ ```ruby
127
+ Kontent::Ai::Delivery::DeliveryClient.new project_id: '<your-project-id>',
128
+ secure_key: '<your-secure-key>',
129
+ with_retry_policy: false
130
+ ```
131
+
132
+ ### Custom URLs
133
+
134
+ When you have a URL (i.e. `next_page` for paging, for testing purposes, or if you prefer to build it on your own) and still want to leverage SDK functionality such as rich text resolving, use the .url method:
135
+
136
+ ```ruby
137
+ delivery_client.items
138
+ .url('https://deliver.kontent.ai/<your-project-id>/items?system.type=grinder')
139
+ .execute do |response|
140
+ # Do something
141
+ end
142
+ ```
143
+
144
+ ## Listing items
145
+
146
+ Use `.item` or `.items` to create a `Kontent::Ai::Delivery::DeliveryQuery`, then call `.execute` to perform the request.
147
+
148
+ ```ruby
149
+ delivery_client.items.execute do |response|
150
+ response.items.each do |item|
151
+ # Do something
152
+ end
153
+ end
154
+ ```
155
+
156
+ You can also execute the query without a block and just get the response:
157
+
158
+ ```ruby
159
+ response = delivery_client.items.execute
160
+ ```
161
+
162
+ ### Filtering
163
+
164
+ You can use [filtering](https://kontent.ai/learn/reference/delivery-api#tag/Filtering-content "See content filtering options in Delivery API") to retrieve particular items. The filtering methods are applied directly to a string and the available methods are:
165
+
166
+ | Method | Example | REST equivalent |
167
+ | --------- | -------------------------------------------------------- | -------------------------------------------------- |
168
+ | all | `'elements.product_status'.all %w[bestseller on_sale]` | ?elements.product_status[all]=bestseller,on_sale |
169
+ | any | `'elements.processing'.any %w[dry__natural_ semi_dry]` | ?elements.processing[any]=dry\__natural_,semi_dry |
170
+ | contains | `'elements.related_articles'.contains 'on_roasts'` | ?elements.related_articles[contains]=on_roasts |
171
+ | eq | `'system.type'.eq 'grinder'` | ?system.type=grinder |
172
+ | not_eq | `'elements.region'.not_eq 'USA'` | ?elements.region[neq]=USA |
173
+ | gt | `'elements.price'.gt 20` | ?elements.price[gt]=20 |
174
+ | gt_or_eq | `'elements.price'.gt_or_eq 20` | ?elements.price[gte]=20 |
175
+ | in | `'system.type'.in %w[coffee brewer]` | ?system.type[in]=coffee,brewer |
176
+ | not_in | `'elements.author'.not_in %w[mberry ericd anthonym]` | ?elements.author[nin]=mberry,ericd,anthonym |
177
+ | lt | `'elements.price'.lt 20` | ?elements.price[lt]=20 |
178
+ | lt_or_eq | `'elements.price'.lt_or_eq 20` | ?elements.price[lte]=20 |
179
+ | range | `'system.last_modified'.range %w[2018-02-01 2018-03-31]` | ?system.last_modified[range]=2018-02-01,2018-03-31 |
180
+ | empty | `'elements.banned_reason'.empty` | ?elements.banned_reason[empty] |
181
+ | not_empty | `'elements.status'.not_empty` | ?elements.status[nempty] |
182
+
183
+ You can pass a single filter or multiple filters in the DeliveryClient methods. For example:
184
+
185
+ ```ruby
186
+ # Single filter
187
+ delivery_client.items('elements.price'.gt 20)
188
+
189
+ # Multiple filters
190
+ delivery_client.items [
191
+ ('elements.price'.gt 20),
192
+ ('system.type'.eq 'grinder')
193
+ ]
194
+ ```
195
+
196
+ ### Parameters
197
+
198
+ The `.item` and `.items` methods return a `Kontent::Ai::Delivery::DeliveryQuery` object which you can further configure before executing. The methods you can call are:
199
+
200
+ | Method | Example | REST equivalent |
201
+ | ----------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | ---------------------------------- |
202
+ | [order_by](https://kontent.ai/learn/reference/delivery-api#operation/list-content-items "order_by") | `order_by 'system.last_modified' '[desc]'` | ?order=system.last_modified[desc] |
203
+ | [skip](https://kontent.ai/learn/reference/delivery-api#operation/list-content-items "skip") | `skip 5` | ?skip=5 |
204
+ | [limit](https://kontent.ai/learn/reference/delivery-api#operation/list-content-items "limit") | `limit 5` | ?limit=5 |
205
+ | [elements](https://kontent.ai/learn/reference/delivery-api#tag/Projection "elements") | `elements %w[price product_name image]` | ?elements=price,product_name,image |
206
+ | [depth](https://kontent.ai/learn/reference/delivery-api#tag/Linked-content-and-components/linked-content-depth "depth") | `depth 0` | ?depth=0 |
207
+ | [language](https://kontent.ai/learn/tutorials/manage-kontent/projects/set-up-languages#a-language-fallbacks "language") | `language 'en'` | ?language=en |
208
+
209
+ For example:
210
+
211
+ ```ruby
212
+ delivery_client.items('system.type'.eq 'coffee')
213
+ .depth(0)
214
+ .limit(5)
215
+ .elements(%W[price product_name])
216
+ .execute do |response|
217
+ # Do something
218
+ end
219
+ ```
220
+
221
+ ### Responses
222
+
223
+ All responses from the `.execute` method will be/extend the `Kontent::Ai::Delivery::Responses::ResponseBase` class which contains the following attributes:
224
+
225
+ - **http_code**: The HTTP status code of the response
226
+ - **headers**: The headers of the response
227
+ - **json**: The full JSON body of the response
228
+
229
+ You can check the response code to determine if the request was successful:
230
+
231
+ ```ruby
232
+ delivery_client.items.execute do |response|
233
+ case response.http_code
234
+ when 200
235
+ # Success!
236
+ when 401
237
+ # Did you forget the secure key?
238
+ else
239
+ # to_s displays a friendly message with details of the response
240
+ puts response.to_s
241
+ end
242
+ end
243
+ ```
244
+
245
+ For successful content item queries, you will get either `DeliveryItemResponse` for single item queries, or `DeliveryItemListingResponse` for multiple item queries. You can access the returned content item(s) at `.item` or `.items` respectively.
246
+
247
+ The `ContentItem` object gives you access to all system elements and content type elements at the `.system` and `.elements` properies. These are dynamic objects, so you can simply type the name of the element you need:
248
+
249
+ ```ruby
250
+ price = response.item.elements.price.value
251
+ ```
252
+
253
+ ### Requesting the latest content
254
+
255
+ Kontent.ai caches content using Fastly, so requests made to Kontent.ai may not be up-to-date. In some cases, such as when reacting to [webhook](https://kontent.ai/learn/tutorials/develop-apps/integrate/webhooks) notifications, you might want to request the latest content from your Kontent.ai project.
256
+
257
+ You can check the headers of the response for the **X-Stale-Content** header to check if the response was served from cache:
258
+
259
+ ```ruby
260
+ delivery_client.item('about_us').execute do |response|
261
+ if response.headers[:x_stale_content].eq 1
262
+ ## Content is stale
263
+ end
264
+ end
265
+ ```
266
+
267
+ You can bypass the cache and get the latest content using `request_latest_content`
268
+
269
+ ```ruby
270
+ delivery_client.item('about_us')
271
+ .request_latest_content
272
+ .execute
273
+ ```
274
+
275
+ ### Providing custom headers
276
+
277
+ If you want to pass custom headers in the request, you can use `custom_headers`. This could be useful when you are developing your package on top of the SDK.
278
+
279
+ Note that you can not override internal headers such as `Authorization`. If headers with an existing key are passed into the method, they will be ignored.
280
+
281
+ ```ruby
282
+ delivery_client.items
283
+ .custom_headers({ 'MY-HEADER' => 'HEADER VALUE' })
284
+ .execute
285
+ ```
286
+
287
+ ### Pagination
288
+
289
+ Most responses also contain a `pagination` attribute to access the [paging](https://kontent.ai/learn/reference/delivery-api#operation/list-content-items "paging") data for the Delivery query. This object contains the following attributes:
290
+
291
+ - **skip**
292
+ - **limit**
293
+ - **count**
294
+ - **next_page**
295
+ - **total_count** (only if `include_total_count` is called)
296
+
297
+ For example, to access the next page URL you can use:
298
+
299
+ ```ruby
300
+ delivery_client.items
301
+ .skip(0)
302
+ .limit(5)
303
+ .include_total_count
304
+ .execute do |response|
305
+ next_page_url = response.pagination.next_page
306
+ end
307
+ ```
308
+
309
+ :warning: Note that using the `include_total_count` method may increase the response time and should only be used if necessary.
310
+
311
+ ## Working with content items
312
+
313
+ ### Assets
314
+
315
+ You can use `.get_assets(code_name)` to get one or more assets from the specified element. This method will always return an array, so use `.first` to get the first asset:
316
+
317
+ ```ruby
318
+ url = response.item.get_assets('teaser_image').first.url
319
+ ```
320
+
321
+ ### Linked items
322
+
323
+ You can get a simple array of code names by accessing the element's value:
324
+
325
+ ```ruby
326
+ links = response.item.elements.facts.value
327
+ ```
328
+
329
+ The `.get_links(element)` method will return an array of ContentItems instead:
330
+
331
+ ```ruby
332
+ response.item.get_links('facts').each do |link|
333
+ title = link.elements.title.value
334
+ end
335
+ ```
336
+
337
+ ### Resolving links
338
+
339
+ If a rich text element contains links to other content items, you will need to generate the URLs to those items. You can do this by registering a `Kontent::Ai::Delivery::Resolvers::ContentLinkResolver` when you instantiate the DeliveryClient. When you create a ContentLinkResolver, you must pass a method that will return the URL, and you may pass another method that will be called if the content contains a link, but the content item is not present in the response:
340
+
341
+ ```ruby
342
+ link_resolver = Kontent::Ai::Delivery::Resolvers::ContentLinkResolver.new(lambda do |link|
343
+ # Link valid
344
+ return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
345
+ return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
346
+ end, lambda do |id|
347
+ # Link broken
348
+ return "/notfound?id=#{id}"
349
+ end)
350
+ delivery_client = Kontent::Ai::Delivery::DeliveryClient.new project_id: '<your-project-id>',
351
+ content_link_url_resolver: link_resolver
352
+ ```
353
+
354
+ You can also build the logic for your resolver in a separate class and register an instance of that class in the DeliveryClient. The class must extend `Kontent::Ai::Delivery::Resolvers::ContentLinkResolver` and contain a `resolve_link(link)` method, as well as the `resolve_404(id)` method for broken links. For example, you can create `MyLinkResolver.rb`:
355
+
356
+ ```ruby
357
+ class MyLinkResolver < Kontent::Ai::Delivery::Resolvers::ContentLinkResolver
358
+ def resolve_link(link)
359
+ return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
360
+ return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
361
+ end
362
+
363
+ def resolve_404(id)
364
+ "/notfound?id=#{id}"
365
+ end
366
+ end
367
+ ```
368
+
369
+ Then create an object of this class when instantiating the DeliveryClient:
370
+
371
+ ```ruby
372
+ delivery_client = Kontent::Ai::Delivery::DeliveryClient.new project_id: '<your-project-id>',
373
+ content_link_url_resolver: MyLinkResolver.new
374
+ ```
375
+
376
+ You can pass a `ContentLinkResolver` to the DeliveryQuery instead of the client if you only want to resolve links for that query, or they should be resolved differently:
377
+
378
+ ```ruby
379
+ delivery_client = Kontent::Ai::Delivery::DeliveryClient.new project_id: '<your-project-id>'
380
+ # Client doesn't use ContentLinkResolver, but query below will
381
+ delivery_client.items
382
+ .with_link_resolver MyLinkResolver.new
383
+ ```
384
+
385
+ The `ContentLink` object that is passed to your resolver contains the following attributes:
386
+
387
+ - **id**: the system.id of the linked content item
388
+ - **code_name**: the system.codename of the linked content item
389
+ - **type**: the content type of the linked content item
390
+ - **url_slug**: the URL slug of the linked content item, or nil if there is none
391
+
392
+ To resolve links in rich text elements, you must retrieve the text using `get_string`:
393
+
394
+ ```ruby
395
+ item_resolver = Kontent::Ai::Delivery::Resolvers::ContentLinkResolver.new(lambda do |link|
396
+ return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
397
+ return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
398
+ end)
399
+ delivery_client = Kontent::Ai::Delivery::DeliveryClient.new project_id: '<your-project-id>',
400
+ content_link_url_resolver: item_resolver
401
+ delivery_client.item('coffee_processing_techniques').execute do |response|
402
+ text = response.item.get_string 'body_copy'
403
+ end
404
+ ```
405
+
406
+ ### Resolving inline content
407
+
408
+ Existing content items can be inserted into a rich text element, or you can create new content items as components. You need to resolve these in your application just as with content links. You can register a resolver when you instantiate the client by passing it with the hash key `inline_content_item_resolver`:
409
+
410
+ ```ruby
411
+ item_resolver = Kontent::Ai::Delivery::Resolvers::InlineContentItemResolver.new(lambda do |item|
412
+ return "<h1>#{item.elements.zip_code.value}</h1>" if item.system.type.eql? 'cafe'
413
+ return "<div>$#{item.elements.price.value}</div>" if item.system.type.eql? 'brewer'
414
+ end)
415
+ delivery_client = Kontent::Ai::Delivery::DeliveryClient.new project_id: '<your-project-id>',
416
+ inline_content_item_resolver: item_resolver
417
+ ```
418
+
419
+ The object passed to the resolving method is a complete ContentItem. Similar to content link resolvers, you can create your own class which extends `Kontent::Ai::Delivery::Resolvers::InlineContentItemResolver` and implements the `resolve_item` method:
420
+
421
+ ```ruby
422
+ class MyItemResolver < Kontent::Ai::Delivery::Resolvers::InlineContentItemResolver
423
+ def resolve_item(item)
424
+ return "<h1>#{item.elements.zip_code.value}</h1>" if item.system.type.eql? 'cafe'
425
+ return "<div>$#{item.elements.price.value}</div>" if item.system.type.eql? 'brewer'
426
+ end
427
+ end
428
+ ```
429
+
430
+ You can also set the inline content resolver per-query:
431
+
432
+ ```ruby
433
+ delivery_client = Kontent::Ai::Delivery::DeliveryClient.new project_id: '<your-project-id>'
434
+ # Client doesn't use InlineContentItemResolver, but query below will
435
+ delivery_client.items
436
+ .with_inline_content_item_resolver MyItemResolver.new
437
+ ```
438
+
439
+ To resolve inline content in elements, you must call `get_string` similar to content item links:
440
+
441
+ ```ruby
442
+ item_resolver = Kontent::Ai::Delivery::Resolvers::InlineContentItemResolver.new(lambda do |item|
443
+ return "<div>$#{item.elements.price.value}</div>" if item.system.type.eql? 'brewer'
444
+ end)
445
+ delivery_client = Kontent::Ai::Delivery::DeliveryClient.new project_id: PROJECT_ID,
446
+ inline_content_item_resolver: item_resolver
447
+ delivery_client.item('our_brewers').execute do |response|
448
+ text = response.item.get_string 'body_copy'
449
+ end
450
+ ```
451
+
452
+ ## Items feed
453
+
454
+ Use the `items_feed` method to retrieve a dynamically paginated list of content items in your project. The result will have a `more_results?` method which indicates that more items can be retrieved from the feed, using the `next_result` method.
455
+
456
+ This method accepts all [filtering](#filtering) and [parameters](https://github.com/kontent-ai/delivery-sdk-ruby#parameters) except _depth_, _skip_, and _limit_. You can read more about the /items-feed endpoint in the [Delivery API reference](https://kontent.ai/learn/reference/delivery-api#operation/enumerate-content-items)
457
+
458
+ Below is an example that will load all content items of a project into a single array:
459
+
460
+ ```ruby
461
+ result = delivery_client.items_feed.execute
462
+ items = result.items
463
+ if result.more_results?
464
+ loop do
465
+ result = result.next_result
466
+ items.push *result.items
467
+ break unless result.more_results?
468
+ end
469
+ end
470
+ ```
471
+
472
+ ## Retrieving content types
473
+
474
+ You can use the `.type` and `.types` methods to request your content types from Kontent.ai:
475
+
476
+ ```ruby
477
+ delivery_client.types.execute do |response|
478
+ # Do something
479
+ end
480
+ delivery_client.type('coffee').execute do |response|
481
+ # Do something
482
+ end
483
+ ```
484
+
485
+ As with content item queries, all content type queries will return a `Kontent::Ai::Delivery::Responses::ResponseBase` of the class `DeliveryTypeResponse` or `DeliveryTypeListingResponse` for single and multiple type queries, respectively.
486
+
487
+ For multiple type queries, you can access the array of `ContentType` objects at `.types`, and at `.type` for singe type queries. You can access information about the type(s) dynamically:
488
+
489
+ ```ruby
490
+ delivery_client.type('coffee').execute do |response|
491
+ field_type = response.type.elements.product_status.type # taxonomy
492
+ end
493
+ ```
494
+
495
+ The DeliveryTypeListingResponse also contains pagination data, similar to DeliveryItemListingResponse.
496
+
497
+ ## Retrieving taxonomy
498
+
499
+ Use the `.taxonomies` and `.taxonomy(code_name)` endpoints to get information about the taxonomy in your project:
500
+
501
+ ```ruby
502
+ # Get all taxonomies
503
+ delivery_client.taxonomies.execute do |response|
504
+ response.taxonomies.each do |tax|
505
+ puts "#{tax.system.name} (#{tax.terms.length})"
506
+ end
507
+ end
508
+
509
+ # Get terms of specific taxonomy
510
+ delivery_client.taxonomy('personas').execute do |response|
511
+ puts response.taxonomy.terms.length
512
+ end
513
+ ```
514
+
515
+ Each response will return either a single `Kontent::Ai::Delivery::TaxonomyGroup` or an array of groups. The taxonomy group(s) are accessible at `.taxonomy` and `.taxonomies` for single and multiple queries, respectively.
516
+
517
+ The `TaxonomyGroup` object contains two attributes `.system` and `.terms` which are dynamic OStruct objects containing the same elements as a standard JSON reponse. For example, given a successful query you could access information about the first term of a group using:
518
+
519
+ ```ruby
520
+ taxonomy_group.terms[0].codename
521
+ ```
522
+
523
+ Note that the terms of a taxonomy group may also contain terms, for example in Dancing Goat's **Personas** taxonomy group, which looks like this:
524
+
525
+ - Coffee expert
526
+ - Barista
527
+ - Cafe owner
528
+ - Coffee enthusiast
529
+ - Coffee lover
530
+ - Coffee blogger
531
+
532
+ To get the code name of the first term under the "Coffee expert" term, you could do this:
533
+
534
+ ```ruby
535
+ delivery_client.taxonomy('personas').execute do |response|
536
+ puts response.taxonomy.terms[0].terms[0].codename
537
+ end
538
+ ```
539
+
540
+ ## Retrieving content type elements
541
+
542
+ Kontent.ai provides an [endpoint](https://kontent.ai/learn/reference/delivery-api#operation/retrieve-a-content-element) for obtaining details about a specific element of a content type. In the Ruby SDK, you can use the `.element` method:
543
+
544
+ ```ruby
545
+ delivery_client.element('brewer', 'product_status').execute do |response|
546
+ puts response.element.type # taxonomy
547
+ end
548
+ ```
549
+
550
+ This returns a `Kontent::Ai::Delivery::Responses::DeliveryElementResponse` where the `element` attribute is a dynamic OStruct representation of the JSON response. This means that you can access any property of the element by simply typing the name as in the above example.
551
+
552
+ The element will always contain **codename**, **type**, and **name**, but multiple choice elements will also contain **options** and taxonomy elements will contain **taxonomy_group**. The Ruby SDK fully supports obtaining [custom elements](https://kontent.ai/learn/reference/custom-elements-js-api) using this approach and any other methods.
553
+
554
+ ## Retrieving languages
555
+
556
+ Use the `.languages` method to list all of the languages in the project:
557
+
558
+ ```ruby
559
+ delivery_client.languages.execute do |response|
560
+ puts response.languages.length # number of languages
561
+ end
562
+ ```
563
+
564
+ The response is a `Kontent::Ai::Delivery::Responses::DeliveryLanguageListingResponse` where `languages` is an array of all langauges. You can access the system properties of each language as they are returned by Kontent:
565
+
566
+ ```ruby
567
+ delivery_client.languages.execute do |response|
568
+ puts response.languages[0].system.codename # en-us
569
+ end
570
+ ```
571
+
572
+ ## Image transformation
573
+
574
+ When you've obtained the URL for an asset, you can use our [Image Transformation API](https://kontent.ai/learn/reference/image-transformation) to make on-the-fly modifications to the image. To do this, use the static `.transform` method of `Kontent::Ai::Delivery::Builders::ImageTransformationBuilder`, then call the transformation methods. When you're done, call the `.url` method to get the new URL:
575
+
576
+ ```ruby
577
+ url = response.item.get_assets('teaser_image').first.url
578
+ url = Kontent::Ai::Delivery::Builders::ImageTransformationBuilder.transform(url)
579
+ # methods...
580
+ .url
581
+ ```
582
+
583
+ The available methods are:
584
+
585
+ | Method | Possible values | REST example |
586
+ | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- |
587
+ | `.with_width` | positive integer, or float between 0 and 1 | ?w=200 |
588
+ | `.with_height` | positive integer, or float between 0 and 1 | ?h=200 |
589
+ | `.with_pixel_ratio` | float greater than 0 but less than 5 | ?dpr=1.5 |
590
+ | `.with_fit_mode` | constants available at `Kontent::Ai::Delivery::Builders::ImageTransformationBuilder` <ul><li>FITMODE_CROP</li><li>FITMODE_CLIP</li><li>FITMODE_SCALE</li></ul> | ?fit=crop |
591
+ | `.with_rect` | 4 integer values representing pixels or floats representing percentages | rect=100,100,0.7,0.7 |
592
+ | `.with_focal_point` | 2 floats between 0 and 1 and one integer between 1 and 100 | ?fp-x=0.2&fp-y=0.7&fp-z=5 |
593
+ | `.with_background_color` | string containing 3, 4, 6, or 8 characters | ?bg=7A0099EE |
594
+ | `.with_output_format` | constants available at `Kontent::Ai::Delivery::Builders::ImageTransformationBuilder` <ul><li>FORMAT_GIF</li><li>FORMAT_PNG</li><li>FORMAT_PNG8</li><li>FORMAT_JPG</li><li>FORMAT_PJPG</li><li>FORMAT_WEBP</li></ul> | ?fm=webp |
595
+ | `.with_quality` | integer between 1 to 100 | ?quality=50 |
596
+ | `.with_lossless` | 'true', 'false', 0, or 1 | ?lossless=1 |
597
+ | `.with_auto_format_selection` | 'true', 'false', 0, or 1 | ?auto=format |
598
+
599
+ ## Feedback & Contributing
600
+
601
+ Check out the [contributing](CONTRIBUTING.md) page to see the best places to file issues, start discussions, and begin contributing.
602
+
603
+ ## License
604
+
605
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
606
+
607
+ ## Code of Conduct
608
+
609
+ Everyone interacting in the Delivery project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
610
+
611
+ ## Wall of Fame
612
+
613
+ We would like to express our thanks to the following people who contributed and made the project possible:
614
+
615
+ - [Eric Dugre](https://github.com/kentico-ericd) - the original author of the SDK
616
+
617
+ Would you like to become a hero too? Pick an [issue](https://github.com/kontent-ai/delivery-sdk-ruby/issues) and send us a pull request!
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "delivery"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here