delivery-sdk-ruby 0.10.5 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f780f88f42bb6381043099d5e3bcca65f8e900ccbd74a2b7e763cde93d48f0f
4
- data.tar.gz: d003bdca98b31de4a6dc58e001bd2dfbe22300754e11709ff24a30b289de1057
3
+ metadata.gz: fd73aeb9d5c21ced1f411e55d6c5580cbf4a47e4c53e079c20a6042c6e4c8a86
4
+ data.tar.gz: 69e4592d7a990b1bafca69909f27435e7882c25c80b8936541b83d29567fdd5a
5
5
  SHA512:
6
- metadata.gz: 789f858cfe6dc8105dde244cd1a8e4573e3c1940cec4e5b68c1648029954347a5171f6ff852fa7ccc109100c8e9b398a879705db08af2ad1e914f07f1203455c
7
- data.tar.gz: 3b2fb78ac028a29482973075960f55af73cb13c8f9eca5202618fbe029d4f64dff731ae56d02f332fc6d7efb63fcc287c0ef0a175ec2bf24f49c2348610bd974
6
+ metadata.gz: 55367ece8fc6d59d62180aab48359507335b0b87e3d38cea94a3e923dc06ddf14cda1be62dca0bf373e7714fc6723aefd90b5878893717d682d49571ad535a79
7
+ data.tar.gz: 6b90bb56897fcbed3bf7cc61e0d0f74a65e5d0d2c14578fd997192d5d2b1e2ef555eeccd9f56675dc9d493f5f44fcb288e738245ac9910bf5d953356e977f86e
data/README.md CHANGED
@@ -243,8 +243,8 @@ If a rich text element contains links to other content items, you will need to g
243
243
 
244
244
  ```ruby
245
245
  link_resolver = Delivery::Resolvers::ContentLinkResolver.new(lambda do |link|
246
- return "/coffees/#{link.url_slug}" if link.type == 'coffee'
247
- return "/brewers/#{link.url_slug}" if link.type == 'brewer'
246
+ return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
247
+ return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
248
248
  end)
249
249
  delivery_client = Delivery::DeliveryClient.new project_id: '<your-project-id>',
250
250
  content_link_url_resolver: link_resolver
@@ -255,8 +255,8 @@ You can also build the logic for your resolver in a separate class and register
255
255
  ```ruby
256
256
  class MyLinkResolver < Delivery::Resolvers::ContentLinkResolver
257
257
  def resolve_link(link)
258
- return "/coffees/#{link.url_slug}" if link.type == 'coffee'
259
- return "/brewers/#{link.url_slug}" if link.type == 'brewer'
258
+ return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
259
+ return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
260
260
  end
261
261
  end
262
262
  ```
@@ -287,17 +287,50 @@ The `ContentLink` object that is passed to your resolver contains the following
287
287
  To resolve links in rich text elements, you must retrieve the text using `get_string`:
288
288
 
289
289
  ```ruby
290
- lambda_resolver = Delivery::Resolvers::ContentLinkResolver.new(lambda do |link|
291
- return "/coffees/#{link.url_slug}" if link.type == 'coffee'
292
- return "/brewers/#{link.url_slug}" if link.type == 'brewer'
290
+ item_resolver = Delivery::Resolvers::ContentLinkResolver.new(lambda do |link|
291
+ return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
292
+ return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
293
293
  end)
294
294
  delivery_client = Delivery::DeliveryClient.new project_id: '<your-project-id>',
295
- content_link_url_resolver: lambda_resolver
295
+ content_link_url_resolver: item_resolver
296
296
  delivery_client.item('coffee_processing_techniques').execute do |response|
297
297
  text = response.item.get_string 'body_copy'
298
298
  end
299
299
  ```
300
300
 
301
+ ## Resolving inline content
302
+
303
+ 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`:
304
+
305
+ ```ruby
306
+ item_resolver = Delivery::Resolvers::InlineContentItemResolver.new(lambda do |item|
307
+ return "<h1>#{item.elements.zip_code.value}</h1>" if item.system.type.eql? 'cafe'
308
+ return "<div>$#{item.elements.price.value}</div>" if item.system.type.eql? 'brewer'
309
+ end)
310
+ delivery_client = Delivery::DeliveryClient.new project_id: '<your-project-id>',
311
+ inline_content_item_resolver: item_resolver
312
+ ```
313
+
314
+ 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:
315
+
316
+ ```ruby
317
+ class MyItemResolver < Delivery::Resolvers::InlineContentItemResolver
318
+ def resolve_item(item)
319
+ return "<h1>#{item.elements.zip_code.value}</h1>" if item.system.type.eql? 'cafe'
320
+ return "<div>$#{item.elements.price.value}</div>" if item.system.type.eql? 'brewer'
321
+ end
322
+ end
323
+ ```
324
+
325
+ You can also set the inline content resolver per-query:
326
+
327
+ ```ruby
328
+ delivery_client = Delivery::DeliveryClient.new project_id: '<your-project-id>'
329
+ # Client doesn't use InlineContentItemResolver, but query below will
330
+ delivery_client.items
331
+ .with_inline_content_item_resolver MyItemResolver.new
332
+ ```
333
+
301
334
  ## Feedback & Contributing
302
335
 
303
336
  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.
@@ -8,3 +8,4 @@ require File.dirname(__FILE__) + '/delivery/responses/delivery_item_response'
8
8
  require File.dirname(__FILE__) + '/delivery/responses/delivery_type_listing_response'
9
9
  require File.dirname(__FILE__) + '/delivery/responses/delivery_type_response'
10
10
  require File.dirname(__FILE__) + '/delivery/resolvers/content_link_resolver'
11
+ require File.dirname(__FILE__) + '/delivery/resolvers/inline_content_item_resolver'
@@ -16,6 +16,7 @@ module Delivery
16
16
  @preview_key = config.fetch(:preview_key, nil)
17
17
  @secure_key = config.fetch(:secure_key, nil)
18
18
  @content_link_url_resolver = config.fetch(:content_link_url_resolver, nil)
19
+ @inline_content_item_resolver = config.fetch(:inline_content_item_resolver, nil)
19
20
  self.use_preview = !@preview_key.nil?
20
21
  end
21
22
 
@@ -37,6 +38,7 @@ module Delivery
37
38
  secure_key: @secure_key,
38
39
  qp: query_parameters,
39
40
  content_link_url_resolver: @content_link_url_resolver,
41
+ inline_content_item_resolver: @inline_content_item_resolver,
40
42
  query_type: QUERY_TYPE_ITEMS
41
43
  q.use_preview = use_preview
42
44
  q.preview_key = @preview_key
@@ -49,6 +51,7 @@ module Delivery
49
51
  code_name: code_name,
50
52
  qp: query_parameters,
51
53
  content_link_url_resolver: @content_link_url_resolver,
54
+ inline_content_item_resolver: @inline_content_item_resolver,
52
55
  query_type: QUERY_TYPE_ITEMS
53
56
  q.use_preview = use_preview
54
57
  q.preview_key = @preview_key
@@ -18,6 +18,7 @@ module Delivery
18
18
  :params,
19
19
  :secure_key,
20
20
  :content_link_url_resolver,
21
+ :inline_content_item_resolver,
21
22
  :query_type
22
23
 
23
24
  # Setter for url, returns self for chaining
@@ -60,6 +61,11 @@ module Delivery
60
61
  self
61
62
  end
62
63
 
64
+ def with_inline_content_item_resolver(resolver)
65
+ self.inline_content_item_resolver = resolver
66
+ self
67
+ end
68
+
63
69
  def order_by(value, sort = '[asc]')
64
70
  set_param('order', value + sort)
65
71
  self
@@ -141,12 +147,14 @@ module Delivery
141
147
  if code_name.nil?
142
148
  Delivery::Responses::DeliveryItemListingResponse.new(
143
149
  JSON.parse(response),
144
- content_link_url_resolver
150
+ content_link_url_resolver,
151
+ inline_content_item_resolver
145
152
  )
146
153
  else
147
154
  Delivery::Responses::DeliveryItemResponse.new(
148
155
  JSON.parse(response),
149
- content_link_url_resolver
156
+ content_link_url_resolver,
157
+ inline_content_item_resolver
150
158
  )
151
159
  end
152
160
  when Delivery::QUERY_TYPE_TYPES
@@ -4,7 +4,8 @@ require 'nokogiri'
4
4
  module Delivery
5
5
  # JSON data of a content item parsed as OpenStruct objects for dynamic use
6
6
  class ContentItem
7
- attr_accessor :content_link_url_resolver
7
+ attr_accessor :content_link_url_resolver,
8
+ :inline_content_item_resolver
8
9
 
9
10
  def elements
10
11
  @elements unless @elements.nil?
@@ -22,24 +23,28 @@ module Delivery
22
23
  )
23
24
  end
24
25
 
25
- def initialize(source, content_link_url_resolver)
26
+ def initialize(source, content_link_url_resolver, inline_content_item_resolver)
26
27
  @source =
27
28
  if source['item'].nil?
28
29
  source
29
30
  else
30
31
  source['item']
31
32
  end
32
- @link_source = source['modular_content']
33
+ @modular_content = source['modular_content']
33
34
  self.content_link_url_resolver = content_link_url_resolver
35
+ self.inline_content_item_resolver = inline_content_item_resolver
34
36
  end
35
37
 
36
38
  def get_string(code_name)
37
39
  element = get_element code_name
38
-
39
- # Resolve content links if there are any and we have a resolver
40
- return content_link_url_resolver.resolve element['value'], element['links'] if should_resolve element
40
+ content = element['value']
41
41
 
42
- element['value'].to_s
42
+ if element['type'] == 'rich_text'
43
+ content = content_link_url_resolver.resolve content, element['links'] if should_resolve_links element
44
+ inline_items = get_inline_items code_name
45
+ content = inline_content_item_resolver.resolve content, inline_items if should_resolve_inline_content element
46
+ end
47
+ content.to_s
43
48
  end
44
49
 
45
50
  # Returns an array of assets inserted into the specified element
@@ -49,17 +54,29 @@ module Delivery
49
54
  end
50
55
 
51
56
  # Returns an array of ContentItems by comparing code names stored in the
52
- # element with items from request's link_source
57
+ # element with items from request's modular_content
53
58
  def get_links(code_name)
54
59
  element = get_element code_name
55
- filtered = @link_source.values.select { |item| element['value'].include?(item['system']['codename']) }
56
- filtered.map { |n| ContentItem.new JSON.parse(JSON.generate(n)), content_link_url_resolver }
60
+ filtered = @modular_content.values.select { |item| element['value'].include?(item['system']['codename']) }
61
+ filtered.map { |n| ContentItem.new JSON.parse(JSON.generate(n)), content_link_url_resolver, inline_content_item_resolver }
62
+ end
63
+
64
+ # Returns an array of ContentItems by comparing code names stored in the
65
+ # modular_content object with items from request's modular_content
66
+ def get_inline_items(code_name)
67
+ element = get_element code_name
68
+ filtered = @modular_content.values.select { |item| element['modular_content'].include?(item['system']['codename']) }
69
+ filtered.map { |n| ContentItem.new JSON.parse(JSON.generate(n)), content_link_url_resolver, inline_content_item_resolver }
57
70
  end
58
71
 
59
72
  private
60
73
 
61
- def should_resolve(element)
62
- element['type'] == 'rich_text' && !element['links'].nil? && !content_link_url_resolver.nil?
74
+ def should_resolve_links(element)
75
+ !element['links'].nil? && !content_link_url_resolver.nil?
76
+ end
77
+
78
+ def should_resolve_inline_content(element)
79
+ !element['modular_content'].nil? && !inline_content_item_resolver.nil?
63
80
  end
64
81
 
65
82
  def get_element(code_name)
@@ -0,0 +1,51 @@
1
+ require 'nokogiri'
2
+
3
+ module Delivery
4
+ module Resolvers
5
+ # Locates <object data-type="item"> tags in content and calls a user-defined method
6
+ # to supply the HTML output for the content item
7
+ class InlineContentItemResolver
8
+ def initialize(callback = nil)
9
+ @callback = callback
10
+ end
11
+
12
+ # Resolves all inline content items in the content
13
+ # @param [String] content The string value stored in the element
14
+ # @param [Array] inline_items ContentItems referenced by the content
15
+ def resolve(content, inline_items)
16
+ doc = Nokogiri::HTML.parse(content).xpath('//body')
17
+ tags = doc.xpath('//object[@type="application/kenticocloud"][@data-type="item"]')
18
+ tags.each do |tag|
19
+ output = resolve_tag tag, inline_items
20
+ doc.at_xpath('//object[@type="application/kenticocloud"][@data-type="item"][@data-codename=$value]',
21
+ nil,
22
+ :value => tag['data-codename']
23
+ )
24
+ .swap(output)
25
+ end
26
+ doc.inner_html
27
+ end
28
+
29
+ private
30
+
31
+ # Accepts a tag found in the content and tries to locate matching
32
+ # ContentItem from JSON response.
33
+ def resolve_tag(tag, inline_items)
34
+ matches = inline_items.select { |item| item.system.codename == tag['data-codename'].to_s }
35
+ provide_output matches
36
+ end
37
+
38
+ def provide_output(matches)
39
+ if !matches.empty?
40
+ if @callback.nil?
41
+ resolve_item matches[0]
42
+ else
43
+ @callback.call matches[0]
44
+ end
45
+ else
46
+ '/404'
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -17,15 +17,17 @@ module Delivery
17
17
  @response['items'].each do |n|
18
18
  items << Delivery::ContentItem.new(
19
19
  n,
20
- @content_link_url_resolver
20
+ @content_link_url_resolver,
21
+ @inline_content_item_resolver
21
22
  )
22
23
  end
23
24
  @items = items
24
25
  end
25
26
 
26
- def initialize(response, content_link_url_resolver)
27
+ def initialize(response, content_link_url_resolver, inline_content_item_resolver)
27
28
  @response = response
28
29
  @content_link_url_resolver = content_link_url_resolver
30
+ @inline_content_item_resolver = inline_content_item_resolver
29
31
  super 200, "Success, #{items.length} items returned"
30
32
  end
31
33
  end
@@ -7,12 +7,13 @@ module Delivery
7
7
  class DeliveryItemResponse < ResponseBase
8
8
  def item
9
9
  @item unless @item.nil?
10
- @item = Delivery::ContentItem.new(@response, @content_link_url_resolver)
10
+ @item = Delivery::ContentItem.new(@response, @content_link_url_resolver, @inline_content_item_resolver)
11
11
  end
12
12
 
13
- def initialize(response, content_link_url_resolver)
13
+ def initialize(response, content_link_url_resolver, inline_content_item_resolver)
14
14
  @response = response
15
15
  @content_link_url_resolver = content_link_url_resolver
16
+ @inline_content_item_resolver = inline_content_item_resolver
16
17
  super 200, "Success, '#{item.system.code_name}' returned"
17
18
  end
18
19
  end
@@ -1,3 +1,3 @@
1
1
  module Delivery
2
- VERSION = '0.10.5'.freeze
2
+ VERSION = '0.11.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delivery-sdk-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.5
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Dugre
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-07 00:00:00.000000000 Z
11
+ date: 2019-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -113,6 +113,7 @@ files:
113
113
  - lib/delivery/query_parameters/filters.rb
114
114
  - lib/delivery/query_parameters/parameter_base.rb
115
115
  - lib/delivery/resolvers/content_link_resolver.rb
116
+ - lib/delivery/resolvers/inline_content_item_resolver.rb
116
117
  - lib/delivery/responses/delivery_item_listing_response.rb
117
118
  - lib/delivery/responses/delivery_item_response.rb
118
119
  - lib/delivery/responses/delivery_type_listing_response.rb