kontent-delivery-sdk-ruby 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +21 -0
  3. data/README.md +511 -0
  4. data/bin/console +14 -0
  5. data/bin/setup +8 -0
  6. data/lib/delivery/builders/image_transformation_builder.rb +271 -0
  7. data/lib/delivery/builders/url_builder.rb +117 -0
  8. data/lib/delivery/client/delivery_client.rb +155 -0
  9. data/lib/delivery/client/delivery_query.rb +249 -0
  10. data/lib/delivery/client/request_manager.rb +108 -0
  11. data/lib/delivery/models/content_item.rb +153 -0
  12. data/lib/delivery/models/content_type.rb +41 -0
  13. data/lib/delivery/models/pagination.rb +21 -0
  14. data/lib/delivery/models/taxonomy_group.rb +39 -0
  15. data/lib/delivery/query_parameters/filters.rb +158 -0
  16. data/lib/delivery/query_parameters/parameter_base.rb +44 -0
  17. data/lib/delivery/query_parameters/query_string.rb +78 -0
  18. data/lib/delivery/resolvers/content_link_resolver.rb +102 -0
  19. data/lib/delivery/resolvers/inline_content_item_resolver.rb +75 -0
  20. data/lib/delivery/resolvers/linked_item_resolver.rb +37 -0
  21. data/lib/delivery/responses/delivery_element_response.rb +33 -0
  22. data/lib/delivery/responses/delivery_item_listing_response.rb +53 -0
  23. data/lib/delivery/responses/delivery_item_response.rb +39 -0
  24. data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +46 -0
  25. data/lib/delivery/responses/delivery_taxonomy_response.rb +32 -0
  26. data/lib/delivery/responses/delivery_type_listing_response.rb +45 -0
  27. data/lib/delivery/responses/delivery_type_response.rb +31 -0
  28. data/lib/delivery/responses/response_base.rb +36 -0
  29. data/lib/delivery/tests/401.json +6 -0
  30. data/lib/delivery/tests/fake_responder.rb +67 -0
  31. data/lib/delivery/tests/filtering/items_gt.json +566 -0
  32. data/lib/delivery/tests/filtering/multiple.json +283 -0
  33. data/lib/delivery/tests/filtering/pagination_about_us.json +647 -0
  34. data/lib/delivery/tests/generic/items.json +4985 -0
  35. data/lib/delivery/tests/generic/items/about_us.json +228 -0
  36. data/lib/delivery/tests/generic/items/aeropress_filters.json +139 -0
  37. data/lib/delivery/tests/generic/items/coffee_processing_techniques.json +169 -0
  38. data/lib/delivery/tests/generic/items/where_does_coffee_come_from_.json +621 -0
  39. data/lib/delivery/tests/generic/taxonomies.json +127 -0
  40. data/lib/delivery/tests/generic/types.json +781 -0
  41. data/lib/delivery/tests/generic/types/brewer/elements/product_status.json +6 -0
  42. data/lib/delivery/version.rb +7 -0
  43. data/lib/kontent-delivery-sdk-ruby.rb +19 -0
  44. metadata +200 -0
@@ -0,0 +1,75 @@
1
+ require 'nokogiri'
2
+
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
14
+
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
36
+ end
37
+
38
+ private
39
+
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
53
+
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
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,37 @@
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
14
+
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
26
+
27
+ private
28
+
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
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ require 'delivery/responses/response_base'
2
+
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
22
+
23
+ def initialize(response)
24
+ @response = response
25
+ super 200,
26
+ "Success, '#{element.codename}' returned",
27
+ JSON.generate(@response)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,53 @@
1
+ require 'delivery/models/content_item'
2
+ require 'delivery/models/pagination'
3
+ require 'delivery/responses/response_base'
4
+
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
20
+
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
39
+ end
40
+
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
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,39 @@
1
+ require 'delivery/models/content_item'
2
+ require 'delivery/responses/response_base'
3
+
4
+ module Kentico
5
+ module Kontent
6
+ module Delivery
7
+ module Responses
8
+ # The response of a successful query for a content item.
9
+ # See https://github.com/Kentico/kontent-delivery-sdk-ruby#listing-items
10
+ class DeliveryItemResponse < ResponseBase
11
+ # A Kentico::Kontent::Delivery::ContentItem object from a
12
+ # Kentico::Kontent::Delivery::DeliveryClient.item call.
13
+ #
14
+ # * *Returns*:
15
+ # - Kentico::Kontent::Delivery::ContentItem
16
+ def item
17
+ @item unless @item.nil?
18
+ linked_items_resolver = Kentico::Kontent::Delivery::Resolvers::LinkedItemResolver.new @response['modular_content'], @content_link_url_resolver, @inline_content_item_resolver
19
+ @item = Kentico::Kontent::Delivery::ContentItem.new(
20
+ @response,
21
+ @content_link_url_resolver,
22
+ @inline_content_item_resolver,
23
+ linked_items_resolver
24
+ )
25
+ end
26
+
27
+ def initialize(response, content_link_url_resolver, inline_content_item_resolver)
28
+ @response = response
29
+ @content_link_url_resolver = content_link_url_resolver
30
+ @inline_content_item_resolver = inline_content_item_resolver
31
+ super 200,
32
+ "Success, '#{item.system.codename}' returned",
33
+ JSON.generate(@response)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,46 @@
1
+ require 'delivery/models/taxonomy_group'
2
+ require 'delivery/models/pagination'
3
+ require 'delivery/responses/response_base'
4
+
5
+ module Kentico
6
+ module Kontent
7
+ module Delivery
8
+ module Responses
9
+ # The response of a successful query for taxonomy groups.
10
+ # See https://github.com/Kentico/kontent-delivery-sdk-ruby#taxonomy
11
+ class DeliveryTaxonomyListingResponse < 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
20
+
21
+ # Parses the 'taxonomies' JSON node of the response from a
22
+ # Kentico::Kontent::Delivery::DeliveryClient.taxonomies call.
23
+ #
24
+ # * *Returns*:
25
+ # - +Array+ The taxonomy groups as Kentico::Kontent::Delivery::TaxonomyGroup objects
26
+ def taxonomies
27
+ @taxonomies unless @taxonomies.nil?
28
+ taxonomies = []
29
+ @response['taxonomies'].each do |n|
30
+ taxonomies << Kentico::Kontent::Delivery::TaxonomyGroup.new(n)
31
+ end
32
+ @taxonomies = taxonomies
33
+ end
34
+
35
+ def initialize(response)
36
+ @response = response
37
+
38
+ super 200,
39
+ "Success, #{taxonomies.length} taxonomies returned",
40
+ JSON.generate(@response)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,32 @@
1
+ require 'delivery/models/taxonomy_group'
2
+ require 'delivery/responses/response_base'
3
+
4
+ module Kentico
5
+ module Kontent
6
+ module Delivery
7
+ module Responses
8
+ # The response of a successful query for a taxonomy group.
9
+ # See https://github.com/Kentico/kontent-delivery-sdk-ruby#taxonomy
10
+ class DeliveryTaxonomyResponse < ResponseBase
11
+ # Parses the response from a
12
+ # Kentico::Kontent::Delivery::DeliveryClient.taxonomy call.
13
+ #
14
+ # * *Returns*:
15
+ # - Kentico::Kontent::Delivery::TaxonomyGroup
16
+ def taxonomy
17
+ @taxonomy unless @taxonomy.nil?
18
+ @taxonomy = Kentico::Kontent::Delivery::TaxonomyGroup.new(@response)
19
+ end
20
+
21
+ def initialize(response)
22
+ @response = response
23
+
24
+ super 200,
25
+ "Success, '#{taxonomy.system.codename}' returned",
26
+ JSON.generate(@response)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,45 @@
1
+ require 'delivery/models/content_type'
2
+ require 'delivery/models/pagination'
3
+ require 'delivery/responses/response_base'
4
+
5
+ module Kentico
6
+ module Kontent
7
+ module Delivery
8
+ module Responses
9
+ # The response of a successful query for content types.
10
+ # See https://github.com/Kentico/kontent-delivery-sdk-ruby#retrieving-content-types
11
+ class DeliveryTypeListingResponse < 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
20
+
21
+ # Parses the 'types' JSON node of the response from a
22
+ # Kentico::Kontent::Delivery::DeliveryClient.types call.
23
+ #
24
+ # * *Returns*:
25
+ # - +Array+ The content types as Kentico::Kontent::Delivery::ContentType objects
26
+ def types
27
+ @types unless @types.nil?
28
+ types = []
29
+ @response['types'].each do |n|
30
+ types << Kentico::Kontent::Delivery::ContentType.new(n)
31
+ end
32
+ @types = types
33
+ end
34
+
35
+ def initialize(response)
36
+ @response = response
37
+ super 200,
38
+ "Success, #{types.length} types returned",
39
+ JSON.generate(@response)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,31 @@
1
+ require 'delivery/models/content_type'
2
+ require 'delivery/responses/response_base'
3
+
4
+ module Kentico
5
+ module Kontent
6
+ module Delivery
7
+ module Responses
8
+ # The response of a successful query for a content type.
9
+ # See https://github.com/Kentico/kontent-delivery-sdk-ruby#retrieving-content-types
10
+ class DeliveryTypeResponse < ResponseBase
11
+ # A Kentico::Kontent::Delivery::ContentType object from a
12
+ # Kentico::Kontent::Delivery::DeliveryClient.type call.
13
+ #
14
+ # * *Returns*:
15
+ # - Kentico::Kontent::Delivery::ContentType
16
+ def type
17
+ @type unless @type.nil?
18
+ @type = Kentico::Kontent::Delivery::ContentType.new(@response)
19
+ end
20
+
21
+ def initialize(response)
22
+ @response = response
23
+ super 200,
24
+ "Success, type '#{type.system.codename}' returned",
25
+ JSON.generate(@response)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ module Kentico
2
+ module Kontent
3
+ module Delivery
4
+ module Responses
5
+ # Base class for all responses from a
6
+ # Kentico::Kontent::Delivery::DeliveryQuery.execute call.
7
+ class ResponseBase
8
+ attr_accessor :http_code,
9
+ :message,
10
+ :json
11
+
12
+ # Constructor.
13
+ #
14
+ # * *Args*:
15
+ # - *http_code* (+integer+) The status code returned by the REST request
16
+ # - *message* (+string+) An informative message about the response, visible when calling +to_s+
17
+ # - *json* (+string+) _optional_ The complete, unmodified JSON response from the server
18
+ def initialize(http_code, message, json = '')
19
+ self.http_code = http_code
20
+ self.message = message
21
+ self.json = json
22
+ end
23
+
24
+ # Provides an informative message about the success of the request
25
+ # by combining the status code and message.
26
+ #
27
+ # * *Returns*:
28
+ # - +string+
29
+ def to_s
30
+ "Response is status code #{http_code} with message:\n#{message}"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,6 @@
1
+ {
2
+ "message": "Missing or invalid access token. Please include the valid access token value in the Authorization header field as an HTTP bearer authorization scheme.",
3
+ "request_id": "|89bb1503b14c254a91bd57c135329822.d31c99c5_",
4
+ "error_code": 3,
5
+ "specific_code": 0
6
+ }