kontent-delivery-sdk-ruby 2.0.18 → 2.0.19
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 +575 -576
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/lib/delivery/builders/image_transformation_builder.rb +271 -271
- data/lib/delivery/builders/url_builder.rb +120 -120
- data/lib/delivery/client/delivery_client.rb +176 -176
- data/lib/delivery/client/delivery_query.rb +302 -302
- data/lib/delivery/client/request_manager.rb +125 -125
- data/lib/delivery/models/content_item.rb +153 -153
- data/lib/delivery/models/content_type.rb +41 -41
- data/lib/delivery/models/pagination.rb +22 -22
- data/lib/delivery/models/taxonomy_group.rb +39 -39
- data/lib/delivery/query_parameters/filters.rb +158 -158
- data/lib/delivery/query_parameters/parameter_base.rb +46 -46
- data/lib/delivery/query_parameters/query_string.rb +78 -78
- data/lib/delivery/resolvers/content_link_resolver.rb +102 -102
- data/lib/delivery/resolvers/inline_content_item_resolver.rb +75 -75
- data/lib/delivery/resolvers/linked_item_resolver.rb +37 -37
- data/lib/delivery/responses/delivery_element_response.rb +34 -34
- data/lib/delivery/responses/delivery_item_listing_response.rb +54 -54
- data/lib/delivery/responses/delivery_item_response.rb +40 -40
- data/lib/delivery/responses/delivery_items_feed_response.rb +58 -58
- data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +47 -47
- data/lib/delivery/responses/delivery_taxonomy_response.rb +33 -33
- data/lib/delivery/responses/delivery_type_listing_response.rb +46 -46
- data/lib/delivery/responses/delivery_type_response.rb +32 -32
- data/lib/delivery/responses/response_base.rb +39 -39
- data/lib/delivery/tests/401.json +5 -5
- data/lib/delivery/tests/429.json +4 -4
- data/lib/delivery/tests/fake_responder.rb +110 -110
- data/lib/delivery/tests/filtering/items_gt.json +565 -565
- data/lib/delivery/tests/filtering/items_with_count.json +4985 -4985
- data/lib/delivery/tests/filtering/multiple.json +282 -282
- data/lib/delivery/tests/filtering/pagination_about_us.json +646 -646
- data/lib/delivery/tests/generic/items.json +4984 -4984
- data/lib/delivery/tests/generic/items/about_us.json +227 -227
- data/lib/delivery/tests/generic/items/aeropress_filters.json +138 -138
- data/lib/delivery/tests/generic/items/coffee_processing_techniques.json +168 -168
- data/lib/delivery/tests/generic/items/where_does_coffee_come_from_.json +620 -620
- data/lib/delivery/tests/generic/taxonomies.json +126 -126
- data/lib/delivery/tests/generic/types.json +780 -780
- data/lib/delivery/tests/generic/types/brewer/elements/product_status.json +5 -5
- data/lib/delivery/tests/items_feed/articles_feed_1.json +39 -39
- data/lib/delivery/tests/items_feed/articles_feed_2.json +78 -78
- data/lib/delivery/tests/items_feed/articles_feed_3.json +104 -104
- data/lib/delivery/version.rb +7 -7
- data/lib/kontent-delivery-sdk-ruby.rb +20 -20
- metadata +30 -11
@@ -1,46 +1,46 @@
|
|
1
|
-
require 'cgi'
|
2
|
-
|
3
|
-
module Kentico
|
4
|
-
module Kontent
|
5
|
-
module Delivery
|
6
|
-
module QueryParameters
|
7
|
-
# Base class for all parameters added to a DeliveryQuery. All
|
8
|
-
# QueryParameters will appear in the query string.
|
9
|
-
class ParameterBase
|
10
|
-
attr_accessor :key
|
11
|
-
SEPARATOR = CGI::escape(',')
|
12
|
-
|
13
|
-
# Constructor.
|
14
|
-
#
|
15
|
-
# * *Args*:
|
16
|
-
# - *key* (+string+) The field to filter upon
|
17
|
-
# - *operator* (+string+) The Kentico Kontent filter being applied to the field, in brackets
|
18
|
-
# - *values* (+Object+) One or more values which will appear as the value of the query string parameter
|
19
|
-
def initialize(key, operator, values)
|
20
|
-
self.key = key
|
21
|
-
values = [values] unless values.respond_to? :each
|
22
|
-
@values = values
|
23
|
-
@operator = operator
|
24
|
-
end
|
25
|
-
|
26
|
-
# Converts the object into a valid query string parameter for use in
|
27
|
-
# a request to Delivery. The key, operator, and values are all escaped
|
28
|
-
# and if there are multiple values, they are joined with commas.
|
29
|
-
#
|
30
|
-
# * *Returns*:
|
31
|
-
# - +string+ A query string parameter without any additional characters (e.g. '&')
|
32
|
-
def provide_query_string_parameter
|
33
|
-
escaped_values = []
|
34
|
-
@values.each { |n| escaped_values << CGI.escape(n.to_s) }
|
35
|
-
format(
|
36
|
-
'%<k>s%<o>s=%<v>s',
|
37
|
-
k: CGI.escape(key),
|
38
|
-
o: CGI.escape(@operator),
|
39
|
-
v: escaped_values.join(SEPARATOR)
|
40
|
-
)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
1
|
+
require 'cgi'
|
2
|
+
|
3
|
+
module Kentico
|
4
|
+
module Kontent
|
5
|
+
module Delivery
|
6
|
+
module QueryParameters
|
7
|
+
# Base class for all parameters added to a DeliveryQuery. All
|
8
|
+
# QueryParameters will appear in the query string.
|
9
|
+
class ParameterBase
|
10
|
+
attr_accessor :key
|
11
|
+
SEPARATOR = CGI::escape(',')
|
12
|
+
|
13
|
+
# Constructor.
|
14
|
+
#
|
15
|
+
# * *Args*:
|
16
|
+
# - *key* (+string+) The field to filter upon
|
17
|
+
# - *operator* (+string+) The Kentico Kontent filter being applied to the field, in brackets
|
18
|
+
# - *values* (+Object+) One or more values which will appear as the value of the query string parameter
|
19
|
+
def initialize(key, operator, values)
|
20
|
+
self.key = key
|
21
|
+
values = [values] unless values.respond_to? :each
|
22
|
+
@values = values
|
23
|
+
@operator = operator
|
24
|
+
end
|
25
|
+
|
26
|
+
# Converts the object into a valid query string parameter for use in
|
27
|
+
# a request to Delivery. The key, operator, and values are all escaped
|
28
|
+
# and if there are multiple values, they are joined with commas.
|
29
|
+
#
|
30
|
+
# * *Returns*:
|
31
|
+
# - +string+ A query string parameter without any additional characters (e.g. '&')
|
32
|
+
def provide_query_string_parameter
|
33
|
+
escaped_values = []
|
34
|
+
@values.each { |n| escaped_values << CGI.escape(n.to_s) }
|
35
|
+
format(
|
36
|
+
'%<k>s%<o>s=%<v>s',
|
37
|
+
k: CGI.escape(key),
|
38
|
+
o: CGI.escape(@operator),
|
39
|
+
v: escaped_values.join(SEPARATOR)
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -1,78 +1,78 @@
|
|
1
|
-
require 'delivery/query_parameters/parameter_base'
|
2
|
-
|
3
|
-
module Kentico
|
4
|
-
module Kontent
|
5
|
-
module Delivery
|
6
|
-
module QueryParameters
|
7
|
-
# Represents the entire query string for a request to Delivery.
|
8
|
-
class QueryString
|
9
|
-
def initialize
|
10
|
-
@params = []
|
11
|
-
end
|
12
|
-
|
13
|
-
# Adds a parameter to the query string
|
14
|
-
#
|
15
|
-
# * *Args*:
|
16
|
-
# - *param* (+Object+) Either a string representing the key for the parameter, or a complete Kentico::Kontent::Delivery::QueryParameters::ParameterBase object
|
17
|
-
# - *values* (+string+) A string or array of strings representing the values for the parameter
|
18
|
-
# - *operator* (+string+) Kentico Kontent filtering parameter, placed after the key, before the equal sign
|
19
|
-
def set_param(param, values = '', operator = '')
|
20
|
-
parameter_base =
|
21
|
-
if param.is_a? String
|
22
|
-
Kentico::Kontent::Delivery::QueryParameters::ParameterBase.new(
|
23
|
-
param,
|
24
|
-
operator,
|
25
|
-
values
|
26
|
-
)
|
27
|
-
else
|
28
|
-
param
|
29
|
-
end
|
30
|
-
# Ensure we have a ParameterBase object
|
31
|
-
return unless parameter_base.respond_to? 'provide_query_string_parameter'
|
32
|
-
|
33
|
-
remove_param parameter_base.key
|
34
|
-
@params << parameter_base
|
35
|
-
end
|
36
|
-
|
37
|
-
# Removes all parameters from the query string with a matching key.
|
38
|
-
#
|
39
|
-
# * *Args*:
|
40
|
-
# - *key* (+string+) Parameter key
|
41
|
-
def remove_param(key)
|
42
|
-
@params.delete_if { |i| i.key.eql? key }
|
43
|
-
end
|
44
|
-
|
45
|
-
# Returns all parameters from the query string with a matching key.
|
46
|
-
#
|
47
|
-
# * *Args*:
|
48
|
-
# - *key* (+string+) Parameter key
|
49
|
-
#
|
50
|
-
# * *Returns*:
|
51
|
-
# - +Object+ One or more Kentico::Kontent::Delivery::QueryParameters::ParameterBase objects
|
52
|
-
def param(key)
|
53
|
-
@params.select { |p| p.key.eql? key }
|
54
|
-
end
|
55
|
-
|
56
|
-
# Checks whether there are any parameters defined.
|
57
|
-
#
|
58
|
-
# * *Returns*:
|
59
|
-
# - +bool+ True if there are no parameters set.
|
60
|
-
def empty?
|
61
|
-
@params.empty?
|
62
|
-
end
|
63
|
-
|
64
|
-
# Generates a full query string based on the set parameters, with the
|
65
|
-
# required '?' character at the start. Accomplished by calling the
|
66
|
-
# Kentico::Kontent::Delivery::QueryParameters::ParameterBase.provide_query_string_parameter
|
67
|
-
# method for each parameter.
|
68
|
-
#
|
69
|
-
# * *Returns*:
|
70
|
-
# - +string+ A complete query string
|
71
|
-
def to_s
|
72
|
-
'?' + @params.map(&:provide_query_string_parameter).join('&')
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
1
|
+
require 'delivery/query_parameters/parameter_base'
|
2
|
+
|
3
|
+
module Kentico
|
4
|
+
module Kontent
|
5
|
+
module Delivery
|
6
|
+
module QueryParameters
|
7
|
+
# Represents the entire query string for a request to Delivery.
|
8
|
+
class QueryString
|
9
|
+
def initialize
|
10
|
+
@params = []
|
11
|
+
end
|
12
|
+
|
13
|
+
# Adds a parameter to the query string
|
14
|
+
#
|
15
|
+
# * *Args*:
|
16
|
+
# - *param* (+Object+) Either a string representing the key for the parameter, or a complete Kentico::Kontent::Delivery::QueryParameters::ParameterBase object
|
17
|
+
# - *values* (+string+) A string or array of strings representing the values for the parameter
|
18
|
+
# - *operator* (+string+) Kentico Kontent filtering parameter, placed after the key, before the equal sign
|
19
|
+
def set_param(param, values = '', operator = '')
|
20
|
+
parameter_base =
|
21
|
+
if param.is_a? String
|
22
|
+
Kentico::Kontent::Delivery::QueryParameters::ParameterBase.new(
|
23
|
+
param,
|
24
|
+
operator,
|
25
|
+
values
|
26
|
+
)
|
27
|
+
else
|
28
|
+
param
|
29
|
+
end
|
30
|
+
# Ensure we have a ParameterBase object
|
31
|
+
return unless parameter_base.respond_to? 'provide_query_string_parameter'
|
32
|
+
|
33
|
+
remove_param parameter_base.key
|
34
|
+
@params << parameter_base
|
35
|
+
end
|
36
|
+
|
37
|
+
# Removes all parameters from the query string with a matching key.
|
38
|
+
#
|
39
|
+
# * *Args*:
|
40
|
+
# - *key* (+string+) Parameter key
|
41
|
+
def remove_param(key)
|
42
|
+
@params.delete_if { |i| i.key.eql? key }
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns all parameters from the query string with a matching key.
|
46
|
+
#
|
47
|
+
# * *Args*:
|
48
|
+
# - *key* (+string+) Parameter key
|
49
|
+
#
|
50
|
+
# * *Returns*:
|
51
|
+
# - +Object+ One or more Kentico::Kontent::Delivery::QueryParameters::ParameterBase objects
|
52
|
+
def param(key)
|
53
|
+
@params.select { |p| p.key.eql? key }
|
54
|
+
end
|
55
|
+
|
56
|
+
# Checks whether there are any parameters defined.
|
57
|
+
#
|
58
|
+
# * *Returns*:
|
59
|
+
# - +bool+ True if there are no parameters set.
|
60
|
+
def empty?
|
61
|
+
@params.empty?
|
62
|
+
end
|
63
|
+
|
64
|
+
# Generates a full query string based on the set parameters, with the
|
65
|
+
# required '?' character at the start. Accomplished by calling the
|
66
|
+
# Kentico::Kontent::Delivery::QueryParameters::ParameterBase.provide_query_string_parameter
|
67
|
+
# method for each parameter.
|
68
|
+
#
|
69
|
+
# * *Returns*:
|
70
|
+
# - +string+ A complete query string
|
71
|
+
def to_s
|
72
|
+
'?' + @params.map(&:provide_query_string_parameter).join('&')
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -1,102 +1,102 @@
|
|
1
|
-
require 'nokogiri'
|
2
|
-
|
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
|
20
|
-
|
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
|
37
|
-
|
38
|
-
private
|
39
|
-
|
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
|
56
|
-
|
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
|
74
|
-
else
|
75
|
-
if @found_handler.nil?
|
76
|
-
resolve_link matches[0]
|
77
|
-
else
|
78
|
-
@found_handler.call matches[0]
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
# Model for links from the JSON response
|
85
|
-
class ContentLink
|
86
|
-
attr_accessor :code_name, :type, :url_slug, :id
|
87
|
-
|
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
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
1
|
+
require 'nokogiri'
|
2
|
+
|
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
|
20
|
+
|
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
|
37
|
+
|
38
|
+
private
|
39
|
+
|
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
|
56
|
+
|
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
|
74
|
+
else
|
75
|
+
if @found_handler.nil?
|
76
|
+
resolve_link matches[0]
|
77
|
+
else
|
78
|
+
@found_handler.call matches[0]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Model for links from the JSON response
|
85
|
+
class ContentLink
|
86
|
+
attr_accessor :code_name, :type, :url_slug, :id
|
87
|
+
|
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
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|