delivery-sdk-ruby 0.15.0 → 0.16.0

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