delivery-sdk-ruby 1.0.5 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
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,103 +1,105 @@
1
1
  require 'rest-client'
2
2
  require 'dotenv/load'
3
3
 
4
- module KenticoCloud
5
- module Delivery
6
- class RequestManager
7
- class << self
8
- MAX_ATTEMPTS = 6
9
- INITIAL_DELAY = 0.2
10
- RETRY_WHEN_CODE = [408, 500, 502, 503, 504].freeze
4
+ module Kentico
5
+ module Kontent
6
+ module Delivery
7
+ class RequestManager
8
+ class << self
9
+ MAX_ATTEMPTS = 6
10
+ INITIAL_DELAY = 0.2
11
+ RETRY_WHEN_CODE = [408, 500, 502, 503, 504].freeze
11
12
 
12
- def start(query, headers)
13
- @query = query
14
- @headers = headers
15
- @times_run = 1
16
- @delay = INITIAL_DELAY
17
- @url = @query.provide_url
18
- continue
19
- end
13
+ def start(query, headers)
14
+ @query = query
15
+ @headers = headers
16
+ @times_run = 1
17
+ @delay = INITIAL_DELAY
18
+ @url = @query.provide_url
19
+ continue
20
+ end
20
21
 
21
- private
22
+ private
22
23
 
23
- def should_retry(potential_response)
24
- return potential_response if @times_run == MAX_ATTEMPTS ||
25
- !RETRY_WHEN_CODE.include?(potential_response.http_code) ||
26
- !@query.with_retry_policy
24
+ def should_retry(potential_response)
25
+ return potential_response if @times_run == MAX_ATTEMPTS ||
26
+ !RETRY_WHEN_CODE.include?(potential_response.http_code) ||
27
+ !@query.with_retry_policy
27
28
 
28
- @times_run += 1
29
- @delay *= 2
30
- sleep(@delay)
31
- continue
32
- end
29
+ @times_run += 1
30
+ @delay *= 2
31
+ sleep(@delay)
32
+ continue
33
+ end
33
34
 
34
- def continue
35
- if ENV['TEST'] == '1'
36
- resp = KenticoCloud::Delivery::Tests::FakeResponder.get_response @query, @url, @headers
37
- return resp if resp.is_a? KenticoCloud::Delivery::Responses::ResponseBase
35
+ def continue
36
+ if ENV['TEST'] == '1'
37
+ resp = Kentico::Kontent::Delivery::Tests::FakeResponder.get_response @query, @url, @headers
38
+ return resp if resp.is_a? Kentico::Kontent::Delivery::Responses::ResponseBase
38
39
 
39
- make_response resp # resp is pure JSON
40
- else
41
- begin
42
- resp = RestClient.get @url, @headers
43
- rescue RestClient::ExceptionWithResponse => err
44
- should_retry KenticoCloud::Delivery::Responses::ResponseBase.new err.http_code, err.response
45
- rescue RestClient::SSLCertificateNotVerified => err
46
- should_retry KenticoCloud::Delivery::Responses::ResponseBase.new 500, err
47
- rescue SocketError => err
48
- should_retry KenticoCloud::Delivery::Responses::ResponseBase.new 500, err.message
40
+ make_response resp # resp is pure JSON
49
41
  else
50
- make_response resp
42
+ begin
43
+ resp = RestClient.get @url, @headers
44
+ rescue RestClient::ExceptionWithResponse => err
45
+ should_retry Kentico::Kontent::Delivery::Responses::ResponseBase.new err.http_code, err.response
46
+ rescue RestClient::SSLCertificateNotVerified => err
47
+ should_retry Kentico::Kontent::Delivery::Responses::ResponseBase.new 500, err
48
+ rescue SocketError => err
49
+ should_retry Kentico::Kontent::Delivery::Responses::ResponseBase.new 500, err.message
50
+ else
51
+ make_response resp
52
+ end
51
53
  end
52
54
  end
53
- end
54
55
 
55
- # Converts a standard REST response based on the type of query.
56
- #
57
- # * *Returns*:
58
- # - An object derived from the KenticoCloud::Delivery::Responses::ResponseBase class
59
- def make_response(response)
60
- case @query.query_type
61
- when KenticoCloud::Delivery::QUERY_TYPE_ITEMS
62
- respond_item response
63
- when KenticoCloud::Delivery::QUERY_TYPE_TYPES
64
- respond_type response
65
- when KenticoCloud::Delivery::QUERY_TYPE_TAXONOMIES
66
- respond_taxonomy response
67
- when KenticoCloud::Delivery::QUERY_TYPE_ELEMENT
68
- KenticoCloud::Delivery::Responses::DeliveryElementResponse.new JSON.parse(response)
56
+ # Converts a standard REST response based on the type of query.
57
+ #
58
+ # * *Returns*:
59
+ # - An object derived from the Kentico::Kontent::Delivery::Responses::ResponseBase class
60
+ def make_response(response)
61
+ case @query.query_type
62
+ when Kentico::Kontent::Delivery::QUERY_TYPE_ITEMS
63
+ respond_item response
64
+ when Kentico::Kontent::Delivery::QUERY_TYPE_TYPES
65
+ respond_type response
66
+ when Kentico::Kontent::Delivery::QUERY_TYPE_TAXONOMIES
67
+ respond_taxonomy response
68
+ when Kentico::Kontent::Delivery::QUERY_TYPE_ELEMENT
69
+ Kentico::Kontent::Delivery::Responses::DeliveryElementResponse.new JSON.parse(response)
70
+ end
69
71
  end
70
- end
71
72
 
72
- def respond_type(response)
73
- if @query.code_name.nil?
74
- KenticoCloud::Delivery::Responses::DeliveryTypeListingResponse.new JSON.parse(response)
75
- else
76
- KenticoCloud::Delivery::Responses::DeliveryTypeResponse.new JSON.parse(response)
73
+ def respond_type(response)
74
+ if @query.code_name.nil?
75
+ Kentico::Kontent::Delivery::Responses::DeliveryTypeListingResponse.new JSON.parse(response)
76
+ else
77
+ Kentico::Kontent::Delivery::Responses::DeliveryTypeResponse.new JSON.parse(response)
78
+ end
77
79
  end
78
- end
79
80
 
80
- def respond_taxonomy(response)
81
- if @query.code_name.nil?
82
- KenticoCloud::Delivery::Responses::DeliveryTaxonomyListingResponse.new JSON.parse(response)
83
- else
84
- KenticoCloud::Delivery::Responses::DeliveryTaxonomyResponse.new JSON.parse(response)
81
+ def respond_taxonomy(response)
82
+ if @query.code_name.nil?
83
+ Kentico::Kontent::Delivery::Responses::DeliveryTaxonomyListingResponse.new JSON.parse(response)
84
+ else
85
+ Kentico::Kontent::Delivery::Responses::DeliveryTaxonomyResponse.new JSON.parse(response)
86
+ end
85
87
  end
86
- end
87
88
 
88
- def respond_item(response)
89
- if @query.code_name.nil?
90
- KenticoCloud::Delivery::Responses::DeliveryItemListingResponse.new(
91
- JSON.parse(response),
92
- @query.content_link_url_resolver,
93
- @query.inline_content_item_resolver
94
- )
95
- else
96
- KenticoCloud::Delivery::Responses::DeliveryItemResponse.new(
97
- JSON.parse(response),
98
- @query.content_link_url_resolver,
99
- @query.inline_content_item_resolver
100
- )
89
+ def respond_item(response)
90
+ if @query.code_name.nil?
91
+ Kentico::Kontent::Delivery::Responses::DeliveryItemListingResponse.new(
92
+ JSON.parse(response),
93
+ @query.content_link_url_resolver,
94
+ @query.inline_content_item_resolver
95
+ )
96
+ else
97
+ Kentico::Kontent::Delivery::Responses::DeliveryItemResponse.new(
98
+ JSON.parse(response),
99
+ @query.content_link_url_resolver,
100
+ @query.inline_content_item_resolver
101
+ )
102
+ end
101
103
  end
102
104
  end
103
105
  end
@@ -1,149 +1,151 @@
1
1
  require 'ostruct'
2
2
  require 'nokogiri'
3
3
 
4
- module KenticoCloud
5
- module Delivery
6
- class ContentItem
7
- attr_accessor :content_link_url_resolver,
8
- :inline_content_item_resolver
9
-
10
- # Parses the 'elements' JSON object as a dynamic OpenStruct object.
11
- #
12
- # * *Returns*:
13
- # - +OpenStruct+ The elements of the content item
14
- def elements
15
- @elements unless @elements.nil?
16
- @elements = JSON.parse(
17
- JSON.generate(@source['elements']),
18
- object_class: OpenStruct
19
- )
20
- end
4
+ module Kentico
5
+ module Kontent
6
+ module Delivery
7
+ class ContentItem
8
+ attr_accessor :content_link_url_resolver,
9
+ :inline_content_item_resolver
10
+
11
+ # Parses the 'elements' JSON object as a dynamic OpenStruct object.
12
+ #
13
+ # * *Returns*:
14
+ # - +OpenStruct+ The elements of the content item
15
+ def elements
16
+ @elements unless @elements.nil?
17
+ @elements = JSON.parse(
18
+ JSON.generate(@source['elements']),
19
+ object_class: OpenStruct
20
+ )
21
+ end
21
22
 
22
- # Parses the 'system' JSON object as a dynamic OpenStruct object.
23
- #
24
- # * *Returns*:
25
- # - +OpenStruct+ The system properties of the content item
26
- def system
27
- @system unless @system.nil?
28
- @system = JSON.parse(
29
- JSON.generate(@source['system']),
30
- object_class: OpenStruct
31
- )
32
- end
23
+ # Parses the 'system' JSON object as a dynamic OpenStruct object.
24
+ #
25
+ # * *Returns*:
26
+ # - +OpenStruct+ The system properties of the content item
27
+ def system
28
+ @system unless @system.nil?
29
+ @system = JSON.parse(
30
+ JSON.generate(@source['system']),
31
+ object_class: OpenStruct
32
+ )
33
+ end
33
34
 
34
- # Constructor.
35
- #
36
- # * *Args*:
37
- # - *source* (+JSON+) The response from a REST request for content items. The item may be on the root or under the 'item' node
38
- # - *content_link_url_resolver* ( KenticoCloud::Delivery::Resolvers::ContentLinkResolver )
39
- # - *inline_content_item_resolver* ( KenticoCloud::Delivery::Resolvers::InlineContentItemResolver )
40
- # - *linked_items_resolver* ( KenticoCloud::Delivery::Resolvers::LinkedItemResolver )
41
- def initialize(source, content_link_url_resolver, inline_content_item_resolver, linked_items_resolver)
42
- @source =
43
- if source['item'].nil?
44
- source
45
- else
46
- source['item']
47
- end
48
- @linked_items_resolver = linked_items_resolver
49
- self.content_link_url_resolver = content_link_url_resolver
50
- self.inline_content_item_resolver = inline_content_item_resolver
51
- end
35
+ # Constructor.
36
+ #
37
+ # * *Args*:
38
+ # - *source* (+JSON+) The response from a REST request for content items. The item may be on the root or under the 'item' node
39
+ # - *content_link_url_resolver* ( Kentico::Kontent::Delivery::Resolvers::ContentLinkResolver )
40
+ # - *inline_content_item_resolver* ( Kentico::Kontent::Delivery::Resolvers::InlineContentItemResolver )
41
+ # - *linked_items_resolver* ( Kentico::Kontent::Delivery::Resolvers::LinkedItemResolver )
42
+ def initialize(source, content_link_url_resolver, inline_content_item_resolver, linked_items_resolver)
43
+ @source =
44
+ if source['item'].nil?
45
+ source
46
+ else
47
+ source['item']
48
+ end
49
+ @linked_items_resolver = linked_items_resolver
50
+ self.content_link_url_resolver = content_link_url_resolver
51
+ self.inline_content_item_resolver = inline_content_item_resolver
52
+ end
52
53
 
53
- # Gets a string representation of the data stored in the element. Using this
54
- # method instead of directly accessing the +elements+ collection causes
55
- # the content to be resolved using the resolvers passed during instantiation.
56
- # See https://github.com/Kentico/delivery-sdk-ruby#resolving-links
57
- #
58
- # * *Args*:
59
- # - *code_name* (+string+) The code name of the desired element
60
- #
61
- # * *Returns*:
62
- # - +string+ The data converted to a string, resolved if the element is a 'rich_text' element
63
- def get_string(code_name)
64
- element = get_element code_name
65
- content = element['value']
66
-
67
- if element['type'] == 'rich_text'
68
- content = content_link_url_resolver.resolve content, element['links'] if should_resolve_links element
69
- inline_items = get_inline_items code_name
70
- content = inline_content_item_resolver.resolve content, inline_items if should_resolve_inline_content element
54
+ # Gets a string representation of the data stored in the element. Using this
55
+ # method instead of directly accessing the +elements+ collection causes
56
+ # the content to be resolved using the resolvers passed during instantiation.
57
+ # See https://github.com/Kentico/kontent-delivery-sdk-ruby#resolving-links
58
+ #
59
+ # * *Args*:
60
+ # - *code_name* (+string+) The code name of the desired element
61
+ #
62
+ # * *Returns*:
63
+ # - +string+ The data converted to a string, resolved if the element is a 'rich_text' element
64
+ def get_string(code_name)
65
+ element = get_element code_name
66
+ content = element['value']
67
+
68
+ if element['type'] == 'rich_text'
69
+ content = content_link_url_resolver.resolve content, element['links'] if should_resolve_links element
70
+ inline_items = get_inline_items code_name
71
+ content = inline_content_item_resolver.resolve content, inline_items if should_resolve_inline_content element
72
+ end
73
+ content.to_s
71
74
  end
72
- content.to_s
73
- end
74
75
 
75
- # Returns an array of assets inserted into the specified element of the
76
- # 'asset' type.
77
- #
78
- # * *Args*:
79
- # - *code_name* (+string+) The code name of the desired element
80
- #
81
- # * *Returns*:
82
- # - +Array+ The element's assets parsed as +OpenStruct+ objects
83
- def get_assets(code_name)
84
- element = get_element code_name
85
- element['value'].map { |n| OpenStruct.new(n) }
86
- end
76
+ # Returns an array of assets inserted into the specified element of the
77
+ # 'asset' type.
78
+ #
79
+ # * *Args*:
80
+ # - *code_name* (+string+) The code name of the desired element
81
+ #
82
+ # * *Returns*:
83
+ # - +Array+ The element's assets parsed as +OpenStruct+ objects
84
+ def get_assets(code_name)
85
+ element = get_element code_name
86
+ element['value'].map { |n| OpenStruct.new(n) }
87
+ end
87
88
 
88
- # Returns an array of ContentItems that are linked in a 'modular_content'
89
- # element.
90
- #
91
- # * *Args*:
92
- # - *code_name* (+string+) The code name of the desired element
93
- #
94
- # * *Returns*:
95
- # - +Array+ The element's linked items parsed as +ContentItem+ objects
96
- def get_links(code_name)
97
- element = get_element code_name
98
- get_linked_items element['value']
99
- end
89
+ # Returns an array of ContentItems that are linked in a 'modular_content'
90
+ # element.
91
+ #
92
+ # * *Args*:
93
+ # - *code_name* (+string+) The code name of the desired element
94
+ #
95
+ # * *Returns*:
96
+ # - +Array+ The element's linked items parsed as +ContentItem+ objects
97
+ def get_links(code_name)
98
+ element = get_element code_name
99
+ get_linked_items element['value']
100
+ end
100
101
 
101
- # Returns an array of ContentItems that are inserted as inline content
102
- # items or componenets of a 'rich_text' element.
103
- #
104
- # * *Args*:
105
- # - *code_name* (+string+) The code name of the desired element
106
- #
107
- # * *Returns*:
108
- # - +Array+ The element's inline content items parsed as +ContentItem+ objects
109
- def get_inline_items(code_name)
110
- element = get_element code_name
111
- get_linked_items element['modular_content']
112
- end
102
+ # Returns an array of ContentItems that are inserted as inline content
103
+ # items or componenets of a 'rich_text' element.
104
+ #
105
+ # * *Args*:
106
+ # - *code_name* (+string+) The code name of the desired element
107
+ #
108
+ # * *Returns*:
109
+ # - +Array+ The element's inline content items parsed as +ContentItem+ objects
110
+ def get_inline_items(code_name)
111
+ element = get_element code_name
112
+ get_linked_items element['modular_content']
113
+ end
113
114
 
114
- private
115
+ private
115
116
 
116
- def should_resolve_links(element)
117
- !element['links'].nil? && !content_link_url_resolver.nil?
118
- end
117
+ def should_resolve_links(element)
118
+ !element['links'].nil? && !content_link_url_resolver.nil?
119
+ end
119
120
 
120
- def should_resolve_inline_content(element)
121
- !element['modular_content'].nil? && !inline_content_item_resolver.nil?
122
- end
121
+ def should_resolve_inline_content(element)
122
+ !element['modular_content'].nil? && !inline_content_item_resolver.nil?
123
+ end
123
124
 
124
- # Gets the JSON object from the 'elements' collection with the specified key
125
- #
126
- # * *Args*:
127
- # - *code_name* (+string+) The code name of the desired element
128
- #
129
- # * *Returns*:
130
- # - +JSON+ The element as a JSON object
131
- #
132
- # * *Raises*:
133
- # - +ArgumentError+ if +code_name+ is +nil+ or not a +string+
134
- def get_element(code_name)
135
- raise ArgumentError, "Argument 'code_name' cannot be null" if code_name.nil?
136
- raise ArgumentError, "Argument 'code_name' is not a string" unless code_name.is_a? String
137
-
138
- @source['elements'][code_name]
139
- end
125
+ # Gets the JSON object from the 'elements' collection with the specified key
126
+ #
127
+ # * *Args*:
128
+ # - *code_name* (+string+, +symbol+) The code name or symbol of the desired element
129
+ #
130
+ # * *Returns*:
131
+ # - +JSON+ The element as a JSON object
132
+ #
133
+ # * *Raises*:
134
+ # - +ArgumentError+ if +code_name+ is +nil+
135
+ def get_element(code_name)
136
+ raise ArgumentError, "Argument 'code_name' cannot be null" if code_name.nil?
137
+
138
+ code_name = code_name.to_s if code_name.is_a? Symbol
139
+ @source['elements'][code_name]
140
+ end
140
141
 
141
- def get_linked_items(codenames)
142
- return [] unless codenames.class == Array
142
+ def get_linked_items(codenames)
143
+ return [] unless codenames.class == Array
143
144
 
144
- codenames.each_with_object([]) do |codename, items|
145
- item = @linked_items_resolver.resolve codename
146
- items << item if item
145
+ codenames.each_with_object([]) do |codename, items|
146
+ item = @linked_items_resolver.resolve codename
147
+ items << item if item
148
+ end
147
149
  end
148
150
  end
149
151
  end