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.
- checksums.yaml +4 -4
- data/LICENSE.md +21 -21
- data/README.md +480 -461
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/lib/delivery-sdk-ruby.rb +17 -17
- data/lib/delivery/builders/image_transformation_builder.rb +143 -141
- data/lib/delivery/builders/url_builder.rb +75 -73
- data/lib/delivery/client/delivery_client.rb +87 -85
- data/lib/delivery/client/delivery_query.rb +190 -188
- data/lib/delivery/models/content_item.rb +98 -96
- data/lib/delivery/models/content_type.rb +28 -26
- data/lib/delivery/models/pagination.rb +15 -13
- data/lib/delivery/models/taxonomy_group.rb +26 -24
- data/lib/delivery/query_parameters/filters.rb +89 -87
- data/lib/delivery/query_parameters/parameter_base.rb +32 -30
- data/lib/delivery/query_parameters/query_string.rb +51 -49
- data/lib/delivery/resolvers/content_link_resolver.rb +64 -62
- data/lib/delivery/resolvers/inline_content_item_resolver.rb +52 -50
- data/lib/delivery/resolvers/linked_item_resolver.rb +30 -28
- 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 +31 -29
- data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +34 -32
- data/lib/delivery/responses/delivery_taxonomy_response.rb +24 -22
- data/lib/delivery/responses/delivery_type_listing_response.rb +33 -31
- data/lib/delivery/responses/delivery_type_response.rb +23 -21
- data/lib/delivery/responses/response_base.rb +22 -20
- data/lib/delivery/version.rb +5 -3
- metadata +13 -14
@@ -1,30 +1,32 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
1
|
+
module KenticoCloud
|
2
|
+
module Delivery
|
3
|
+
# Contains static methods for adding parameters to a DeliveryQuery
|
4
|
+
# as well as the Filter class.
|
5
|
+
module QueryParameters
|
6
|
+
# Base class for all parameters added to a DeliveryQuery using the
|
7
|
+
# .parameters method. All parameters appear in the query string.
|
8
|
+
class ParameterBase
|
9
|
+
attr_accessor :key
|
10
|
+
SEPARATOR = CGI.escape(',')
|
11
|
+
|
12
|
+
def initialize(key, operator, values)
|
13
|
+
self.key = key
|
14
|
+
values = [values] unless values.respond_to? :each
|
15
|
+
@values = values
|
16
|
+
@operator = operator
|
17
|
+
end
|
18
|
+
|
19
|
+
def provide_query_string_parameter
|
20
|
+
escaped_values = []
|
21
|
+
@values.each { |n| escaped_values << CGI.escape(n.to_s) }
|
22
|
+
format(
|
23
|
+
'%<k>s%<o>s=%<v>s',
|
24
|
+
k: CGI.escape(key),
|
25
|
+
o: CGI.escape(@operator),
|
26
|
+
v: escaped_values.join(SEPARATOR)
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -1,49 +1,51 @@
|
|
1
|
-
require 'delivery/query_parameters/parameter_base'
|
2
|
-
|
3
|
-
module
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
1
|
+
require 'delivery/query_parameters/parameter_base'
|
2
|
+
|
3
|
+
module KenticoCloud
|
4
|
+
module Delivery
|
5
|
+
module QueryParameters
|
6
|
+
class QueryString
|
7
|
+
def initialize
|
8
|
+
@params = []
|
9
|
+
end
|
10
|
+
|
11
|
+
# Adds a parameter to the query string
|
12
|
+
# @param [String] param Either a string representing the key for the parameter, or a complete ParameterBase object
|
13
|
+
# @param [String] values A string or array of strings representing the values for the parameter
|
14
|
+
# @param [String] operator Kentico Cloud filtering parameter, placed after the key, before the equal sign
|
15
|
+
def set_param(param, values = '', operator = '')
|
16
|
+
parameter_base =
|
17
|
+
if param.is_a? String
|
18
|
+
KenticoCloud::Delivery::QueryParameters::ParameterBase.new(
|
19
|
+
param,
|
20
|
+
operator,
|
21
|
+
values
|
22
|
+
)
|
23
|
+
else
|
24
|
+
param
|
25
|
+
end
|
26
|
+
# Ensure we have a ParameterBase object
|
27
|
+
return unless parameter_base.respond_to? 'provide_query_string_parameter'
|
28
|
+
|
29
|
+
remove_param parameter_base.key
|
30
|
+
@params << parameter_base
|
31
|
+
end
|
32
|
+
|
33
|
+
def remove_param(key)
|
34
|
+
@params.delete_if { |i| i.key.eql? key }
|
35
|
+
end
|
36
|
+
|
37
|
+
def param(key)
|
38
|
+
@params.select { |p| p.key.eql? key }
|
39
|
+
end
|
40
|
+
|
41
|
+
def empty?
|
42
|
+
@params.empty?
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_s
|
46
|
+
'?' + @params.map(&:provide_query_string_parameter).join('&')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -1,62 +1,64 @@
|
|
1
|
-
require 'nokogiri'
|
2
|
-
|
3
|
-
module
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
if
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
end
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module KenticoCloud
|
4
|
+
module Delivery
|
5
|
+
module Resolvers
|
6
|
+
# Locates <a data-item-id=""> tags in content and calls a user-defined method
|
7
|
+
# to supply the href for content item links
|
8
|
+
class ContentLinkResolver
|
9
|
+
def initialize(callback = nil)
|
10
|
+
@callback = callback
|
11
|
+
end
|
12
|
+
|
13
|
+
# Resolves all links in the content
|
14
|
+
# @param [String] content The string value stored in the element
|
15
|
+
# @param [Array] links The collection of source links from the JSON response
|
16
|
+
def resolve(content, links)
|
17
|
+
doc = Nokogiri::HTML.parse(content).xpath('//body')
|
18
|
+
links = links.map { |link| ContentLink.new link }
|
19
|
+
tags = doc.xpath('//a[@data-item-id]')
|
20
|
+
# This line performs the link resolving and replaces the tags in doc
|
21
|
+
tags.map { |tag| resolve_tag tag, links }
|
22
|
+
doc.inner_html
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
# Accepts a tag found in the content and tries to locate matching
|
28
|
+
# source link from JSON response. If found, resolves URL and returns
|
29
|
+
# the tag with generated HREF
|
30
|
+
def resolve_tag(tag, links)
|
31
|
+
matches = links.select { |link| link.id == tag['data-item-id'].to_s }
|
32
|
+
url = provide_url matches
|
33
|
+
tag['href'] = url
|
34
|
+
tag
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns a url if a link was found in source links, otherwise returns 404
|
38
|
+
def provide_url(matches)
|
39
|
+
if !matches.empty?
|
40
|
+
if @callback.nil?
|
41
|
+
resolve_link matches[0]
|
42
|
+
else
|
43
|
+
@callback.call matches[0]
|
44
|
+
end
|
45
|
+
else
|
46
|
+
'/404'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Model for links from the JSON response
|
52
|
+
class ContentLink
|
53
|
+
attr_accessor :code_name, :type, :url_slug, :id
|
54
|
+
|
55
|
+
def initialize(link)
|
56
|
+
self.id = link[0]
|
57
|
+
self.code_name = link[1]['codename']
|
58
|
+
self.type = link[1]['type']
|
59
|
+
self.url_slug = link[1]['url_slug']
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -1,50 +1,52 @@
|
|
1
|
-
require 'nokogiri'
|
2
|
-
|
3
|
-
module
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
if
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
end
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module KenticoCloud
|
4
|
+
module Delivery
|
5
|
+
module Resolvers
|
6
|
+
# Locates <object data-type="item"> tags in content and calls a user-defined method
|
7
|
+
# to supply the HTML output for the content item
|
8
|
+
class InlineContentItemResolver
|
9
|
+
def initialize(callback = nil)
|
10
|
+
@callback = callback
|
11
|
+
end
|
12
|
+
|
13
|
+
# Resolves all inline content items in the content
|
14
|
+
# @param [String] content The string value stored in the element
|
15
|
+
# @param [Array] inline_items ContentItems referenced by the content
|
16
|
+
def resolve(content, inline_items)
|
17
|
+
doc = Nokogiri::HTML.parse(content).xpath('//body')
|
18
|
+
tags = doc.xpath('//object[@type="application/kenticocloud"][@data-type="item"]')
|
19
|
+
tags.each do |tag|
|
20
|
+
output = resolve_tag tag, inline_items
|
21
|
+
el = doc.at_xpath(
|
22
|
+
'//object[@type="application/kenticocloud"][@data-type="item"][@data-codename=$value]',
|
23
|
+
nil,
|
24
|
+
value: tag['data-codename']
|
25
|
+
)
|
26
|
+
el.swap(output) unless output.nil?
|
27
|
+
end
|
28
|
+
doc.inner_html
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
# Accepts a tag found in the content and tries to locate matching
|
34
|
+
# ContentItem from JSON response.
|
35
|
+
def resolve_tag(tag, inline_items)
|
36
|
+
matches = inline_items.select { |item| item.system.codename == tag['data-codename'].to_s }
|
37
|
+
provide_output matches
|
38
|
+
end
|
39
|
+
|
40
|
+
def provide_output(matches)
|
41
|
+
if !matches.empty?
|
42
|
+
if @callback.nil?
|
43
|
+
resolve_item matches[0]
|
44
|
+
else
|
45
|
+
@callback.call matches[0]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -1,28 +1,30 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
1
|
+
module KenticoCloud
|
2
|
+
module Delivery
|
3
|
+
# Resolves a content item by its codename
|
4
|
+
# It contains the modular content of item/items response
|
5
|
+
class LinkedItemResolver
|
6
|
+
def initialize(modular_content, content_link_url_resolver, inline_content_item_resolver)
|
7
|
+
@modular_content = modular_content
|
8
|
+
@content_link_url_resolver = content_link_url_resolver
|
9
|
+
@inline_content_item_resolver = inline_content_item_resolver
|
10
|
+
@resolved_items = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
# Resolves a content item
|
14
|
+
# If the link for a codename was resolved before,
|
15
|
+
# it returns the same instance of Content Item
|
16
|
+
# @param [String] codename Codename of the content item
|
17
|
+
# @return [ContentItem]
|
18
|
+
def resolve(codename)
|
19
|
+
@resolved_items[codename] ||= resolve_item(codename)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def resolve_item(codename)
|
25
|
+
item = @modular_content.values.find { |i| i['system']['codename'] == codename }
|
26
|
+
ContentItem.new JSON.parse(JSON.generate(item)), @content_link_url_resolver, @inline_content_item_resolver, self
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|