kontent-delivery-sdk-ruby 2.0.16 → 2.0.22
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +196 -157
- data/lib/delivery/builders/image_transformation_builder.rb +1 -0
- data/lib/delivery/builders/url_builder.rb +3 -0
- data/lib/delivery/client/delivery_client.rb +8 -0
- data/lib/delivery/client/delivery_query.rb +2 -2
- data/lib/delivery/client/request_manager.rb +2 -0
- data/lib/delivery/models/language.rb +29 -0
- data/lib/delivery/query_parameters/filters.rb +45 -2
- data/lib/delivery/query_parameters/parameter_base.rb +17 -7
- data/lib/delivery/responses/delivery_language_listing_response.rb +44 -0
- data/lib/delivery/responses/response_base.rb +2 -2
- data/lib/delivery/tests/fake_responder.rb +1 -6
- data/lib/delivery/tests/filtering/items_with_count.json +5241 -4842
- data/lib/delivery/tests/filtering/{pagination_about_us.json → pagination.json} +179 -64
- data/lib/delivery/tests/generic/items.json +1131 -732
- data/lib/delivery/tests/generic/items/about_us.json +76 -27
- data/lib/delivery/tests/generic/items/aeropress_filters.json +28 -11
- data/lib/delivery/tests/generic/items/coffee_processing_techniques.json +409 -12
- data/lib/delivery/tests/generic/items/empty_rich_text.json +24 -0
- data/lib/delivery/tests/generic/items/rich_text_complex_tables.json +81 -0
- data/lib/delivery/tests/generic/items/where_does_coffee_come_from_.json +478 -500
- data/lib/delivery/tests/generic/languages.json +24 -0
- data/lib/delivery/tests/generic/taxonomies.json +78 -1
- data/lib/delivery/tests/generic/taxonomies/manufacturer.json +30 -0
- data/lib/delivery/tests/generic/types.json +808 -753
- data/lib/delivery/tests/generic/types/brewer.json +89 -0
- data/lib/kontent-delivery-sdk-ruby.rb +2 -0
- metadata +49 -27
- data/lib/delivery/tests/filtering/items_gt.json +0 -566
- data/lib/delivery/tests/filtering/multiple.json +0 -283
- data/lib/delivery/version.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b93d8bae053c876e19256fc6695fa2dd593dc9479f89da1ede61ec166316a0cb
|
4
|
+
data.tar.gz: 21721f7c4105cc9c07e62009b01b5d380f485082d807a1337002d44eaebc52fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8df410c1f68405b40824f82fe7ac866de358e8df90042419bc52406807bd89610adabdbd5dcaf8c4a9bf751bc56882ff670c2d4c704bd1bc5e21ee49bea8927a
|
7
|
+
data.tar.gz: 15b2ebbe2e3a98b5a4f5e66e6d7af39c04c6db4d8652cd061f9bf9c0824c4dda87e522f4e7d4b812d2e84a378e9348e2a62c132018ad8032fb6b01b1801be750
|
data/README.md
CHANGED
@@ -1,36 +1,42 @@
|
|
1
|
-
|
1
|
+
![build](https://github.com/Kentico/kontent-delivery-sdk-ruby/actions/workflows/build.yml/badge.svg)
|
2
2
|
[![Join the chat at https://kentico-community.slack.com](https://img.shields.io/badge/join-slack-E6186D.svg)](https://kentico-community.slack.com)
|
3
3
|
[![Stack Overflow](https://img.shields.io/badge/Stack%20Overflow-ASK%20NOW-FE7A16.svg?logo=stackoverflow&logoColor=white)](https://stackoverflow.com/tags/kentico-kontent)
|
4
4
|
[![Version](https://img.shields.io/gem/v/kontent-delivery-sdk-ruby.svg?style=flat)](https://rubygems.org/gems/kontent-delivery-sdk-ruby)
|
5
|
-
[![Maintainability](https://api.codeclimate.com/v1/badges/c83f2067f9cae9bde737/maintainability)](https://codeclimate.com/github/Kentico/kontent-delivery-sdk-ruby/maintainability)
|
6
|
-
[![Test Coverage](https://api.codeclimate.com/v1/badges/c83f2067f9cae9bde737/test_coverage)](https://codeclimate.com/github/Kentico/kontent-delivery-sdk-ruby/test_coverage)
|
7
5
|
|
8
6
|
# Delivery Ruby SDK
|
9
7
|
|
10
|
-
![Banner](
|
8
|
+
![Banner](/banner.png)
|
11
9
|
|
12
10
|
The Delivery Ruby SDK can be used in Ruby/Rails projects to retrieve content from Kentico Kontent. This is a community project and not an official Kentico SDK. If you find a bug in the SDK or have a feature request, please submit a GitHub issue.
|
13
11
|
|
14
|
-
See [How to setup a development environment on Windows](https://github.com/Kentico/kontent-delivery-sdk-ruby/wiki/How-to-setup-development-environment-on-Windows) for local development.
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
12
|
+
See [How to setup a development environment on Windows](https://github.com/Kentico/kontent-delivery-sdk-ruby/wiki/How-to-setup-development-environment-on-Windows) for local development, and check out the [Kentico Kontent Blog](https://kontent.ai/blog/creating-a-kentico-cloud-ruby-on-rails-application) for a tutorial on creating a Rails application.
|
13
|
+
|
14
|
+
## Table of contents
|
15
|
+
|
16
|
+
- [Installation](#installation)
|
17
|
+
- [Creating a client](#creating-a-client)
|
18
|
+
- [Previewing unpublished content](#previewing-unpublished-content)
|
19
|
+
- [Making secure requests](#making-secure-requests)
|
20
|
+
- [Retry policy](#retry-policy)
|
21
|
+
- [Custom URLs](#custom-urls)
|
22
|
+
- [Listing items](#listing-items)
|
23
|
+
- [Filtering](#filtering)
|
24
|
+
- [Parameters](#parameters)
|
25
|
+
- [Responses](#responses)
|
26
|
+
- [Requesting the latest content](#requesting-the-latest-content)
|
27
|
+
- [Providing custom headers](#providing-custom-headers)
|
28
|
+
- [Pagination](#pagination)
|
29
|
+
- [Working with content items](#working-with-content-items)
|
30
|
+
- [Assets](#assets)
|
31
|
+
- [Linked items](#linked-items)
|
32
|
+
- [Resolving inline content](#resolving-inline-content)
|
33
|
+
- [Resolving links](#resolving-links)
|
34
|
+
- [Items feed](#items-feed)
|
35
|
+
- [Retrieving content types](#retrieving-content-types)
|
36
|
+
- [Retrieving taxonomy](#retrieving-taxonomy)
|
37
|
+
- [Retrieving content type elements](#retrieving-content-type-elements)
|
38
|
+
- [Retrieving languages](#retrieving-languages)
|
39
|
+
- [Image transformation](#image-transformation)
|
34
40
|
|
35
41
|
## Installation
|
36
42
|
|
@@ -40,7 +46,7 @@ To use the SDK in your own project, add the gem to your Gemfile:
|
|
40
46
|
gem 'kontent-delivery-sdk-ruby'
|
41
47
|
```
|
42
48
|
|
43
|
-
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:
|
49
|
+
Then run `bundle install`. You can also download the gem from [RubyGems.org](https://rubygems.org/gems/kontent-delivery-sdk-ruby). To use the SDK in an `.rb` file, you need to require it:
|
44
50
|
|
45
51
|
```ruby
|
46
52
|
require 'kontent-delivery-sdk-ruby'
|
@@ -63,14 +69,14 @@ delivery_client = KK::DeliveryClient.new project_id: '<your-project-id>'
|
|
63
69
|
|
64
70
|
### Previewing unpublished content
|
65
71
|
|
66
|
-
To enable
|
72
|
+
To [enable preview](https://docs.kontent.ai/tutorials/develop-apps/get-content/configuring-preview-for-content-items "See how to configure your app and Kontent project to enable content preview"), pass the Preview API Key to the constructor:
|
67
73
|
|
68
74
|
```ruby
|
69
75
|
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
|
70
76
|
preview_key: '<your-preview-key>'
|
71
77
|
```
|
72
78
|
|
73
|
-
This enables preview, but you can toggle preview at any time by setting the `use_preview` attribute of DeliveryClient which is
|
79
|
+
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:
|
74
80
|
|
75
81
|
```ruby
|
76
82
|
# For all queries created by client
|
@@ -86,14 +92,14 @@ end
|
|
86
92
|
|
87
93
|
### Making secure requests
|
88
94
|
|
89
|
-
If you've [secured access](https://
|
95
|
+
If you've [secured access](https://docs.kontent.ai/tutorials/develop-apps/get-content/securing-public-access "See how to enable secured access for your Kontent project") to your project, you need to provide the `DeliveryClient` with either the primary or secondary key:
|
90
96
|
|
91
97
|
```ruby
|
92
98
|
Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
|
93
99
|
secure_key: '<your-secure-key>'
|
94
100
|
```
|
95
101
|
|
96
|
-
You can then request
|
102
|
+
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.
|
97
103
|
|
98
104
|
### Retry policy
|
99
105
|
|
@@ -116,6 +122,18 @@ Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
|
|
116
122
|
with_retry_policy: false
|
117
123
|
```
|
118
124
|
|
125
|
+
### Custom URLs
|
126
|
+
|
127
|
+
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:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
delivery_client.items
|
131
|
+
.url('https://deliver.kontent.ai/<your-project-id>/items?system.type=grinder')
|
132
|
+
.execute do |response|
|
133
|
+
# Do something
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
119
137
|
## Listing items
|
120
138
|
|
121
139
|
|
@@ -137,7 +155,7 @@ response = delivery_client.items.execute
|
|
137
155
|
|
138
156
|
### Filtering
|
139
157
|
|
140
|
-
You can use [filtering](https://
|
158
|
+
You can use [filtering](https://docs.kontent.ai/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:
|
141
159
|
|
142
160
|
|Method|Example|REST equivalent|
|
143
161
|
|--|--|--|
|
@@ -145,12 +163,16 @@ You can use [filtering](https://developer.kenticocloud.com/v1/reference#content-
|
|
145
163
|
|any|`'elements.processing'.any %w[dry__natural_ semi_dry]`|?elements.processing[any]=dry__natural_,semi_dry|
|
146
164
|
|contains|`'elements.related_articles'.contains 'on_roasts'`|?elements.related_articles[contains]=on_roasts|
|
147
165
|
|eq|`'system.type'.eq 'grinder'`|?system.type=grinder|
|
166
|
+
|not_eq|`'elements.region'.not_eq 'USA'`|?elements.region[neq]=USA|
|
148
167
|
|gt|`'elements.price'.gt 20`|?elements.price[gt]=20|
|
149
168
|
|gt_or_eq|`'elements.price'.gt_or_eq 20`|?elements.price[gte]=20|
|
150
169
|
|in|`'system.type'.in %w[coffee brewer]`|?system.type[in]=coffee,brewer|
|
170
|
+
|not_in|`'elements.author'.not_in %w[mberry ericd anthonym]`|?elements.author[nin]=mberry,ericd,anthonym|
|
151
171
|
|lt|`'elements.price'.lt 20`|?elements.price[lt]=20|
|
152
172
|
|lt_or_eq|`'elements.price'.lt_or_eq 20`|?elements.price[lte]=20|
|
153
173
|
|range|`'system.last_modified'.range %w[2018-02-01 2018-03-31]`|?system.last_modified[range]=2018-02-01,2018-03-31|
|
174
|
+
|empty|`'elements.banned_reason'.empty`|?elements.banned_reason[empty]|
|
175
|
+
|not_empty|`'elements.status'.not_empty`|?elements.status[nempty]|
|
154
176
|
|
155
177
|
You can pass a single filter or multiple filters in the DeliveryClient methods. For example:
|
156
178
|
|
@@ -171,12 +193,12 @@ The `.item` and `.items` methods return a `Kentico::Kontent::Delivery::DeliveryQ
|
|
171
193
|
|
172
194
|
|Method|Example|REST equivalent
|
173
195
|
|--|--|--|
|
174
|
-
|[order_by](https://
|
175
|
-
|[skip](https://
|
176
|
-
|[limit](https://
|
177
|
-
|[elements](https://
|
178
|
-
|[depth](https://
|
179
|
-
|[language](https://
|
196
|
+
|[order_by](https://docs.kontent.ai/reference/delivery-api#operation/list-content-items "order_by")|`order_by 'system.last_modified' '[desc]'`|?order=system.last_modified[desc]
|
197
|
+
|[skip](https://docs.kontent.ai/reference/delivery-api#operation/list-content-items "skip")|`skip 5`|?skip=5
|
198
|
+
|[limit](https://docs.kontent.ai/reference/delivery-api#operation/list-content-items "limit")|`limit 5`|?limit=5
|
199
|
+
|[elements](https://docs.kontent.ai/reference/delivery-api#tag/Projection "elements")|`elements %w[price product_name image]`|?elements=price,product_name,image
|
200
|
+
|[depth](https://docs.kontent.ai/reference/delivery-api#tag/Linked-content-and-components/linked-content-depth "depth")|`depth 0`|?depth=0
|
201
|
+
|[language](https://docs.kontent.ai/tutorials/set-up-projects/set-up-languages/localization-in-kentico-kontent#a-understanding-language-fallbacks "language")|`language 'en'`|?language=en
|
180
202
|
|
181
203
|
For example:
|
182
204
|
|
@@ -224,7 +246,7 @@ price = response.item.elements.price.value
|
|
224
246
|
|
225
247
|
### Requesting the latest content
|
226
248
|
|
227
|
-
Kentico caches content using Fastly, so requests made to Kentico Kontent may not be up-to-date. In some cases, such as when reacting to [webhook](https://
|
249
|
+
Kentico caches content using Fastly, so requests made to Kentico Kontent may not be up-to-date. In some cases, such as when reacting to [webhook](https://docs.kontent.ai/tutorials/develop-apps/integrate/using-webhooks-for-automatic-updates) notifications, you might want to request the latest content from your Kentico Kontent project.
|
228
250
|
|
229
251
|
You can check the headers of the response for the **X-Stale-Content** header to check if the response was served from cache:
|
230
252
|
|
@@ -256,18 +278,32 @@ delivery_client.items
|
|
256
278
|
.execute
|
257
279
|
```
|
258
280
|
|
259
|
-
###
|
281
|
+
### Pagination
|
260
282
|
|
261
|
-
|
283
|
+
Most responses also contain a `pagination` attribute to access the [paging](https://docs.kontent.ai/reference/delivery-api#operation/list-content-items "paging") data for the Delivery query. This object contains the following attributes:
|
284
|
+
|
285
|
+
- **skip**
|
286
|
+
- **limit**
|
287
|
+
- **count**
|
288
|
+
- **next_page**
|
289
|
+
- **total_count** (only if `include_total_count` is called)
|
290
|
+
|
291
|
+
For example, to access the next page URL you can use:
|
262
292
|
|
263
293
|
```ruby
|
264
294
|
delivery_client.items
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
295
|
+
.skip(0)
|
296
|
+
.limit(5)
|
297
|
+
.include_total_count
|
298
|
+
.execute do |response|
|
299
|
+
next_page_url = response.pagination.next_page
|
300
|
+
end
|
269
301
|
```
|
270
302
|
|
303
|
+
:warning: Note that using the `include_total_count` method may increase the response time and should only be used if necessary.
|
304
|
+
|
305
|
+
## Working with content items
|
306
|
+
|
271
307
|
### Assets
|
272
308
|
|
273
309
|
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:
|
@@ -291,36 +327,126 @@ response.item.get_links('facts').each do |link|
|
|
291
327
|
title = link.elements.title.value
|
292
328
|
end
|
293
329
|
```
|
330
|
+
### Resolving inline content
|
294
331
|
|
295
|
-
|
332
|
+
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`:
|
296
333
|
|
297
|
-
|
334
|
+
```ruby
|
335
|
+
item_resolver = Kentico::Kontent::Delivery::Resolvers::InlineContentItemResolver.new(lambda do |item|
|
336
|
+
return "<h1>#{item.elements.zip_code.value}</h1>" if item.system.type.eql? 'cafe'
|
337
|
+
return "<div>$#{item.elements.price.value}</div>" if item.system.type.eql? 'brewer'
|
338
|
+
end)
|
339
|
+
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
|
340
|
+
inline_content_item_resolver: item_resolver
|
341
|
+
```
|
298
342
|
|
299
|
-
|
300
|
-
- **limit**
|
301
|
-
- **count**
|
302
|
-
- **next_page**
|
303
|
-
- **total_count** (only if `include_total_count` is called)
|
343
|
+
The object passed to the resolving method is a complete ContentItem. Similar to content link resolvers, you can create your own class which extends `Kentico::Kontent::Delivery::Resolvers::InlineContentItemResolver` and implements the `resolve_item` method:
|
304
344
|
|
305
|
-
|
345
|
+
```ruby
|
346
|
+
class MyItemResolver < Kentico::Kontent::Delivery::Resolvers::InlineContentItemResolver
|
347
|
+
def resolve_item(item)
|
348
|
+
return "<h1>#{item.elements.zip_code.value}</h1>" if item.system.type.eql? 'cafe'
|
349
|
+
return "<div>$#{item.elements.price.value}</div>" if item.system.type.eql? 'brewer'
|
350
|
+
end
|
351
|
+
end
|
352
|
+
```
|
353
|
+
|
354
|
+
You can also set the inline content resolver per-query:
|
306
355
|
|
307
356
|
```ruby
|
357
|
+
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>'
|
358
|
+
# Client doesn't use InlineContentItemResolver, but query below will
|
308
359
|
delivery_client.items
|
309
|
-
|
310
|
-
.limit(5)
|
311
|
-
.include_total_count
|
312
|
-
.execute do |response|
|
313
|
-
next_page_url = response.pagination.next_page
|
314
|
-
end
|
360
|
+
.with_inline_content_item_resolver MyItemResolver.new
|
315
361
|
```
|
316
362
|
|
317
|
-
|
363
|
+
To resolve inline content in elements, you must call `get_string` similar to content item links:
|
364
|
+
|
365
|
+
```ruby
|
366
|
+
item_resolver = Kentico::Kontent::Delivery::Resolvers::InlineContentItemResolver.new(lambda do |item|
|
367
|
+
return "<div>$#{item.elements.price.value}</div>" if item.system.type.eql? 'brewer'
|
368
|
+
end)
|
369
|
+
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: PROJECT_ID,
|
370
|
+
inline_content_item_resolver: item_resolver
|
371
|
+
delivery_client.item('our_brewers').execute do |response|
|
372
|
+
text = response.item.get_string 'body_copy'
|
373
|
+
end
|
374
|
+
```
|
375
|
+
|
376
|
+
### Resolving links
|
377
|
+
|
378
|
+
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 `Kentico::Kontent::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:
|
379
|
+
|
380
|
+
```ruby
|
381
|
+
link_resolver = Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver.new(lambda do |link|
|
382
|
+
# Link valid
|
383
|
+
return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
|
384
|
+
return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
|
385
|
+
end, lambda do |id|
|
386
|
+
# Link broken
|
387
|
+
return "/notfound?id=#{id}"
|
388
|
+
end)
|
389
|
+
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
|
390
|
+
content_link_url_resolver: link_resolver
|
391
|
+
```
|
392
|
+
|
393
|
+
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 `Kentico::Kontent::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`:
|
394
|
+
|
395
|
+
```ruby
|
396
|
+
class MyLinkResolver < Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver
|
397
|
+
def resolve_link(link)
|
398
|
+
return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
|
399
|
+
return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
|
400
|
+
end
|
401
|
+
|
402
|
+
def resolve_404(id)
|
403
|
+
"/notfound?id=#{id}"
|
404
|
+
end
|
405
|
+
end
|
406
|
+
```
|
407
|
+
|
408
|
+
Then create an object of this class when instantiating the DeliveryClient:
|
409
|
+
|
410
|
+
```ruby
|
411
|
+
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
|
412
|
+
content_link_url_resolver: MyLinkResolver.new
|
413
|
+
```
|
414
|
+
|
415
|
+
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:
|
416
|
+
|
417
|
+
```ruby
|
418
|
+
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>'
|
419
|
+
# Client doesn't use ContentLinkResolver, but query below will
|
420
|
+
delivery_client.items
|
421
|
+
.with_link_resolver MyLinkResolver.new
|
422
|
+
```
|
423
|
+
|
424
|
+
The `ContentLink` object that is passed to your resolver contains the following attributes:
|
425
|
+
|
426
|
+
- **id**: the system.id of the linked content item
|
427
|
+
- **code_name**: the system.codename of the linked content item
|
428
|
+
- **type**: the content type of the linked content item
|
429
|
+
- **url_slug**: the URL slug of the linked content item, or nil if there is none
|
430
|
+
|
431
|
+
To resolve links in rich text elements, you must retrieve the text using `get_string`:
|
432
|
+
|
433
|
+
```ruby
|
434
|
+
item_resolver = Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver.new(lambda do |link|
|
435
|
+
return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
|
436
|
+
return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
|
437
|
+
end)
|
438
|
+
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
|
439
|
+
content_link_url_resolver: item_resolver
|
440
|
+
delivery_client.item('coffee_processing_techniques').execute do |response|
|
441
|
+
text = response.item.get_string 'body_copy'
|
442
|
+
end
|
443
|
+
```
|
318
444
|
|
319
445
|
## Items feed
|
320
446
|
|
321
447
|
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.
|
322
448
|
|
323
|
-
This method accepts all [filtering](
|
449
|
+
This method accepts all [filtering](#filtering) and [parameters](https://github.com/Kentico/kontent-delivery-sdk-ruby#parameters) except _depth_, _skip_, and _limit_. You can read more about the /items-feed endpoint in the [Kontent documentation](https://docs.kontent.ai/reference/delivery-api#operation/enumerate-content-items)
|
324
450
|
|
325
451
|
Below is an example that will load all content items of a project into a single array:
|
326
452
|
|
@@ -349,8 +475,6 @@ delivery_client.type('coffee').execute do |response|
|
|
349
475
|
end
|
350
476
|
```
|
351
477
|
|
352
|
-
### Responses
|
353
|
-
|
354
478
|
As with content item queries, all content type queries will return a `Kentico::Kontent::Delivery::Responses::ResponseBase` of the class `DeliveryTypeResponse` or `DeliveryTypeListingResponse` for single and multiple type queries, respectively.
|
355
479
|
|
356
480
|
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:
|
@@ -362,7 +486,7 @@ end
|
|
362
486
|
```
|
363
487
|
The DeliveryTypeListingResponse also contains pagination data, similar to DeliveryItemListingResponse.
|
364
488
|
|
365
|
-
##
|
489
|
+
## Retrieving taxonomy
|
366
490
|
|
367
491
|
Use the `.taxonomies` and `.taxonomy(code_name)` endpoints to get information about the taxonomy in your project:
|
368
492
|
|
@@ -407,7 +531,7 @@ end
|
|
407
531
|
|
408
532
|
## Retrieving content type elements
|
409
533
|
|
410
|
-
Kentico Kontent provides an [endpoint](https://
|
534
|
+
Kentico Kontent provides an [endpoint](https://docs.kontent.ai/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:
|
411
535
|
|
412
536
|
```ruby
|
413
537
|
delivery_client.element('brewer', 'product_status').execute do |response|
|
@@ -417,113 +541,29 @@ end
|
|
417
541
|
|
418
542
|
This returns a `Kentico::Kontent::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.
|
419
543
|
|
420
|
-
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://
|
544
|
+
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://docs.kontent.ai/reference/custom-elements-js-api) using this approach and any other methods.
|
421
545
|
|
422
|
-
##
|
546
|
+
## Retrieving languages
|
423
547
|
|
424
|
-
|
548
|
+
Use the `.languages` method to list all of the languages in the project:
|
425
549
|
|
426
550
|
```ruby
|
427
|
-
|
428
|
-
|
429
|
-
return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
|
430
|
-
return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
|
431
|
-
end, lambda do |id|
|
432
|
-
# Link broken
|
433
|
-
return "/notfound?id=#{id}"
|
434
|
-
end)
|
435
|
-
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
|
436
|
-
content_link_url_resolver: link_resolver
|
437
|
-
```
|
438
|
-
|
439
|
-
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 `Kentico::Kontent::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`:
|
440
|
-
|
441
|
-
```ruby
|
442
|
-
class MyLinkResolver < Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver
|
443
|
-
def resolve_link(link)
|
444
|
-
return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
|
445
|
-
return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
|
446
|
-
end
|
447
|
-
|
448
|
-
def resolve_404(id)
|
449
|
-
"/notfound?id=#{id}"
|
450
|
-
end
|
551
|
+
delivery_client.languages.execute do |response|
|
552
|
+
puts response.languages.length # number of languages
|
451
553
|
end
|
452
554
|
```
|
453
555
|
|
454
|
-
|
556
|
+
The response is a `Kentico::Kontent::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:
|
455
557
|
|
456
558
|
```ruby
|
457
|
-
delivery_client
|
458
|
-
|
459
|
-
```
|
460
|
-
|
461
|
-
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:
|
462
|
-
|
463
|
-
```ruby
|
464
|
-
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>'
|
465
|
-
# Client doesn't use ContentLinkResolver, but query below will
|
466
|
-
delivery_client.items
|
467
|
-
.with_link_resolver MyLinkResolver.new
|
468
|
-
```
|
469
|
-
|
470
|
-
The `ContentLink` object that is passed to your resolver contains the following attributes:
|
471
|
-
|
472
|
-
- **id**: the system.id of the linked content item
|
473
|
-
- **code_name**: the system.codename of the linked content item
|
474
|
-
- **type**: the content type of the linked content item
|
475
|
-
- **url_slug**: the URL slug of the linked content item, or nil if there is none
|
476
|
-
|
477
|
-
To resolve links in rich text elements, you must retrieve the text using `get_string`:
|
478
|
-
|
479
|
-
```ruby
|
480
|
-
item_resolver = Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver.new(lambda do |link|
|
481
|
-
return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
|
482
|
-
return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
|
483
|
-
end)
|
484
|
-
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
|
485
|
-
content_link_url_resolver: item_resolver
|
486
|
-
delivery_client.item('coffee_processing_techniques').execute do |response|
|
487
|
-
text = response.item.get_string 'body_copy'
|
488
|
-
end
|
489
|
-
```
|
490
|
-
|
491
|
-
## Resolving inline content
|
492
|
-
|
493
|
-
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`:
|
494
|
-
|
495
|
-
```ruby
|
496
|
-
item_resolver = Kentico::Kontent::Delivery::Resolvers::InlineContentItemResolver.new(lambda do |item|
|
497
|
-
return "<h1>#{item.elements.zip_code.value}</h1>" if item.system.type.eql? 'cafe'
|
498
|
-
return "<div>$#{item.elements.price.value}</div>" if item.system.type.eql? 'brewer'
|
499
|
-
end)
|
500
|
-
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
|
501
|
-
inline_content_item_resolver: item_resolver
|
502
|
-
```
|
503
|
-
|
504
|
-
The object passed to the resolving method is a complete ContentItem. Similar to content link resolvers, you can create your own class which extends `Kentico::Kontent::Delivery::Resolvers::InlineContentItemResolver` and implements the `resolve_item` method:
|
505
|
-
|
506
|
-
```ruby
|
507
|
-
class MyItemResolver < Kentico::Kontent::Delivery::Resolvers::InlineContentItemResolver
|
508
|
-
def resolve_item(item)
|
509
|
-
return "<h1>#{item.elements.zip_code.value}</h1>" if item.system.type.eql? 'cafe'
|
510
|
-
return "<div>$#{item.elements.price.value}</div>" if item.system.type.eql? 'brewer'
|
511
|
-
end
|
559
|
+
delivery_client.languages.execute do |response|
|
560
|
+
puts response.languages[0].system.codename # en-us
|
512
561
|
end
|
513
562
|
```
|
514
563
|
|
515
|
-
You can also set the inline content resolver per-query:
|
516
|
-
|
517
|
-
```ruby
|
518
|
-
delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>'
|
519
|
-
# Client doesn't use InlineContentItemResolver, but query below will
|
520
|
-
delivery_client.items
|
521
|
-
.with_inline_content_item_resolver MyItemResolver.new
|
522
|
-
```
|
523
|
-
|
524
564
|
## Image transformation
|
525
565
|
|
526
|
-
When you've obtained the URL for an asset, you can use our [Image Transformation API](https://
|
566
|
+
When you've obtained the URL for an asset, you can use our [Image Transformation API](https://docs.kontent.ai/reference/image-transformation) to make on-the-fly modifications to the image. To do this, use the static `.transform` method of `Kentico::Kontent::Delivery::Builders::ImageTransformationBuilder`, then call the transformation methods. When you're done, call the `.url` method to get the new URL:
|
527
567
|
|
528
568
|
```ruby
|
529
569
|
url = response.item.get_assets('teaser_image').first.url
|
@@ -560,4 +600,3 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
560
600
|
|
561
601
|
Everyone interacting in the Delivery project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Kentico/kontent-delivery-sdk-net/blob/master/CODE_OF_CONDUCT.md).
|
562
602
|
|
563
|
-
![Analytics](https://kentico-ga-beacon.azurewebsites.net/api/UA-69014260-4/Kentico/kontent-delivery-sdk-ruby?pixel)
|