delivery-sdk-ruby 0.14.13 → 0.15.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 +461 -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 +141 -141
  8. data/lib/delivery/builders/url_builder.rb +73 -73
  9. data/lib/delivery/client/delivery_client.rb +85 -85
  10. data/lib/delivery/client/delivery_query.rb +188 -183
  11. data/lib/delivery/models/content_item.rb +96 -96
  12. data/lib/delivery/models/content_type.rb +26 -26
  13. data/lib/delivery/models/pagination.rb +13 -13
  14. data/lib/delivery/models/taxonomy_group.rb +24 -24
  15. data/lib/delivery/query_parameters/filters.rb +87 -87
  16. data/lib/delivery/query_parameters/parameter_base.rb +30 -30
  17. data/lib/delivery/query_parameters/query_string.rb +49 -49
  18. data/lib/delivery/resolvers/content_link_resolver.rb +62 -62
  19. data/lib/delivery/resolvers/inline_content_item_resolver.rb +50 -50
  20. data/lib/delivery/resolvers/linked_item_resolver.rb +27 -27
  21. data/lib/delivery/responses/delivery_element_response.rb +23 -23
  22. data/lib/delivery/responses/delivery_item_listing_response.rb +39 -39
  23. data/lib/delivery/responses/delivery_item_response.rb +29 -29
  24. data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +32 -32
  25. data/lib/delivery/responses/delivery_taxonomy_response.rb +22 -22
  26. data/lib/delivery/responses/delivery_type_listing_response.rb +31 -31
  27. data/lib/delivery/responses/delivery_type_response.rb +21 -21
  28. data/lib/delivery/responses/response_base.rb +20 -20
  29. data/lib/delivery/version.rb +3 -3
  30. metadata +13 -12
@@ -1,85 +1,85 @@
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 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,183 +1,188 @@
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
- # Map each hash value to attr with corresponding key
37
- # from https://stackoverflow.com/a/2681014/5656214
38
- config.each do |k, v|
39
- instance_variable_set("@#{k}", v) unless v.nil?
40
- end
41
- self.query_string = Delivery::QueryParameters::QueryString.new
42
- return if config.fetch(:qp, nil).nil?
43
-
44
- # Query parameters were passed, parse and validate
45
- validate_params config.fetch(:qp)
46
- end
47
-
48
- def execute
49
- provide_url
50
- begin
51
- resp = execute_rest
52
- rescue RestClient::ExceptionWithResponse => err
53
- resp = Delivery::Responses::ResponseBase.new err.http_code, err.response
54
- rescue RestClient::SSLCertificateNotVerified => err
55
- resp = Delivery::Responses::ResponseBase.new 500, err
56
- rescue SocketError => err
57
- resp = Delivery::Responses::ResponseBase.new 500, err.message
58
- else
59
- resp = make_response resp
60
- ensure
61
- yield resp if block_given?
62
- resp
63
- end
64
- end
65
-
66
- def with_link_resolver(resolver)
67
- self.content_link_url_resolver = resolver
68
- self
69
- end
70
-
71
- def with_inline_content_item_resolver(resolver)
72
- self.inline_content_item_resolver = resolver
73
- self
74
- end
75
-
76
- def order_by(value, sort = '[asc]')
77
- query_string.set_param('order', value + sort)
78
- self
79
- end
80
-
81
- def skip(value)
82
- query_string.set_param('skip', value)
83
- self
84
- end
85
-
86
- def language(value)
87
- query_string.set_param('language', value)
88
- end
89
-
90
- def limit(value)
91
- query_string.set_param('limit', value)
92
- self
93
- end
94
-
95
- def elements(value)
96
- query_string.set_param('elements', value)
97
- self
98
- end
99
-
100
- def depth(value)
101
- query_string.set_param('depth', value)
102
- self
103
- end
104
-
105
- private
106
-
107
- def provide_url
108
- @url = Delivery::Builders::UrlBuilder.provide_url self if @url.nil?
109
- Delivery::Builders::UrlBuilder.validate_url @url
110
- end
111
-
112
- def validate_params(query_parameters)
113
- params = if query_parameters.is_a? Array
114
- query_parameters
115
- else
116
- [query_parameters]
117
- end
118
- params.each do |p|
119
- query_string.set_param p
120
- unless p.is_a? Delivery::QueryParameters::Filter
121
- raise ArgumentError, ERROR_PARAMS
122
- end
123
- end
124
- end
125
-
126
- # Returns true if this query should use preview mode. Raises an error if
127
- # preview is enabled, but the key is nil
128
- def should_preview
129
- raise ERROR_PREVIEW if use_preview && preview_key.nil?
130
-
131
- use_preview && !preview_key.nil?
132
- end
133
-
134
- def execute_rest
135
- if should_preview
136
- RestClient.get @url, 'X-KC-SDKID' => provide_sdk_header, Authorization: 'Bearer ' + preview_key
137
- else
138
- if secure_key.nil?
139
- RestClient.get @url, 'X-KC-SDKID' => provide_sdk_header
140
- else
141
- RestClient.get @url, 'X-KC-SDKID' => provide_sdk_header, Authorization: 'Bearer ' + secure_key
142
- end
143
- end
144
- end
145
-
146
- def provide_sdk_header
147
- "rubygems.org;delivery-sdk-ruby;#{Delivery::VERSION}"
148
- end
149
-
150
- def make_response(response)
151
- case query_type
152
- when Delivery::QUERY_TYPE_ITEMS
153
- if code_name.nil?
154
- Delivery::Responses::DeliveryItemListingResponse.new(
155
- JSON.parse(response),
156
- content_link_url_resolver,
157
- inline_content_item_resolver
158
- )
159
- else
160
- Delivery::Responses::DeliveryItemResponse.new(
161
- JSON.parse(response),
162
- content_link_url_resolver,
163
- inline_content_item_resolver
164
- )
165
- end
166
- when Delivery::QUERY_TYPE_TYPES
167
- if code_name.nil?
168
- Delivery::Responses::DeliveryTypeListingResponse.new JSON.parse(response)
169
- else
170
- Delivery::Responses::DeliveryTypeResponse.new JSON.parse(response)
171
- end
172
- when Delivery::QUERY_TYPE_TAXONOMIES
173
- if code_name.nil?
174
- Delivery::Responses::DeliveryTaxonomyListingResponse.new JSON.parse(response)
175
- else
176
- Delivery::Responses::DeliveryTaxonomyResponse.new JSON.parse(response)
177
- end
178
- when Delivery::QUERY_TYPE_ELEMENT
179
- Delivery::Responses::DeliveryElementResponse.new JSON.parse(response)
180
- end
181
- end
182
- end
183
- 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 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