kontent-delivery-sdk-ruby 2.0.19 → 2.0.20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +21 -21
  3. data/README.md +583 -575
  4. data/bin/console +14 -14
  5. data/bin/setup +8 -8
  6. data/lib/delivery/builders/image_transformation_builder.rb +271 -271
  7. data/lib/delivery/builders/url_builder.rb +120 -120
  8. data/lib/delivery/client/delivery_client.rb +176 -176
  9. data/lib/delivery/client/delivery_query.rb +302 -302
  10. data/lib/delivery/client/request_manager.rb +125 -125
  11. data/lib/delivery/models/content_item.rb +153 -153
  12. data/lib/delivery/models/content_type.rb +41 -41
  13. data/lib/delivery/models/pagination.rb +22 -22
  14. data/lib/delivery/models/taxonomy_group.rb +39 -39
  15. data/lib/delivery/query_parameters/filters.rb +201 -158
  16. data/lib/delivery/query_parameters/parameter_base.rb +56 -46
  17. data/lib/delivery/query_parameters/query_string.rb +78 -78
  18. data/lib/delivery/resolvers/content_link_resolver.rb +102 -102
  19. data/lib/delivery/resolvers/inline_content_item_resolver.rb +75 -75
  20. data/lib/delivery/resolvers/linked_item_resolver.rb +37 -37
  21. data/lib/delivery/responses/delivery_element_response.rb +34 -34
  22. data/lib/delivery/responses/delivery_item_listing_response.rb +54 -54
  23. data/lib/delivery/responses/delivery_item_response.rb +40 -40
  24. data/lib/delivery/responses/delivery_items_feed_response.rb +58 -58
  25. data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +47 -47
  26. data/lib/delivery/responses/delivery_taxonomy_response.rb +33 -33
  27. data/lib/delivery/responses/delivery_type_listing_response.rb +46 -46
  28. data/lib/delivery/responses/delivery_type_response.rb +32 -32
  29. data/lib/delivery/responses/response_base.rb +39 -39
  30. data/lib/delivery/tests/401.json +5 -5
  31. data/lib/delivery/tests/429.json +4 -4
  32. data/lib/delivery/tests/fake_responder.rb +105 -110
  33. data/lib/delivery/tests/filtering/items_with_count.json +5385 -4986
  34. data/lib/delivery/tests/filtering/{pagination_about_us.json → pagination.json} +761 -646
  35. data/lib/delivery/tests/generic/items.json +5383 -4984
  36. data/lib/delivery/tests/generic/items/about_us.json +276 -227
  37. data/lib/delivery/tests/generic/items/aeropress_filters.json +155 -138
  38. data/lib/delivery/tests/generic/items/coffee_processing_techniques.json +565 -168
  39. data/lib/delivery/tests/generic/items/where_does_coffee_come_from_.json +517 -621
  40. data/lib/delivery/tests/generic/taxonomies.json +203 -126
  41. data/lib/delivery/tests/generic/types.json +836 -781
  42. data/lib/delivery/tests/generic/types/brewer/elements/product_status.json +5 -5
  43. data/lib/delivery/tests/items_feed/articles_feed_1.json +39 -39
  44. data/lib/delivery/tests/items_feed/articles_feed_2.json +78 -78
  45. data/lib/delivery/tests/items_feed/articles_feed_3.json +104 -104
  46. data/lib/kontent-delivery-sdk-ruby.rb +20 -20
  47. metadata +20 -24
  48. data/lib/delivery/tests/filtering/items_gt.json +0 -566
  49. data/lib/delivery/tests/filtering/multiple.json +0 -283
  50. data/lib/delivery/version.rb +0 -7
@@ -1,46 +1,56 @@
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
+ # - *eq_sign* (+boolean+) If false, the equals sign is not generated in the parameter
20
+ def initialize(key, operator, values, eq_sign = true)
21
+ self.key = key
22
+ values = [values] unless values.respond_to? :each
23
+ @values = values
24
+ @operator = operator
25
+ @eq_sign = eq_sign
26
+ end
27
+
28
+ # Converts the object into a valid query string parameter for use in
29
+ # a request to Delivery. The key, operator, and values are all escaped
30
+ # and if there are multiple values, they are joined with commas.
31
+ #
32
+ # * *Returns*:
33
+ # - +string+ A query string parameter without any additional characters (e.g. '&')
34
+ def provide_query_string_parameter
35
+ escaped_values = []
36
+ @values.each { |n| escaped_values << CGI.escape(n.to_s) }
37
+ if @eq_sign
38
+ format(
39
+ '%<k>s%<o>s=%<v>s',
40
+ k: CGI.escape(key),
41
+ o: CGI.escape(@operator),
42
+ v: escaped_values.join(SEPARATOR)
43
+ )
44
+ else
45
+ format(
46
+ '%<k>s%<o>s',
47
+ k: CGI.escape(key),
48
+ o: CGI.escape(@operator)
49
+ )
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ 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