delivery-sdk-ruby 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +13 -5
- data/lib/delivery/resolvers/content_link_resolver.rb +23 -11
- data/lib/delivery/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bcfd75f3bee26c46149ed8faef71642ead7dfc5948f583d87056e0cdf2556f0a
|
4
|
+
data.tar.gz: 0b18049d740dd31e8abf162bcd26b6b9a06ed35b155a446dd891fe33e8969e9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a68250979aa4433a51288ea4e0df5a32d92c4cf42bebc4c97fa4ba140f7ea29f1675fdefb15ee2278af37cb34a1c896a22e393e4916246ca2f90903ed4251c9
|
7
|
+
data.tar.gz: 276eef68b913365d19e49db57eace88f0325caeb534fd0928fe237521bca34b0c3395610bbd58011017162f5703504fb85b1c739c0cc9411c4557a9ee1cecce4
|
data/README.md
CHANGED
@@ -346,18 +346,22 @@ The element will always contain __codename__, __type__, and __name__, but multip
|
|
346
346
|
|
347
347
|
## Resolving links
|
348
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:
|
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, 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:
|
350
350
|
|
351
351
|
```ruby
|
352
352
|
link_resolver = KenticoCloud::Delivery::Resolvers::ContentLinkResolver.new(lambda do |link|
|
353
|
-
|
354
|
-
|
355
|
-
|
353
|
+
# Link valid
|
354
|
+
return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
|
355
|
+
return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
|
356
|
+
end, lambda do |id|
|
357
|
+
# link broken
|
358
|
+
return "/notfound?id=#{id}"
|
359
|
+
end)
|
356
360
|
delivery_client = KenticoCloud::Delivery::DeliveryClient.new project_id: '<your-project-id>',
|
357
361
|
content_link_url_resolver: link_resolver
|
358
362
|
```
|
359
363
|
|
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`:
|
364
|
+
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, as well as the `resolve_404(id)` method for broken links. For example, you can create `MyLinkResolver.rb`:
|
361
365
|
|
362
366
|
```ruby
|
363
367
|
class MyLinkResolver < KenticoCloud::Delivery::Resolvers::ContentLinkResolver
|
@@ -365,6 +369,10 @@ class MyLinkResolver < KenticoCloud::Delivery::Resolvers::ContentLinkResolver
|
|
365
369
|
return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
|
366
370
|
return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
|
367
371
|
end
|
372
|
+
|
373
|
+
def resolve_404(id)
|
374
|
+
"/notfound?id=#{id}"
|
375
|
+
end
|
368
376
|
end
|
369
377
|
```
|
370
378
|
|
@@ -7,8 +7,14 @@ module KenticoCloud
|
|
7
7
|
# to supply the href for content item links.
|
8
8
|
# See https://github.com/Kentico/delivery-sdk-ruby#resolving-links
|
9
9
|
class ContentLinkResolver
|
10
|
-
|
11
|
-
|
10
|
+
# Constructor.
|
11
|
+
#
|
12
|
+
# * *Args*:
|
13
|
+
# - *found_handler* (+lambda+) _optional_ Method to be called when resolving a content link and the content item is present in the response
|
14
|
+
# - *not_found_handler* (+lambda+) _optional_ Method to be called when resolving a content link and the content item isn't present in the response
|
15
|
+
def initialize(found_handler = nil, not_found_handler = nil)
|
16
|
+
@found_handler = found_handler
|
17
|
+
@not_found = not_found_handler
|
12
18
|
end
|
13
19
|
|
14
20
|
# Resolves all links in the content.
|
@@ -42,28 +48,34 @@ module KenticoCloud
|
|
42
48
|
# - +string+ The <a data-item-id=""> tag with an HREF generated by the +provide_url+ method
|
43
49
|
def resolve_tag(tag, links)
|
44
50
|
matches = links.select { |link| link.id == tag['data-item-id'].to_s }
|
45
|
-
url = provide_url matches
|
51
|
+
url = provide_url matches, tag['data-item-id']
|
46
52
|
tag['href'] = url
|
47
53
|
tag
|
48
54
|
end
|
49
55
|
|
50
56
|
# Uses the +resolve_link+ method to generate a URL for a ContentLink
|
51
|
-
# object
|
57
|
+
# object, or +resolve_404+ if the content item was not present in the
|
58
|
+
# response.
|
52
59
|
#
|
53
60
|
# * *Args*:
|
54
61
|
# - *matches* (+Array+) The ContentLink objects with an ID matching a particular <a data-item-id=""> tag
|
62
|
+
# - *id* (+string+) The ID of the <a data-item-id=""> tag being resolved
|
55
63
|
#
|
56
64
|
# * *Returns*:
|
57
|
-
# - +string+ A url
|
58
|
-
def provide_url(matches)
|
59
|
-
if
|
60
|
-
if @
|
61
|
-
|
65
|
+
# - +string+ A url to the item or 404 page
|
66
|
+
def provide_url(matches, id)
|
67
|
+
if matches.empty?
|
68
|
+
if @not_found_handler.nil?
|
69
|
+
resolve_404 id
|
62
70
|
else
|
63
|
-
@
|
71
|
+
@not_found_handler.call id
|
64
72
|
end
|
65
73
|
else
|
66
|
-
|
74
|
+
if @found_handler.nil?
|
75
|
+
resolve_link matches[0]
|
76
|
+
else
|
77
|
+
@found_handler.call matches[0]
|
78
|
+
end
|
67
79
|
end
|
68
80
|
end
|
69
81
|
end
|
data/lib/delivery/version.rb
CHANGED