delivery-sdk-ruby 1.0.5 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +49 -48
- data/lib/delivery/builders/image_transformation_builder.rb +239 -237
- data/lib/delivery/builders/url_builder.rb +95 -93
- data/lib/delivery/client/delivery_client.rb +137 -135
- data/lib/delivery/client/delivery_query.rb +222 -220
- data/lib/delivery/client/request_manager.rb +83 -81
- data/lib/delivery/models/content_item.rb +132 -130
- data/lib/delivery/models/content_type.rb +33 -31
- data/lib/delivery/models/pagination.rb +16 -14
- data/lib/delivery/models/taxonomy_group.rb +33 -31
- data/lib/delivery/query_parameters/filters.rb +36 -34
- data/lib/delivery/query_parameters/parameter_base.rb +37 -35
- data/lib/delivery/query_parameters/query_string.rb +65 -63
- data/lib/delivery/resolvers/content_link_resolver.rb +85 -83
- data/lib/delivery/resolvers/inline_content_item_resolver.rb +61 -59
- data/lib/delivery/resolvers/linked_item_resolver.rb +29 -27
- data/lib/delivery/responses/delivery_element_response.rb +25 -23
- data/lib/delivery/responses/delivery_item_listing_response.rb +41 -39
- data/lib/delivery/responses/delivery_item_response.rb +30 -28
- data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +33 -31
- data/lib/delivery/responses/delivery_taxonomy_response.rb +22 -20
- data/lib/delivery/responses/delivery_type_listing_response.rb +33 -31
- data/lib/delivery/responses/delivery_type_response.rb +22 -20
- data/lib/delivery/responses/response_base.rb +29 -27
- data/lib/delivery/tests/fake_responder.rb +50 -48
- data/lib/delivery/tests/filtering/items_gt.json +4 -4
- data/lib/delivery/tests/filtering/multiple.json +2 -2
- data/lib/delivery/tests/filtering/pagination_about_us.json +3 -3
- data/lib/delivery/tests/generic/items.json +25 -25
- data/lib/delivery/tests/generic/items/about_us.json +1 -1
- data/lib/delivery/tests/generic/items/aeropress_filters.json +1 -1
- data/lib/delivery/tests/generic/items/coffee_processing_techniques.json +1 -1
- data/lib/delivery/tests/generic/items/where_does_coffee_come_from_.json +4 -4
- data/lib/delivery/version.rb +5 -3
- data/lib/kontent-delivery-sdk-ruby.rb +19 -0
- metadata +38 -23
@@ -1,98 +1,100 @@
|
|
1
1
|
require 'nokogiri'
|
2
2
|
|
3
|
-
module
|
4
|
-
module
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
3
|
+
module Kentico
|
4
|
+
module Kontent
|
5
|
+
module Delivery
|
6
|
+
module Resolvers
|
7
|
+
# Locates <a data-item-id=""> tags in content and calls a user-defined method
|
8
|
+
# to supply the href for content item links.
|
9
|
+
# See https://github.com/Kentico/kontent-delivery-sdk-ruby#resolving-links
|
10
|
+
class ContentLinkResolver
|
11
|
+
# Constructor.
|
12
|
+
#
|
13
|
+
# * *Args*:
|
14
|
+
# - *found_handler* (+lambda+) _optional_ Method to be called when resolving a content link and the content item is present in the response
|
15
|
+
# - *not_found_handler* (+lambda+) _optional_ Method to be called when resolving a content link and the content item isn't present in the response
|
16
|
+
def initialize(found_handler = nil, not_found_handler = nil)
|
17
|
+
@found_handler = found_handler
|
18
|
+
@not_found = not_found_handler
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
21
|
+
# Resolves all links in the content.
|
22
|
+
#
|
23
|
+
# * *Args*:
|
24
|
+
# - *content* (+string+) The string value stored in the element
|
25
|
+
# - *links* (+Array+) The collection of links from an element's 'links' JSON node
|
26
|
+
#
|
27
|
+
# * *Returns*:
|
28
|
+
# - +string+ The original content passed, with all links resolved
|
29
|
+
def resolve(content, links)
|
30
|
+
doc = Nokogiri::HTML.parse(content).xpath('//body')
|
31
|
+
links = links.map { |link| ContentLink.new link }
|
32
|
+
tags = doc.xpath('//a[@data-item-id]')
|
33
|
+
# This line performs the link resolving and replaces the tags in doc
|
34
|
+
tags.map { |tag| resolve_tag tag, links }
|
35
|
+
doc.to_xhtml
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
+
private
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
40
|
+
# Accepts a tag found in the content and tries to locate matching
|
41
|
+
# source link from JSON response. If found, resolves URL and returns
|
42
|
+
# the tag with generated HREF.
|
43
|
+
#
|
44
|
+
# * *Args*:
|
45
|
+
# - *tag* (+string+) A <a data-item-id=""> tag found in the content
|
46
|
+
# - *links* (+Array+) The collection of links from an element's 'links' JSON node, converted to Kentico::Kontent::Delivery::Resolvers::ContentLink objects
|
47
|
+
#
|
48
|
+
# * *Returns*:
|
49
|
+
# - +string+ The <a data-item-id=""> tag with an HREF generated by the +provide_url+ method
|
50
|
+
def resolve_tag(tag, links)
|
51
|
+
matches = links.select { |link| link.id == tag['data-item-id'].to_s }
|
52
|
+
url = provide_url matches, tag['data-item-id']
|
53
|
+
tag['href'] = url
|
54
|
+
tag
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
else
|
74
|
-
if @found_handler.nil?
|
75
|
-
resolve_link matches[0]
|
57
|
+
# Uses the +resolve_link+ method to generate a URL for a ContentLink
|
58
|
+
# object, or +resolve_404+ if the content item was not present in the
|
59
|
+
# response.
|
60
|
+
#
|
61
|
+
# * *Args*:
|
62
|
+
# - *matches* (+Array+) The ContentLink objects with an ID matching a particular <a data-item-id=""> tag
|
63
|
+
# - *id* (+string+) The ID of the <a data-item-id=""> tag being resolved
|
64
|
+
#
|
65
|
+
# * *Returns*:
|
66
|
+
# - +string+ A url to the item or 404 page
|
67
|
+
def provide_url(matches, id)
|
68
|
+
if matches.empty?
|
69
|
+
if @not_found_handler.nil?
|
70
|
+
resolve_404 id
|
71
|
+
else
|
72
|
+
@not_found_handler.call id
|
73
|
+
end
|
76
74
|
else
|
77
|
-
@found_handler.
|
75
|
+
if @found_handler.nil?
|
76
|
+
resolve_link matches[0]
|
77
|
+
else
|
78
|
+
@found_handler.call matches[0]
|
79
|
+
end
|
78
80
|
end
|
79
81
|
end
|
80
82
|
end
|
81
|
-
end
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
84
|
+
# Model for links from the JSON response
|
85
|
+
class ContentLink
|
86
|
+
attr_accessor :code_name, :type, :url_slug, :id
|
86
87
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
88
|
+
# Constructor.
|
89
|
+
#
|
90
|
+
# * *Args*:
|
91
|
+
# - *link* (+JSON+) One link from an element's 'links' JSON node
|
92
|
+
def initialize(link)
|
93
|
+
self.id = link[0]
|
94
|
+
self.code_name = link[1]['codename']
|
95
|
+
self.type = link[1]['type']
|
96
|
+
self.url_slug = link[1]['url_slug']
|
97
|
+
end
|
96
98
|
end
|
97
99
|
end
|
98
100
|
end
|
@@ -1,69 +1,71 @@
|
|
1
1
|
require 'nokogiri'
|
2
2
|
|
3
|
-
module
|
4
|
-
module
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
3
|
+
module Kentico
|
4
|
+
module Kontent
|
5
|
+
module Delivery
|
6
|
+
module Resolvers
|
7
|
+
# Locates <object data-type="item"> tags in content and calls a user-defined
|
8
|
+
# method to supply the output for the content item.
|
9
|
+
# See https://github.com/Kentico/kontent-delivery-sdk-ruby#resolving-inline-content
|
10
|
+
class InlineContentItemResolver
|
11
|
+
def initialize(callback = nil)
|
12
|
+
@callback = callback
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
15
|
+
# Resolves all inline content items in the content.
|
16
|
+
#
|
17
|
+
# * *Args*:
|
18
|
+
# - *content* (+string+) The string value stored in the element
|
19
|
+
# - *inline_items* (+Array+) ContentItems referenced by the content from the 'modular_content' JSON node
|
20
|
+
#
|
21
|
+
# * *Returns*:
|
22
|
+
# - +string+ The original content passed, with all <object data-type="item"> replaced with custom output
|
23
|
+
def resolve(content, inline_items)
|
24
|
+
doc = Nokogiri::HTML.parse(content).xpath('//body')
|
25
|
+
tags = doc.xpath('//object[@type="application/kenticocloud"][@data-type="item"]')
|
26
|
+
tags.each do |tag|
|
27
|
+
output = resolve_tag tag, inline_items
|
28
|
+
el = doc.at_xpath(
|
29
|
+
'//object[@type="application/kenticocloud"][@data-type="item"][@data-codename=$value]',
|
30
|
+
nil,
|
31
|
+
value: tag['data-codename']
|
32
|
+
)
|
33
|
+
el.swap(output) unless output.nil?
|
34
|
+
end
|
35
|
+
doc.inner_html
|
33
36
|
end
|
34
|
-
doc.inner_html
|
35
|
-
end
|
36
37
|
|
37
|
-
|
38
|
+
private
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
40
|
+
# Accepts a tag found in the content and tries to locate matching
|
41
|
+
# ContentItem from JSON response.
|
42
|
+
#
|
43
|
+
# * *Args*:
|
44
|
+
# - *tag* (+string+) A <object data-type="item"> tag found in the content
|
45
|
+
# - *inline_items* (+Array+) ContentItems referenced by the content from the 'modular_content' JSON node
|
46
|
+
#
|
47
|
+
# * *Returns*:
|
48
|
+
# - +string+ The custom output generated by the +provide_output+ method
|
49
|
+
def resolve_tag(tag, inline_items)
|
50
|
+
matches = inline_items.select { |item| item.system.codename == tag['data-codename'].to_s }
|
51
|
+
provide_output matches
|
52
|
+
end
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
54
|
+
# Generates custom output for a content item using the +resolve_item+
|
55
|
+
# method.
|
56
|
+
#
|
57
|
+
# * *Args*:
|
58
|
+
# - *matches* (+Array+) The ContentItems from the 'modular_content' JSON node which match the code name of a particular <object data-type="item"> tag
|
59
|
+
#
|
60
|
+
# * *Returns*:
|
61
|
+
# - +string+ The custom output generated by the +resolve_item+ method
|
62
|
+
def provide_output(matches)
|
63
|
+
if !matches.empty?
|
64
|
+
if @callback.nil?
|
65
|
+
resolve_item matches[0]
|
66
|
+
else
|
67
|
+
@callback.call matches[0]
|
68
|
+
end
|
67
69
|
end
|
68
70
|
end
|
69
71
|
end
|
@@ -1,33 +1,35 @@
|
|
1
|
-
module
|
2
|
-
module
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
module Kentico
|
2
|
+
module Kontent
|
3
|
+
module Delivery
|
4
|
+
module Resolvers
|
5
|
+
# Resolves a content item by its codename. It contains the modular content
|
6
|
+
# of item/items response.
|
7
|
+
class LinkedItemResolver
|
8
|
+
def initialize(modular_content, content_link_url_resolver, inline_content_item_resolver)
|
9
|
+
@modular_content = modular_content
|
10
|
+
@content_link_url_resolver = content_link_url_resolver
|
11
|
+
@inline_content_item_resolver = inline_content_item_resolver
|
12
|
+
@resolved_items = {}
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
15
|
+
# Resolves a content item. If the link for a codename was resolved
|
16
|
+
# before, it returns the same instance of ContentItem.
|
17
|
+
#
|
18
|
+
# * *Args*:
|
19
|
+
# - *codename* (+string+) Codename of the content item
|
20
|
+
#
|
21
|
+
# * *Return*:
|
22
|
+
# - Kentico::Kontent::Delivery::ContentItem
|
23
|
+
def resolve(codename)
|
24
|
+
@resolved_items[codename] ||= resolve_item(codename)
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
+
private
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
def resolve_item(codename)
|
30
|
+
item = @modular_content.values.find { |i| i['system']['codename'] == codename }
|
31
|
+
ContentItem.new JSON.parse(JSON.generate(item)), @content_link_url_resolver, @inline_content_item_resolver, self
|
32
|
+
end
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
@@ -1,29 +1,31 @@
|
|
1
1
|
require 'delivery/responses/response_base'
|
2
2
|
|
3
|
-
module
|
4
|
-
module
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
JSON.
|
18
|
-
|
19
|
-
|
20
|
-
|
3
|
+
module Kentico
|
4
|
+
module Kontent
|
5
|
+
module Delivery
|
6
|
+
module Responses
|
7
|
+
# The response of a successful query of a content type's element
|
8
|
+
# See https://github.com/Kentico/kontent-delivery-sdk-ruby#retrieving-content-type-elements
|
9
|
+
class DeliveryElementResponse < ResponseBase
|
10
|
+
# An element's definition from a
|
11
|
+
# Kentico::Kontent::Delivery::DeliveryClient.element call
|
12
|
+
#
|
13
|
+
# * *Returns*:
|
14
|
+
# - +OpenStruct+ The element of a content item
|
15
|
+
def element
|
16
|
+
@element unless @element.nil?
|
17
|
+
@element = JSON.parse(
|
18
|
+
JSON.generate(@response),
|
19
|
+
object_class: OpenStruct
|
20
|
+
)
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
def initialize(response)
|
24
|
+
@response = response
|
25
|
+
super 200,
|
26
|
+
"Success, '#{element.codename}' returned",
|
27
|
+
JSON.generate(@response)
|
28
|
+
end
|
27
29
|
end
|
28
30
|
end
|
29
31
|
end
|
@@ -2,48 +2,50 @@ require 'delivery/models/content_item'
|
|
2
2
|
require 'delivery/models/pagination'
|
3
3
|
require 'delivery/responses/response_base'
|
4
4
|
|
5
|
-
module
|
6
|
-
module
|
7
|
-
module
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
5
|
+
module Kentico
|
6
|
+
module Kontent
|
7
|
+
module Delivery
|
8
|
+
module Responses
|
9
|
+
# The response of a successful query for content items.
|
10
|
+
# See https://github.com/Kentico/kontent-delivery-sdk-ruby#listing-items
|
11
|
+
class DeliveryItemListingResponse < ResponseBase
|
12
|
+
# Parses the 'pagination' JSON node of the response.
|
13
|
+
#
|
14
|
+
# * *Returns*:
|
15
|
+
# - Kentico::Kontent::Delivery::Pagination
|
16
|
+
def pagination
|
17
|
+
@pagination unless @pagination.nil?
|
18
|
+
@pagination = Pagination.new @response['pagination']
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
21
|
+
# A collection of Kentico::Kontent::Delivery::ContentItem objects from
|
22
|
+
# a Kentico::Kontent::Delivery::DeliveryClient.items call.
|
23
|
+
#
|
24
|
+
# * *Returns*:
|
25
|
+
# - +Array+ One or more Kentico::Kontent::Delivery::ContentItem objects
|
26
|
+
def items
|
27
|
+
@items unless @items.nil?
|
28
|
+
linked_items_resolver = Kentico::Kontent::Delivery::Resolvers::LinkedItemResolver.new @response['modular_content'], @content_link_url_resolver, @inline_content_item_resolver
|
29
|
+
items = []
|
30
|
+
@response['items'].each do |n|
|
31
|
+
items << Kentico::Kontent::Delivery::ContentItem.new(
|
32
|
+
n,
|
33
|
+
@content_link_url_resolver,
|
34
|
+
@inline_content_item_resolver,
|
35
|
+
linked_items_resolver
|
36
|
+
)
|
37
|
+
end
|
38
|
+
@items = items
|
36
39
|
end
|
37
|
-
@items = items
|
38
|
-
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
41
|
+
def initialize(response, content_link_url_resolver, inline_content_item_resolver)
|
42
|
+
@response = response
|
43
|
+
@content_link_url_resolver = content_link_url_resolver
|
44
|
+
@inline_content_item_resolver = inline_content_item_resolver
|
45
|
+
super 200,
|
46
|
+
"Success, #{items.length} items returned",
|
47
|
+
JSON.generate(@response)
|
48
|
+
end
|
47
49
|
end
|
48
50
|
end
|
49
51
|
end
|