delivery-sdk-ruby 1.0.5 → 2.0.3

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