delivery-sdk-ruby 0.15.0 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +21 -21
  3. data/README.md +480 -461
  4. data/bin/console +14 -14
  5. data/bin/setup +8 -8
  6. data/lib/delivery-sdk-ruby.rb +17 -17
  7. data/lib/delivery/builders/image_transformation_builder.rb +143 -141
  8. data/lib/delivery/builders/url_builder.rb +75 -73
  9. data/lib/delivery/client/delivery_client.rb +87 -85
  10. data/lib/delivery/client/delivery_query.rb +190 -188
  11. data/lib/delivery/models/content_item.rb +98 -96
  12. data/lib/delivery/models/content_type.rb +28 -26
  13. data/lib/delivery/models/pagination.rb +15 -13
  14. data/lib/delivery/models/taxonomy_group.rb +26 -24
  15. data/lib/delivery/query_parameters/filters.rb +89 -87
  16. data/lib/delivery/query_parameters/parameter_base.rb +32 -30
  17. data/lib/delivery/query_parameters/query_string.rb +51 -49
  18. data/lib/delivery/resolvers/content_link_resolver.rb +64 -62
  19. data/lib/delivery/resolvers/inline_content_item_resolver.rb +52 -50
  20. data/lib/delivery/resolvers/linked_item_resolver.rb +30 -28
  21. data/lib/delivery/responses/delivery_element_response.rb +25 -23
  22. data/lib/delivery/responses/delivery_item_listing_response.rb +41 -39
  23. data/lib/delivery/responses/delivery_item_response.rb +31 -29
  24. data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +34 -32
  25. data/lib/delivery/responses/delivery_taxonomy_response.rb +24 -22
  26. data/lib/delivery/responses/delivery_type_listing_response.rb +33 -31
  27. data/lib/delivery/responses/delivery_type_response.rb +23 -21
  28. data/lib/delivery/responses/response_base.rb +22 -20
  29. data/lib/delivery/version.rb +5 -3
  30. metadata +13 -14
@@ -1,85 +1,87 @@
1
- require 'delivery/client/delivery_query'
2
- require 'delivery/responses/delivery_item_listing_response'
3
- require 'delivery/responses/delivery_item_response'
4
- require 'json'
5
-
6
- module Delivery
7
- QUERY_TYPE_TYPES = 'QUERY_TYPE_TYPES'.freeze
8
- QUERY_TYPE_ITEMS = 'QUERY_TYPE_ITEMS'.freeze
9
- QUERY_TYPE_TAXONOMIES = 'QUERY_TYPE_TAXONOMIES'.freeze
10
- QUERY_TYPE_ELEMENT = 'QUERY_TYPE_ELEMENT'.freeze
11
-
12
- # Executes requests against the Kentico Cloud Delivery API.
13
- class DeliveryClient
14
- attr_accessor :use_preview
15
-
16
- def initialize(config)
17
- @project_id = config.fetch(:project_id)
18
- @preview_key = config.fetch(:preview_key, nil)
19
- @secure_key = config.fetch(:secure_key, nil)
20
- @content_link_url_resolver = config.fetch(:content_link_url_resolver, nil)
21
- @inline_content_item_resolver = config.fetch(:inline_content_item_resolver, nil)
22
- self.use_preview = !@preview_key.nil?
23
- end
24
-
25
- def types
26
- DeliveryQuery.new project_id: @project_id,
27
- secure_key: @secure_key,
28
- query_type: QUERY_TYPE_TYPES
29
- end
30
-
31
- def type(code_name)
32
- DeliveryQuery.new project_id: @project_id,
33
- secure_key: @secure_key,
34
- code_name: code_name,
35
- query_type: QUERY_TYPE_TYPES
36
- end
37
-
38
- def items(query_parameters = [])
39
- q = DeliveryQuery.new project_id: @project_id,
40
- secure_key: @secure_key,
41
- qp: query_parameters,
42
- content_link_url_resolver: @content_link_url_resolver,
43
- inline_content_item_resolver: @inline_content_item_resolver,
44
- query_type: QUERY_TYPE_ITEMS
45
- q.use_preview = use_preview
46
- q.preview_key = @preview_key
47
- q
48
- end
49
-
50
- def item(code_name, query_parameters = [])
51
- q = DeliveryQuery.new project_id: @project_id,
52
- secure_key: @secure_key,
53
- code_name: code_name,
54
- qp: query_parameters,
55
- content_link_url_resolver: @content_link_url_resolver,
56
- inline_content_item_resolver: @inline_content_item_resolver,
57
- query_type: QUERY_TYPE_ITEMS
58
- q.use_preview = use_preview
59
- q.preview_key = @preview_key
60
- q
61
- end
62
-
63
- def taxonomies(query_parameters = [])
64
- DeliveryQuery.new project_id: @project_id,
65
- secure_key: @secure_key,
66
- qp: query_parameters,
67
- query_type: QUERY_TYPE_TAXONOMIES
68
- end
69
-
70
- def taxonomy(code_name)
71
- DeliveryQuery.new project_id: @project_id,
72
- secure_key: @secure_key,
73
- code_name: code_name,
74
- query_type: QUERY_TYPE_TAXONOMIES
75
- end
76
-
77
- def element(content_type, element)
78
- DeliveryQuery.new project_id: @project_id,
79
- secure_key: @secure_key,
80
- code_name: element,
81
- content_type: content_type,
82
- query_type: QUERY_TYPE_ELEMENT
83
- end
84
- end
85
- end
1
+ require 'delivery/client/delivery_query'
2
+ require 'delivery/responses/delivery_item_listing_response'
3
+ require 'delivery/responses/delivery_item_response'
4
+ require 'json'
5
+
6
+ module KenticoCloud
7
+ module Delivery
8
+ QUERY_TYPE_TYPES = 'QUERY_TYPE_TYPES'.freeze
9
+ QUERY_TYPE_ITEMS = 'QUERY_TYPE_ITEMS'.freeze
10
+ QUERY_TYPE_TAXONOMIES = 'QUERY_TYPE_TAXONOMIES'.freeze
11
+ QUERY_TYPE_ELEMENT = 'QUERY_TYPE_ELEMENT'.freeze
12
+
13
+ # Executes requests against the Kentico Cloud Delivery API.
14
+ class DeliveryClient
15
+ attr_accessor :use_preview
16
+
17
+ def initialize(config)
18
+ @project_id = config.fetch(:project_id)
19
+ @preview_key = config.fetch(:preview_key, nil)
20
+ @secure_key = config.fetch(:secure_key, nil)
21
+ @content_link_url_resolver = config.fetch(:content_link_url_resolver, nil)
22
+ @inline_content_item_resolver = config.fetch(:inline_content_item_resolver, nil)
23
+ self.use_preview = !@preview_key.nil?
24
+ end
25
+
26
+ def types
27
+ DeliveryQuery.new project_id: @project_id,
28
+ secure_key: @secure_key,
29
+ query_type: QUERY_TYPE_TYPES
30
+ end
31
+
32
+ def type(code_name)
33
+ DeliveryQuery.new project_id: @project_id,
34
+ secure_key: @secure_key,
35
+ code_name: code_name,
36
+ query_type: QUERY_TYPE_TYPES
37
+ end
38
+
39
+ def items(query_parameters = [])
40
+ q = DeliveryQuery.new project_id: @project_id,
41
+ secure_key: @secure_key,
42
+ qp: query_parameters,
43
+ content_link_url_resolver: @content_link_url_resolver,
44
+ inline_content_item_resolver: @inline_content_item_resolver,
45
+ query_type: QUERY_TYPE_ITEMS
46
+ q.use_preview = use_preview
47
+ q.preview_key = @preview_key
48
+ q
49
+ end
50
+
51
+ def item(code_name, query_parameters = [])
52
+ q = DeliveryQuery.new project_id: @project_id,
53
+ secure_key: @secure_key,
54
+ code_name: code_name,
55
+ qp: query_parameters,
56
+ content_link_url_resolver: @content_link_url_resolver,
57
+ inline_content_item_resolver: @inline_content_item_resolver,
58
+ query_type: QUERY_TYPE_ITEMS
59
+ q.use_preview = use_preview
60
+ q.preview_key = @preview_key
61
+ q
62
+ end
63
+
64
+ def taxonomies(query_parameters = [])
65
+ DeliveryQuery.new project_id: @project_id,
66
+ secure_key: @secure_key,
67
+ qp: query_parameters,
68
+ query_type: QUERY_TYPE_TAXONOMIES
69
+ end
70
+
71
+ def taxonomy(code_name)
72
+ DeliveryQuery.new project_id: @project_id,
73
+ secure_key: @secure_key,
74
+ code_name: code_name,
75
+ query_type: QUERY_TYPE_TAXONOMIES
76
+ end
77
+
78
+ def element(content_type, element)
79
+ DeliveryQuery.new project_id: @project_id,
80
+ secure_key: @secure_key,
81
+ code_name: element,
82
+ content_type: content_type,
83
+ query_type: QUERY_TYPE_ELEMENT
84
+ end
85
+ end
86
+ end
87
+ end
@@ -1,188 +1,190 @@
1
- require 'rest-client'
2
- require 'delivery/builders/url_builder'
3
- require 'delivery/query_parameters/query_string'
4
- require 'delivery/version'
5
-
6
- module Delivery
7
- # Responsible for translating query parameters into the
8
- # corresponding REST request to Kentico Cloud.
9
- class DeliveryQuery
10
- ERROR_PREVIEW = 'Preview is enabled for the query, but the key is null. '\
11
- 'You can set the preview_key attribute of the query, or '\
12
- 'when you initialize the client. See '\
13
- 'https://github.com/Kentico/delivery-sdk-ruby#previewing-unpublished-content'.freeze
14
- ERROR_PARAMS = 'Only filters may be passed in the .item or .items methods'\
15
- '. See https://github.com/Kentico/delivery-sdk-ruby#filtering'.freeze
16
- attr_accessor :use_preview,
17
- :preview_key,
18
- :project_id,
19
- :code_name,
20
- :secure_key,
21
- :content_link_url_resolver,
22
- :inline_content_item_resolver,
23
- :query_type,
24
- :query_string,
25
- :content_type
26
-
27
- # Setter for url, returns self for chaining
28
- # .url represents *manually* configured urls, otherwise final url is
29
- # generated in .execute and this will return nil
30
- def url(url = nil)
31
- @url = url unless url.nil?
32
- self
33
- end
34
-
35
- def initialize(config)
36
- @headers = {}
37
-
38
- # Map each hash value to attr with corresponding key
39
- # from https://stackoverflow.com/a/2681014/5656214
40
- config.each do |k, v|
41
- instance_variable_set("@#{k}", v) unless v.nil?
42
- end
43
- self.query_string = Delivery::QueryParameters::QueryString.new
44
- return if config.fetch(:qp, nil).nil?
45
-
46
- # Query parameters were passed, parse and validate
47
- validate_params config.fetch(:qp)
48
- end
49
-
50
- def execute
51
- provide_url
52
- begin
53
- resp = execute_rest
54
- rescue RestClient::ExceptionWithResponse => err
55
- resp = Delivery::Responses::ResponseBase.new err.http_code, err.response
56
- rescue RestClient::SSLCertificateNotVerified => err
57
- resp = Delivery::Responses::ResponseBase.new 500, err
58
- rescue SocketError => err
59
- resp = Delivery::Responses::ResponseBase.new 500, err.message
60
- else
61
- resp = make_response resp
62
- ensure
63
- yield resp if block_given?
64
- resp
65
- end
66
- end
67
-
68
- def with_link_resolver(resolver)
69
- self.content_link_url_resolver = resolver
70
- self
71
- end
72
-
73
- def with_inline_content_item_resolver(resolver)
74
- self.inline_content_item_resolver = resolver
75
- self
76
- end
77
-
78
- def order_by(value, sort = '[asc]')
79
- query_string.set_param('order', value + sort)
80
- self
81
- end
82
-
83
- def skip(value)
84
- query_string.set_param('skip', value)
85
- self
86
- end
87
-
88
- def language(value)
89
- query_string.set_param('language', value)
90
- end
91
-
92
- def limit(value)
93
- query_string.set_param('limit', value)
94
- self
95
- end
96
-
97
- def elements(value)
98
- query_string.set_param('elements', value)
99
- self
100
- end
101
-
102
- def depth(value)
103
- query_string.set_param('depth', value)
104
- self
105
- end
106
-
107
- def request_latest_content
108
- @headers['X-KC-Wait-For-Loading-New-Content'] = true
109
- self
110
- end
111
-
112
- private
113
-
114
- def provide_url
115
- @url = Delivery::Builders::UrlBuilder.provide_url self if @url.nil?
116
- Delivery::Builders::UrlBuilder.validate_url @url
117
- end
118
-
119
- def validate_params(query_parameters)
120
- params = if query_parameters.is_a? Array
121
- query_parameters
122
- else
123
- [query_parameters]
124
- end
125
- params.each do |p|
126
- query_string.set_param p
127
- unless p.is_a? Delivery::QueryParameters::Filter
128
- raise ArgumentError, ERROR_PARAMS
129
- end
130
- end
131
- end
132
-
133
- # Returns true if this query should use preview mode. Raises an error if
134
- # preview is enabled, but the key is nil
135
- def should_preview
136
- raise ERROR_PREVIEW if use_preview && preview_key.nil?
137
-
138
- use_preview && !preview_key.nil?
139
- end
140
-
141
- def execute_rest
142
- headers = @headers.clone
143
-
144
- headers['X-KC-SDKID'] = provide_sdk_header
145
- headers['Authorization'] = "Bearer #{preview_key}" if should_preview
146
- headers['Authorization'] = "Bearer #{secure_key}" if !should_preview && secure_key
147
-
148
- RestClient.get @url, headers
149
- end
150
-
151
- def provide_sdk_header
152
- "rubygems.org;delivery-sdk-ruby;#{Delivery::VERSION}"
153
- end
154
-
155
- def make_response(response)
156
- case query_type
157
- when Delivery::QUERY_TYPE_ITEMS
158
- if code_name.nil?
159
- Delivery::Responses::DeliveryItemListingResponse.new(
160
- JSON.parse(response),
161
- content_link_url_resolver,
162
- inline_content_item_resolver
163
- )
164
- else
165
- Delivery::Responses::DeliveryItemResponse.new(
166
- JSON.parse(response),
167
- content_link_url_resolver,
168
- inline_content_item_resolver
169
- )
170
- end
171
- when Delivery::QUERY_TYPE_TYPES
172
- if code_name.nil?
173
- Delivery::Responses::DeliveryTypeListingResponse.new JSON.parse(response)
174
- else
175
- Delivery::Responses::DeliveryTypeResponse.new JSON.parse(response)
176
- end
177
- when Delivery::QUERY_TYPE_TAXONOMIES
178
- if code_name.nil?
179
- Delivery::Responses::DeliveryTaxonomyListingResponse.new JSON.parse(response)
180
- else
181
- Delivery::Responses::DeliveryTaxonomyResponse.new JSON.parse(response)
182
- end
183
- when Delivery::QUERY_TYPE_ELEMENT
184
- Delivery::Responses::DeliveryElementResponse.new JSON.parse(response)
185
- end
186
- end
187
- end
188
- end
1
+ require 'rest-client'
2
+ require 'delivery/builders/url_builder'
3
+ require 'delivery/query_parameters/query_string'
4
+ require 'delivery/version'
5
+
6
+ module KenticoCloud
7
+ module Delivery
8
+ # Responsible for translating query parameters into the
9
+ # corresponding REST request to Kentico Cloud.
10
+ class DeliveryQuery
11
+ ERROR_PREVIEW = 'Preview is enabled for the query, but the key is null. '\
12
+ 'You can set the preview_key attribute of the query, or '\
13
+ 'when you initialize the client. See '\
14
+ 'https://github.com/Kentico/delivery-sdk-ruby#previewing-unpublished-content'.freeze
15
+ ERROR_PARAMS = 'Only filters may be passed in the .item or .items methods'\
16
+ '. See https://github.com/Kentico/delivery-sdk-ruby#filtering'.freeze
17
+ attr_accessor :use_preview,
18
+ :preview_key,
19
+ :project_id,
20
+ :code_name,
21
+ :secure_key,
22
+ :content_link_url_resolver,
23
+ :inline_content_item_resolver,
24
+ :query_type,
25
+ :query_string,
26
+ :content_type
27
+
28
+ # Setter for url, returns self for chaining
29
+ # .url represents *manually* configured urls, otherwise final url is
30
+ # generated in .execute and this will return nil
31
+ def url(url = nil)
32
+ @url = url unless url.nil?
33
+ self
34
+ end
35
+
36
+ def initialize(config)
37
+ @headers = {}
38
+
39
+ # Map each hash value to attr with corresponding key
40
+ # from https://stackoverflow.com/a/2681014/5656214
41
+ config.each do |k, v|
42
+ instance_variable_set("@#{k}", v) unless v.nil?
43
+ end
44
+ self.query_string = KenticoCloud::Delivery::QueryParameters::QueryString.new
45
+ return if config.fetch(:qp, nil).nil?
46
+
47
+ # Query parameters were passed, parse and validate
48
+ validate_params config.fetch(:qp)
49
+ end
50
+
51
+ def execute
52
+ provide_url
53
+ begin
54
+ resp = execute_rest
55
+ rescue RestClient::ExceptionWithResponse => err
56
+ resp = KenticoCloud::Delivery::Responses::ResponseBase.new err.http_code, err.response
57
+ rescue RestClient::SSLCertificateNotVerified => err
58
+ resp = KenticoCloud::Delivery::Responses::ResponseBase.new 500, err
59
+ rescue SocketError => err
60
+ resp = KenticoCloud::Delivery::Responses::ResponseBase.new 500, err.message
61
+ else
62
+ resp = make_response resp
63
+ ensure
64
+ yield resp if block_given?
65
+ resp
66
+ end
67
+ end
68
+
69
+ def with_link_resolver(resolver)
70
+ self.content_link_url_resolver = resolver
71
+ self
72
+ end
73
+
74
+ def with_inline_content_item_resolver(resolver)
75
+ self.inline_content_item_resolver = resolver
76
+ self
77
+ end
78
+
79
+ def order_by(value, sort = '[asc]')
80
+ query_string.set_param('order', value + sort)
81
+ self
82
+ end
83
+
84
+ def skip(value)
85
+ query_string.set_param('skip', value)
86
+ self
87
+ end
88
+
89
+ def language(value)
90
+ query_string.set_param('language', value)
91
+ end
92
+
93
+ def limit(value)
94
+ query_string.set_param('limit', value)
95
+ self
96
+ end
97
+
98
+ def elements(value)
99
+ query_string.set_param('elements', value)
100
+ self
101
+ end
102
+
103
+ def depth(value)
104
+ query_string.set_param('depth', value)
105
+ self
106
+ end
107
+
108
+ def request_latest_content
109
+ @headers['X-KC-Wait-For-Loading-New-Content'] = true
110
+ self
111
+ end
112
+
113
+ private
114
+
115
+ def provide_url
116
+ @url = KenticoCloud::Delivery::Builders::UrlBuilder.provide_url self if @url.nil?
117
+ KenticoCloud::Delivery::Builders::UrlBuilder.validate_url @url
118
+ end
119
+
120
+ def validate_params(query_parameters)
121
+ params = if query_parameters.is_a? Array
122
+ query_parameters
123
+ else
124
+ [query_parameters]
125
+ end
126
+ params.each do |p|
127
+ query_string.set_param p
128
+ unless p.is_a? KenticoCloud::Delivery::QueryParameters::Filter
129
+ raise ArgumentError, ERROR_PARAMS
130
+ end
131
+ end
132
+ end
133
+
134
+ # Returns true if this query should use preview mode. Raises an error if
135
+ # preview is enabled, but the key is nil
136
+ def should_preview
137
+ raise ERROR_PREVIEW if use_preview && preview_key.nil?
138
+
139
+ use_preview && !preview_key.nil?
140
+ end
141
+
142
+ def execute_rest
143
+ headers = @headers.clone
144
+
145
+ headers['X-KC-SDKID'] = provide_sdk_header
146
+ headers['Authorization'] = "Bearer #{preview_key}" if should_preview
147
+ headers['Authorization'] = "Bearer #{secure_key}" if !should_preview && secure_key
148
+
149
+ RestClient.get @url, headers
150
+ end
151
+
152
+ def provide_sdk_header
153
+ "rubygems.org;delivery-sdk-ruby;#{KenticoCloud::Delivery::VERSION}"
154
+ end
155
+
156
+ def make_response(response)
157
+ case query_type
158
+ when KenticoCloud::Delivery::QUERY_TYPE_ITEMS
159
+ if code_name.nil?
160
+ KenticoCloud::Delivery::Responses::DeliveryItemListingResponse.new(
161
+ JSON.parse(response),
162
+ content_link_url_resolver,
163
+ inline_content_item_resolver
164
+ )
165
+ else
166
+ KenticoCloud::Delivery::Responses::DeliveryItemResponse.new(
167
+ JSON.parse(response),
168
+ content_link_url_resolver,
169
+ inline_content_item_resolver
170
+ )
171
+ end
172
+ when KenticoCloud::Delivery::QUERY_TYPE_TYPES
173
+ if code_name.nil?
174
+ KenticoCloud::Delivery::Responses::DeliveryTypeListingResponse.new JSON.parse(response)
175
+ else
176
+ KenticoCloud::Delivery::Responses::DeliveryTypeResponse.new JSON.parse(response)
177
+ end
178
+ when KenticoCloud::Delivery::QUERY_TYPE_TAXONOMIES
179
+ if code_name.nil?
180
+ KenticoCloud::Delivery::Responses::DeliveryTaxonomyListingResponse.new JSON.parse(response)
181
+ else
182
+ KenticoCloud::Delivery::Responses::DeliveryTaxonomyResponse.new JSON.parse(response)
183
+ end
184
+ when KenticoCloud::Delivery::QUERY_TYPE_ELEMENT
185
+ KenticoCloud::Delivery::Responses::DeliveryElementResponse.new JSON.parse(response)
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end