kontent-delivery-sdk-ruby 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +21 -0
  3. data/README.md +511 -0
  4. data/bin/console +14 -0
  5. data/bin/setup +8 -0
  6. data/lib/delivery/builders/image_transformation_builder.rb +271 -0
  7. data/lib/delivery/builders/url_builder.rb +117 -0
  8. data/lib/delivery/client/delivery_client.rb +155 -0
  9. data/lib/delivery/client/delivery_query.rb +249 -0
  10. data/lib/delivery/client/request_manager.rb +108 -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/pagination.rb +21 -0
  14. data/lib/delivery/models/taxonomy_group.rb +39 -0
  15. data/lib/delivery/query_parameters/filters.rb +158 -0
  16. data/lib/delivery/query_parameters/parameter_base.rb +44 -0
  17. data/lib/delivery/query_parameters/query_string.rb +78 -0
  18. data/lib/delivery/resolvers/content_link_resolver.rb +102 -0
  19. data/lib/delivery/resolvers/inline_content_item_resolver.rb +75 -0
  20. data/lib/delivery/resolvers/linked_item_resolver.rb +37 -0
  21. data/lib/delivery/responses/delivery_element_response.rb +33 -0
  22. data/lib/delivery/responses/delivery_item_listing_response.rb +53 -0
  23. data/lib/delivery/responses/delivery_item_response.rb +39 -0
  24. data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +46 -0
  25. data/lib/delivery/responses/delivery_taxonomy_response.rb +32 -0
  26. data/lib/delivery/responses/delivery_type_listing_response.rb +45 -0
  27. data/lib/delivery/responses/delivery_type_response.rb +31 -0
  28. data/lib/delivery/responses/response_base.rb +36 -0
  29. data/lib/delivery/tests/401.json +6 -0
  30. data/lib/delivery/tests/fake_responder.rb +67 -0
  31. data/lib/delivery/tests/filtering/items_gt.json +566 -0
  32. data/lib/delivery/tests/filtering/multiple.json +283 -0
  33. data/lib/delivery/tests/filtering/pagination_about_us.json +647 -0
  34. data/lib/delivery/tests/generic/items.json +4985 -0
  35. data/lib/delivery/tests/generic/items/about_us.json +228 -0
  36. data/lib/delivery/tests/generic/items/aeropress_filters.json +139 -0
  37. data/lib/delivery/tests/generic/items/coffee_processing_techniques.json +169 -0
  38. data/lib/delivery/tests/generic/items/where_does_coffee_come_from_.json +621 -0
  39. data/lib/delivery/tests/generic/taxonomies.json +127 -0
  40. data/lib/delivery/tests/generic/types.json +781 -0
  41. data/lib/delivery/tests/generic/types/brewer/elements/product_status.json +6 -0
  42. data/lib/delivery/version.rb +7 -0
  43. data/lib/kontent-delivery-sdk-ruby.rb +19 -0
  44. metadata +200 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 513b680aab32ffe519e271fd3b0115031668acc1f1874b7a299d79a5219a11c7
4
+ data.tar.gz: f3f02edf1e288569042ca5a2ce76120fd6a79e993d97f2f4771883ac9e5df32f
5
+ SHA512:
6
+ metadata.gz: 6b28a1b96087048630429042e23d637822996884134d49e0ddf732f946a9f53325769a0ef9a7c8bc1c72eaefe7b27a18218f7ea4f56136789b0f04a492b7a1e1
7
+ data.tar.gz: 6ef0e3356eeb44a8713569386c9e9611601c1ad4d8d1ffc7ec7bae18af72480e6f64d1888e2ccb80df27e1386b359d1c05d5b8af1a12a8753f37c6013004e406
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Eric Dugre
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,511 @@
1
+ [![Build Status](https://api.travis-ci.com/Kentico/kontent-delivery-sdk-ruby.svg?branch=master)](https://travis-ci.com/Kentico/kontent-delivery-sdk-ruby)
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
+ [![Stack Overflow](https://img.shields.io/badge/Stack%20Overflow-ASK%20NOW-FE7A16.svg?logo=stackoverflow&logoColor=white)](https://stackoverflow.com/tags/kentico-kontent)
4
+ [![Version](https://img.shields.io/gem/v/delivery-sdk-ruby.svg?style=flat)](https://rubygems.org/gems/delivery-sdk-ruby)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/b2e3fae28a2b2601d815/maintainability)](https://codeclimate.com/github/Kentico/kontent-delivery-sdk-ruby/maintainability)
6
+ [![Test coverage](https://api.codeclimate.com/v1/badges/b2e3fae28a2b2601d815/test_coverage)](https://codeclimate.com/github/Kentico/delivery-sdk-ruby/test_coverage)
7
+
8
+ # Delivery Ruby SDK
9
+
10
+ ![Banner](https://github.com/Kentico/kontent-delivery-sdk-ruby/blob/master/banner.png)
11
+
12
+ 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
+
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
+ ## Demo Rails application
18
+
19
+ This repository contains a very basic Rails application that you can run locally to see how the SDK can be used. To run the Dancing Goat demo application, clone this repository and open `/dancing_goat/app/controllers/application_controller.rb`. Add your project ID to the file here:
20
+
21
+ ```ruby
22
+ class ApplicationController < ActionController::Base
23
+ PROJECT_ID = '<your-project-id>'.freeze
24
+ ```
25
+
26
+ If you don't have the sample project installed in Kentico Kontent, you can generate a new project [here](https://app.kontent.ai/sample-project-generator). Save the file, then open a terminal in the `/dancing_goat` directory and run the following commands:
27
+
28
+ ```
29
+ bundle install
30
+ rails server
31
+ ```
32
+
33
+ The site should be accessible at localhost:3000. You can also follow a step-by-step guide to creating a basic Rails application on the [Kentico Kontent Blog](https://kontent.ai/blog/creating-a-kentico-cloud-ruby-on-rails-application).
34
+
35
+ ## Installation
36
+
37
+ To use the SDK in your own project, add the gem to your Gemfile:
38
+
39
+ ```ruby
40
+ gem 'delivery-sdk-ruby'
41
+ ```
42
+
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:
44
+
45
+ ```ruby
46
+ require 'kontent-delivery-sdk-ruby'
47
+ ```
48
+
49
+ ## Creating a client
50
+
51
+ You will use `Kentico::Kontent::Delivery::DeliveryClient` to obtain content from Kentico Kontent. Create an instance of the client and pass your project ID:
52
+
53
+ ```ruby
54
+ delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>'
55
+ ```
56
+
57
+ :gem: **Pro tip:** You can alias namespaces to make them shorter, e.g.
58
+
59
+ ```ruby
60
+ KK = Kentico::Kontent::Delivery
61
+ delivery_client = KK::DeliveryClient.new project_id: '<your-project-id>'
62
+ ```
63
+
64
+ ### Previewing unpublished content
65
+
66
+ To enable [preview](https://developer.kenticocloud.com/docs/previewing-content-in-a-separate-environment "preview"), pass the Preview API Key to the constructor:
67
+
68
+ ```ruby
69
+ delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
70
+ preview_key: '<your-preview-key>'
71
+ ```
72
+
73
+ This enables preview, but you can toggle preview at any time by setting the `use_preview` attribute of DeliveryClient which is propogated to all queries created by the client, _or_ per-query by setting it's `use_preview` attribute:
74
+
75
+ ```ruby
76
+ # For all queries created by client
77
+ delivery_client.use_preview = false
78
+
79
+ # Per-query
80
+ query = delivery_client.items
81
+ query.use_preview = false
82
+ query.execute do |response|
83
+ # Do something
84
+ end
85
+ ```
86
+
87
+ ### Making secure requests
88
+
89
+ If you've [secured access](https://developer.kenticocloud.com/docs/securing-public-access "Securing public access") to your project, you need to provide the DeliveryClient with the primary or secondary key:
90
+
91
+ ```ruby
92
+ Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
93
+ secure_key: '<your-secure-key>'
94
+ ```
95
+
96
+ ### Retry policy
97
+
98
+ By default, the SDK uses a retry policy, asking for requested content again in case of an error. The default policy retries the HTTP requests if the following status codes are returned:
99
+
100
+ * 408 - `RequestTimeout`
101
+ * 500 - `InternalServerError`
102
+ * 502 - `BadGateway`
103
+ * 503 - `ServiceUnavailable`
104
+ * 504 - `GatewayTimeout`
105
+
106
+ The default policy retries requests 5 times, totaling 6 overall attempts to retrieve content before returning a `ResponseBase` object containing the error. The consecutive attempts are delayed exponentially: 200 milliseconds, 400 milliseconds, 800 milliseconds, etc.
107
+
108
+ To disable the retry policy, you can use the `with_retry_policy` argument:
109
+
110
+ ```ruby
111
+ Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
112
+ secure_key: '<your-secure-key>',
113
+ with_retry_policy: false
114
+ ```
115
+
116
+ ## Listing items
117
+
118
+
119
+ Use `.item` or `.items` to create a `Kentico::Kontent::Delivery::DeliveryQuery`, then call `.execute` to perform the request.
120
+
121
+ ```ruby
122
+ delivery_client.items.execute do |response|
123
+ response.items.each do |item|
124
+ # Do something
125
+ end
126
+ end
127
+ ```
128
+
129
+ You can also execute the query without a block and just get the response:
130
+
131
+ ```ruby
132
+ response = delivery_client.items.execute
133
+ ```
134
+
135
+ ### Filtering
136
+
137
+ You can use [filtering](https://developer.kenticocloud.com/v1/reference#content-filtering "filtering") to retrieve particular items. The filtering methods are applied directly to a string and the available methods are:
138
+
139
+ |Method|Example|REST equivalent|
140
+ |--|--|--|
141
+ |all|`'elements.product_status'.all %w[bestseller on_sale]`|?elements.product_status[all]=bestseller,on_sale|
142
+ |any|`'elements.processing'.any %w[dry__natural_ semi_dry]`|?elements.processing[any]=dry__natural_,semi_dry|
143
+ |contains|`'elements.related_articles'.contains 'on_roasts'`|?elements.related_articles[contains]=on_roasts|
144
+ |eq|`'system.type'.eq 'grinder'`|?system.type=grinder|
145
+ |gt|`'elements.price'.gt 20`|?elements.price[gt]=20|
146
+ |gt_or_eq|`'elements.price'.gt_or_eq 20`|?elements.price[gte]=20|
147
+ |in|`'system.type'.in %w[coffee brewer]`|?system.type[in]=coffee,brewer|
148
+ |lt|`'elements.price'.lt 20`|?elements.price[lt]=20|
149
+ |lt_or_eq|`'elements.price'.lt_or_eq 20`|?elements.price[lte]=20|
150
+ |range|`'system.last_modified'.range %w[2018-02-01 2018-03-31]`|?system.last_modified[range]=2018-02-01,2018-03-31|
151
+
152
+ You can pass a single filter or multiple filters in the DeliveryClient methods. For example:
153
+
154
+ ```ruby
155
+ # Single filter
156
+ delivery_client.items('elements.price'.gt 20)
157
+
158
+ # Multiple filters
159
+ delivery_client.items [
160
+ ('elements.price'.gt 20),
161
+ ('system.type'.eq 'grinder')
162
+ ]
163
+ ```
164
+
165
+ ### Parameters
166
+
167
+ The `.item` and `.items` methods return a `Kentico::Kontent::Delivery::DeliveryQuery` object which you can further configure before executing. The methods you can call are:
168
+
169
+ |Method|Example|REST equivalent
170
+ |--|--|--|
171
+ |[order_by](https://developer.kenticocloud.com/v1/reference#content-ordering "order_by")|`order_by 'system.last_modified' '[desc]'`|?order=system.last_modified[desc]
172
+ |[skip](https://developer.kenticocloud.com/v1/reference#listing-response-paging "skip")|`skip 5`|?skip=5
173
+ |[limit](https://developer.kenticocloud.com/v1/reference#listing-response-paging "limit")|`limit 5`|?limit=5
174
+ |[elements](https://developer.kenticocloud.com/v1/reference#projection "elements")|`elements %w[price product_name image]`|?elements=price,product_name,image
175
+ |[depth](https://developer.kenticocloud.com/v1/reference#linked-content "depth")|`depth 0`|?depth=0
176
+ |[language](https://developer.kenticocloud.com/docs/understanding-language-fallbacks "language")|`language 'en'`|?language=en
177
+
178
+ For example:
179
+
180
+ ```ruby
181
+ delivery_client.items('system.type'.eq 'coffee')
182
+ .depth(0)
183
+ .limit(5)
184
+ .elements(%W[price product_name])
185
+ .execute do |response|
186
+ # Do something
187
+ end
188
+ ```
189
+
190
+ ### Requesting the latest content
191
+
192
+ 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://developer.kenticocloud.com/docs/webhooks) notifications, you might want to request the latest content from your Kentico Kontent project.
193
+
194
+ You can bypass the cache and get the latest content using `request_latest_content`
195
+
196
+ ```ruby
197
+ delivery_client.items('system.type'.eq 'coffee')
198
+ .request_latest_content
199
+ .execute
200
+ ```
201
+
202
+ ### Custom URLs
203
+
204
+ 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:
205
+
206
+ ```ruby
207
+ delivery_client.items
208
+ .url('https://deliver.kontent.ai/<your-project-id>/items?system.type=grinder')
209
+ .execute do |response|
210
+ # Do something
211
+ end
212
+ ```
213
+
214
+ ### Responses
215
+
216
+ All responses from the `.execute` method will be/extend the `Kentico::Kontent::Delivery::Responses::ResponseBase` class which contains an `http_code` attribute and a friendly message that can be displayed by calling `.to_s`. You can check the code to determine if the request was successful:
217
+
218
+ ```ruby
219
+ delivery_client.items.execute do |response|
220
+ case response.http_code
221
+ when 200
222
+ # Success!
223
+ when 401
224
+ # Did you forget the secure key?
225
+ else
226
+ puts response.to_s
227
+ end
228
+ end
229
+ ```
230
+
231
+ You can also view the raw JSON response of the the query using the `.json` attribute.
232
+
233
+ 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.
234
+
235
+ 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:
236
+
237
+ ```ruby
238
+ price = response.item.elements.price.value
239
+ ```
240
+
241
+ ### Assets
242
+
243
+ 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:
244
+
245
+ ```ruby
246
+ url = response.item.get_assets('teaser_image').first.url
247
+ ```
248
+
249
+ ### Linked items
250
+
251
+ You can get a simple array of code names by accessing the element's value:
252
+
253
+ ```ruby
254
+ links = response.item.elements.facts.value
255
+ ```
256
+
257
+ The `.get_links(element)` method will return an array of ContentItems instead:
258
+
259
+ ```ruby
260
+ response.item.get_links('facts').each do |link|
261
+ title = link.elements.title.value
262
+ end
263
+ ```
264
+
265
+ ### Pagination
266
+
267
+ The `DeliveryItemListingResponse` also contains a `pagination` attribute to access the [paging](https://developer.kenticocloud.com/v1/reference#listing-response-paging "paging") data for the Delivery query. This object contains the following attributes:
268
+
269
+ - **skip**
270
+ - **limit**
271
+ - **count**
272
+ - **next_page**
273
+
274
+ For example, to access the next page URL you can use:
275
+
276
+ ```ruby
277
+ delivery_client.items
278
+ .skip(0)
279
+ .limit(5)
280
+ .execute do |response|
281
+ next_page_url = response.pagination.next_page
282
+ end
283
+ ```
284
+
285
+ You can then request the secure published content in your project. Be sure to not expose the key if the file(s) it appears in are publicly-available.
286
+
287
+ ## Retrieving content types
288
+
289
+ You can use the `.type` and `.types` methods to request your content types from Kentico Kontent:
290
+
291
+ ```ruby
292
+ delivery_client.types.execute do |response|
293
+ # Do something
294
+ end
295
+ delivery_client.type('coffee').execute do |response|
296
+ # Do something
297
+ end
298
+ ```
299
+
300
+ ### Responses
301
+
302
+ 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.
303
+
304
+ 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:
305
+
306
+ ```ruby
307
+ delivery_client.type('coffee').execute do |response|
308
+ field_type = response.type.elements.product_status.type # taxonomy
309
+ end
310
+ ```
311
+ The DeliveryTypeListingResponse also contains pagination data, similar to DeliveryItemListingResponse.
312
+
313
+ ## Taxonomy
314
+
315
+ Use the `.taxonomies` and `.taxonomy(code_name)` endpoints to get information about the taxonomy in your project:
316
+
317
+ ```ruby
318
+ # Get all taxonomies
319
+ delivery_client.taxonomies.execute do |response|
320
+ response.taxonomies.each do |tax|
321
+ puts "#{tax.system.name} (#{tax.terms.length})"
322
+ end
323
+ end
324
+
325
+ # Get terms of specific taxonomy
326
+ delivery_client.taxonomy('personas').execute do |response|
327
+ puts response.taxonomy.terms.length
328
+ end
329
+ ```
330
+
331
+ Each response will return either a single `Kentico::Kontent::Delivery::TaxonomyGroup` or an array of groups. The taxonomy group(s) are accessible at `.taxonomy` and `.taxonomies` for single and multiple queries, respectively.
332
+
333
+ 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:
334
+
335
+ ```ruby
336
+ taxonomy_group.terms[0].codename
337
+ ```
338
+
339
+ 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:
340
+
341
+ - Coffee expert
342
+ - Barista
343
+ - Cafe owner
344
+ - Coffee enthusiast
345
+ - Coffee lover
346
+ - Coffee blogger
347
+
348
+ To get the code name of the first term under the "Coffee expert" term, you could do this:
349
+
350
+ ```ruby
351
+ delivery_client.taxonomy('personas').execute do |response|
352
+ puts response.taxonomy.terms[0].terms[0].codename
353
+ end
354
+ ```
355
+
356
+ ## Retrieving content type elements
357
+
358
+ Kentico Kontent provides an [endpoint](https://developer.kenticocloud.com/v1/reference#view-a-content-type-element) for obtaining details about a specific element of a content type. In the Ruby SDK, you can use the `.element` method:
359
+
360
+ ```ruby
361
+ delivery_client.element('brewer', 'product_status').execute do |response|
362
+ puts response.element.type # taxonomy
363
+ end
364
+ ```
365
+
366
+ 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.
367
+
368
+ 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://developer.kenticocloud.com/v1/reference#custom-elements-api) using this approach and any other methods.
369
+
370
+ ## Resolving links
371
+
372
+ 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:
373
+
374
+ ```ruby
375
+ link_resolver = Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver.new(lambda do |link|
376
+ # Link valid
377
+ return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
378
+ return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
379
+ end, lambda do |id|
380
+ # Link broken
381
+ return "/notfound?id=#{id}"
382
+ end)
383
+ delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
384
+ content_link_url_resolver: link_resolver
385
+ ```
386
+
387
+ 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`:
388
+
389
+ ```ruby
390
+ class MyLinkResolver < Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver
391
+ def resolve_link(link)
392
+ return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
393
+ return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
394
+ end
395
+
396
+ def resolve_404(id)
397
+ "/notfound?id=#{id}"
398
+ end
399
+ end
400
+ ```
401
+
402
+ Then create an object of this class when instantiating the DeliveryClient:
403
+
404
+ ```ruby
405
+ delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
406
+ content_link_url_resolver: MyLinkResolver.new
407
+ ```
408
+
409
+ 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:
410
+
411
+ ```ruby
412
+ delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>'
413
+ # Client doesn't use ContentLinkResolver, but query below will
414
+ delivery_client.items
415
+ .with_link_resolver MyLinkResolver.new
416
+ ```
417
+
418
+ The `ContentLink` object that is passed to your resolver contains the following attributes:
419
+
420
+ - **id**: the system.id of the linked content item
421
+ - **code_name**: the system.codename of the linked content item
422
+ - **type**: the content type of the linked content item
423
+ - **url_slug**: the URL slug of the linked content item, or nil if there is none
424
+
425
+ To resolve links in rich text elements, you must retrieve the text using `get_string`:
426
+
427
+ ```ruby
428
+ item_resolver = Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver.new(lambda do |link|
429
+ return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
430
+ return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
431
+ end)
432
+ delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
433
+ content_link_url_resolver: item_resolver
434
+ delivery_client.item('coffee_processing_techniques').execute do |response|
435
+ text = response.item.get_string 'body_copy'
436
+ end
437
+ ```
438
+
439
+ ## Resolving inline content
440
+
441
+ 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`:
442
+
443
+ ```ruby
444
+ item_resolver = Kentico::Kontent::Delivery::Resolvers::InlineContentItemResolver.new(lambda do |item|
445
+ return "<h1>#{item.elements.zip_code.value}</h1>" if item.system.type.eql? 'cafe'
446
+ return "<div>$#{item.elements.price.value}</div>" if item.system.type.eql? 'brewer'
447
+ end)
448
+ delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>',
449
+ inline_content_item_resolver: item_resolver
450
+ ```
451
+
452
+ 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:
453
+
454
+ ```ruby
455
+ class MyItemResolver < Kentico::Kontent::Delivery::Resolvers::InlineContentItemResolver
456
+ def resolve_item(item)
457
+ return "<h1>#{item.elements.zip_code.value}</h1>" if item.system.type.eql? 'cafe'
458
+ return "<div>$#{item.elements.price.value}</div>" if item.system.type.eql? 'brewer'
459
+ end
460
+ end
461
+ ```
462
+
463
+ You can also set the inline content resolver per-query:
464
+
465
+ ```ruby
466
+ delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: '<your-project-id>'
467
+ # Client doesn't use InlineContentItemResolver, but query below will
468
+ delivery_client.items
469
+ .with_inline_content_item_resolver MyItemResolver.new
470
+ ```
471
+
472
+ ## Image transformation
473
+
474
+ When you've obtained the URL for an asset, you can use our [Image Transformation API](https://developer.kenticocloud.com/v1/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:
475
+
476
+ ```ruby
477
+ url = response.item.get_assets('teaser_image').first.url
478
+ url = Kentico::Kontent::Delivery::Builders::ImageTransformationBuilder.transform(url)
479
+ # methods...
480
+ .url
481
+ ```
482
+
483
+ The available methods are:
484
+
485
+ |Method|Possible values|REST example
486
+ |--|--|--|
487
+ |`.with_width`| positive integer, or float between 0 and 1| ?w=200
488
+ |`.with_height`| positive integer, or float between 0 and 1| ?h=200
489
+ |`.with_pixel_ratio`| float greater than 0 but less than 5| ?dpr=1.5
490
+ |`.with_fit_mode`| constants available at `Kentico::Kontent::Delivery::Builders::ImageTransformationBuilder` <ul><li>FITMODE_CROP</li><li>FITMODE_CLIP</li><li>FITMODE_SCALE</li></ul>| ?fit=crop
491
+ |`.with_rect`| 4 integer values representing pixels or floats representing percentages|rect=100,100,0.7,0.7
492
+ |`.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
493
+ |`.with_background_color`| string containing 3, 4, 6, or 8 characters | ?bg=7A0099EE
494
+ |`.with_output_format`| constants available at `Kentico::Kontent::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
495
+ |`.with_quality`| integer between 1 to 100 | ?quality=50
496
+ |`.with_lossless`| 'true', 'false', 0, or 1| ?lossless=1
497
+ |`.with_auto_format_selection`| 'true', 'false', 0, or 1 | ?auto=format
498
+
499
+ ## Feedback & Contributing
500
+
501
+ Check out the [contributing](https://github.com/Kentico/kontent-delivery-sdk-ruby/blob/master/CONTRIBUTING.md) page to see the best places to file issues, start discussions, and begin contributing.
502
+
503
+ ## License
504
+
505
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
506
+
507
+ ## Code of Conduct
508
+
509
+ 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).
510
+
511
+ ![Analytics](https://kentico-ga-beacon.azurewebsites.net/api/UA-69014260-4/Kentico/kontent-delivery-sdk-ruby?pixel)