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.
- checksums.yaml +4 -4
- data/LICENSE.md +21 -21
- data/README.md +480 -461
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/lib/delivery-sdk-ruby.rb +17 -17
- data/lib/delivery/builders/image_transformation_builder.rb +143 -141
- data/lib/delivery/builders/url_builder.rb +75 -73
- data/lib/delivery/client/delivery_client.rb +87 -85
- data/lib/delivery/client/delivery_query.rb +190 -188
- data/lib/delivery/models/content_item.rb +98 -96
- data/lib/delivery/models/content_type.rb +28 -26
- data/lib/delivery/models/pagination.rb +15 -13
- data/lib/delivery/models/taxonomy_group.rb +26 -24
- data/lib/delivery/query_parameters/filters.rb +89 -87
- data/lib/delivery/query_parameters/parameter_base.rb +32 -30
- data/lib/delivery/query_parameters/query_string.rb +51 -49
- data/lib/delivery/resolvers/content_link_resolver.rb +64 -62
- data/lib/delivery/resolvers/inline_content_item_resolver.rb +52 -50
- data/lib/delivery/resolvers/linked_item_resolver.rb +30 -28
- data/lib/delivery/responses/delivery_element_response.rb +25 -23
- data/lib/delivery/responses/delivery_item_listing_response.rb +41 -39
- data/lib/delivery/responses/delivery_item_response.rb +31 -29
- data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +34 -32
- data/lib/delivery/responses/delivery_taxonomy_response.rb +24 -22
- data/lib/delivery/responses/delivery_type_listing_response.rb +33 -31
- data/lib/delivery/responses/delivery_type_response.rb +23 -21
- data/lib/delivery/responses/response_base.rb +22 -20
- data/lib/delivery/version.rb +5 -3
- 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
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
Delivery::
|
185
|
-
|
186
|
-
|
187
|
-
|
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
|