zendesk_api 3.1.1 → 4.0.0.pre.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/zendesk_api/actions.rb +31 -31
- data/lib/zendesk_api/association.rb +8 -8
- data/lib/zendesk_api/associations.rb +30 -30
- data/lib/zendesk_api/client.rb +69 -36
- data/lib/zendesk_api/collection.rb +41 -40
- data/lib/zendesk_api/configuration.rb +11 -8
- data/lib/zendesk_api/core_ext/inflection.rb +2 -2
- data/lib/zendesk_api/delegator.rb +1 -1
- data/lib/zendesk_api/helpers.rb +10 -10
- data/lib/zendesk_api/middleware/request/api_token_impersonate.rb +29 -0
- data/lib/zendesk_api/middleware/request/encode_json.rb +3 -4
- data/lib/zendesk_api/middleware/request/etag_cache.rb +27 -4
- data/lib/zendesk_api/middleware/request/raise_rate_limited.rb +3 -3
- data/lib/zendesk_api/middleware/request/retry.rb +56 -20
- data/lib/zendesk_api/middleware/request/upload.rb +1 -1
- data/lib/zendesk_api/middleware/request/url_based_access_token.rb +2 -2
- data/lib/zendesk_api/middleware/response/deflate.rb +1 -1
- data/lib/zendesk_api/middleware/response/gzip.rb +3 -3
- data/lib/zendesk_api/middleware/response/logger.rb +3 -3
- data/lib/zendesk_api/middleware/response/parse_iso_dates.rb +1 -1
- data/lib/zendesk_api/middleware/response/parse_json.rb +3 -3
- data/lib/zendesk_api/middleware/response/raise_error.rb +1 -1
- data/lib/zendesk_api/middleware/response/sanitize_response.rb +1 -1
- data/lib/zendesk_api/middleware/response/zendesk_request_event.rb +72 -0
- data/lib/zendesk_api/pagination.rb +3 -3
- data/lib/zendesk_api/resource.rb +16 -24
- data/lib/zendesk_api/resources.rb +84 -80
- data/lib/zendesk_api/search.rb +6 -6
- data/lib/zendesk_api/silent_mash.rb +1 -1
- data/lib/zendesk_api/track_changes.rb +2 -2
- data/lib/zendesk_api/trackie.rb +3 -3
- data/lib/zendesk_api/version.rb +1 -1
- data/lib/zendesk_api.rb +5 -5
- data/util/resource_handler.rb +5 -5
- data/util/verb_handler.rb +1 -1
- metadata +27 -16
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '0803fe00f30628a98bab5d0fbee4a63abd7e03adf5228aeb39c063fdd1c94836'
|
|
4
|
+
data.tar.gz: da85cd280c76b87f2ab6068e8f3d379b4f45c922d1ad8f82ffa4ec1877a34969
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ab2b864baf7fa4d916c2cab9bf27f470f226e45857b09f52a4648a97256ad2eccae2cca48cb52a9aea455329333f34bf4a5e889d951f679dd7157cf7642a034f
|
|
7
|
+
data.tar.gz: c6f9ff945f9f825ffa6483ab1ef521b4ef20b8377541f28a4fa8efa718eb52f49d08665c226a4673bf60325f2a57048ad8cfc769a8ea723e4641753dd6208290
|
data/lib/zendesk_api/actions.rb
CHANGED
|
@@ -43,8 +43,8 @@ module ZendeskAPI
|
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
# Saves, returning false if it fails and attaching the errors
|
|
46
|
-
def save(options = {}, &
|
|
47
|
-
save!(options, &
|
|
46
|
+
def save(options = {}, &)
|
|
47
|
+
save!(options, &)
|
|
48
48
|
rescue ZendeskAPI::Error::RecordInvalid => e
|
|
49
49
|
@errors = e.errors
|
|
50
50
|
false
|
|
@@ -66,7 +66,7 @@ module ZendeskAPI
|
|
|
66
66
|
self.class.associations.each do |association_data|
|
|
67
67
|
association_name = association_data[:name]
|
|
68
68
|
|
|
69
|
-
next unless send("#{association_name}_used?") && association = send(association_name)
|
|
69
|
+
next unless send("#{association_name}_used?") && (association = send(association_name))
|
|
70
70
|
|
|
71
71
|
inline_creation = association_data[:inline] == :create && new_record?
|
|
72
72
|
changed = association.is_a?(Collection) || association.changed?
|
|
@@ -110,7 +110,7 @@ module ZendeskAPI
|
|
|
110
110
|
@client = client # so we can use client.logger in rescue
|
|
111
111
|
|
|
112
112
|
raise ArgumentError, "No :id given" unless options[:id] || options["id"] || ancestors.include?(SingularResource)
|
|
113
|
-
association = options.delete(:association) || Association.new(:
|
|
113
|
+
association = options.delete(:association) || Association.new(class: self)
|
|
114
114
|
|
|
115
115
|
includes = Array(options[:include])
|
|
116
116
|
options[:include] = includes.join(",") if includes.any?
|
|
@@ -127,8 +127,8 @@ module ZendeskAPI
|
|
|
127
127
|
# Finds, returning nil if it fails
|
|
128
128
|
# @param [Client] client The {Client} object to be used
|
|
129
129
|
# @param [Hash] options Any additional GET parameters to be added
|
|
130
|
-
def find(client, options = {}, &
|
|
131
|
-
find!(client, options, &
|
|
130
|
+
def find(client, options = {}, &)
|
|
131
|
+
find!(client, options, &)
|
|
132
132
|
rescue ZendeskAPI::Error::ClientError
|
|
133
133
|
nil
|
|
134
134
|
end
|
|
@@ -157,8 +157,8 @@ module ZendeskAPI
|
|
|
157
157
|
# Creates, returning nil if it fails
|
|
158
158
|
# @param [Client] client The {Client} object to be used
|
|
159
159
|
# @param [Hash] options Any additional GET parameters to be added
|
|
160
|
-
def create(client, attributes = {}, &
|
|
161
|
-
create!(client, attributes, &
|
|
160
|
+
def create(client, attributes = {}, &)
|
|
161
|
+
create!(client, attributes, &)
|
|
162
162
|
rescue ZendeskAPI::Error::ClientError
|
|
163
163
|
nil
|
|
164
164
|
end
|
|
@@ -170,9 +170,9 @@ module ZendeskAPI
|
|
|
170
170
|
# @param [Client] client The {Client} object to be used
|
|
171
171
|
# @param [Array] attributes_array An array of resources to be created.
|
|
172
172
|
# @return [JobStatus] the {JobStatus} instance for this create job
|
|
173
|
-
def create_many!(client, attributes_array, association = Association.new(:
|
|
173
|
+
def create_many!(client, attributes_array, association = Association.new(class: self))
|
|
174
174
|
response = client.connection.post("#{association.generate_path}/create_many") do |req|
|
|
175
|
-
req.body = {
|
|
175
|
+
req.body = {resource_name => attributes_array}
|
|
176
176
|
|
|
177
177
|
yield req if block_given?
|
|
178
178
|
end
|
|
@@ -185,9 +185,9 @@ module ZendeskAPI
|
|
|
185
185
|
# Creates or updates resource using the create_or_update endpoint.
|
|
186
186
|
# @param [Client] client The {Client} object to be used
|
|
187
187
|
# @param [Hash] attributes The attributes to create.
|
|
188
|
-
def create_or_update!(client, attributes, association = Association.new(:
|
|
188
|
+
def create_or_update!(client, attributes, association = Association.new(class: self))
|
|
189
189
|
response = client.connection.post("#{association.generate_path}/create_or_update") do |req|
|
|
190
|
-
req.body = {
|
|
190
|
+
req.body = {singular_resource_name => attributes}
|
|
191
191
|
|
|
192
192
|
yield req if block_given?
|
|
193
193
|
end
|
|
@@ -204,10 +204,10 @@ module ZendeskAPI
|
|
|
204
204
|
#
|
|
205
205
|
# @return [JobStatus] the {JobStatus} instance for this destroy job
|
|
206
206
|
def create_or_update_many!(client, attributes)
|
|
207
|
-
association = Association.new(:
|
|
207
|
+
association = Association.new(class: self)
|
|
208
208
|
|
|
209
209
|
response = client.connection.post("#{association.generate_path}/create_or_update_many") do |req|
|
|
210
|
-
req.body = {
|
|
210
|
+
req.body = {resource_name => attributes}
|
|
211
211
|
|
|
212
212
|
yield req if block_given?
|
|
213
213
|
end
|
|
@@ -239,8 +239,8 @@ module ZendeskAPI
|
|
|
239
239
|
end
|
|
240
240
|
|
|
241
241
|
# Destroys, returning false on error.
|
|
242
|
-
def destroy(&
|
|
243
|
-
destroy!(&
|
|
242
|
+
def destroy(&)
|
|
243
|
+
destroy!(&)
|
|
244
244
|
rescue ZendeskAPI::Error::ClientError
|
|
245
245
|
false
|
|
246
246
|
end
|
|
@@ -249,15 +249,15 @@ module ZendeskAPI
|
|
|
249
249
|
# Deletes a resource given the id passed in.
|
|
250
250
|
# @param [Client] client The {Client} object to be used
|
|
251
251
|
# @param [Hash] opts The optional parameters to pass. Defaults to {}
|
|
252
|
-
def destroy!(client, opts = {}, &
|
|
253
|
-
new(client, opts).destroy!(&
|
|
252
|
+
def destroy!(client, opts = {}, &)
|
|
253
|
+
new(client, opts).destroy!(&)
|
|
254
254
|
|
|
255
255
|
true
|
|
256
256
|
end
|
|
257
257
|
|
|
258
258
|
# Destroys, returning false on error.
|
|
259
|
-
def destroy(client, attributes = {}, &
|
|
260
|
-
destroy!(client, attributes, &
|
|
259
|
+
def destroy(client, attributes = {}, &)
|
|
260
|
+
destroy!(client, attributes, &)
|
|
261
261
|
rescue ZendeskAPI::Error::ClientError
|
|
262
262
|
false
|
|
263
263
|
end
|
|
@@ -269,9 +269,9 @@ module ZendeskAPI
|
|
|
269
269
|
# @param [Client] client The {Client} object to be used
|
|
270
270
|
# @param [Array] ids An array of ids to destroy
|
|
271
271
|
# @return [JobStatus] the {JobStatus} instance for this destroy job
|
|
272
|
-
def destroy_many!(client, ids, association = Association.new(:
|
|
272
|
+
def destroy_many!(client, ids, association = Association.new(class: self))
|
|
273
273
|
response = client.connection.delete("#{association.generate_path}/destroy_many") do |req|
|
|
274
|
-
req.params = {
|
|
274
|
+
req.params = {ids: ids.join(",")}
|
|
275
275
|
|
|
276
276
|
yield req if block_given?
|
|
277
277
|
end
|
|
@@ -289,8 +289,8 @@ module ZendeskAPI
|
|
|
289
289
|
|
|
290
290
|
module ClassMethod
|
|
291
291
|
# Updates, returning false on error.
|
|
292
|
-
def update(client, attributes = {}, &
|
|
293
|
-
update!(client, attributes, &
|
|
292
|
+
def update(client, attributes = {}, &)
|
|
293
|
+
update!(client, attributes, &)
|
|
294
294
|
rescue ZendeskAPI::Error::ClientError
|
|
295
295
|
false
|
|
296
296
|
end
|
|
@@ -298,11 +298,11 @@ module ZendeskAPI
|
|
|
298
298
|
# Updates a resource given the id passed in.
|
|
299
299
|
# @param [Client] client The {Client} object to be used
|
|
300
300
|
# @param [Hash] attributes The attributes to update. Default to {
|
|
301
|
-
def update!(client, attributes = {}, &
|
|
301
|
+
def update!(client, attributes = {}, &)
|
|
302
302
|
ZendeskAPI::Client.check_deprecated_namespace_usage attributes, singular_resource_name
|
|
303
|
-
resource = new(client, :
|
|
303
|
+
resource = new(client, id: attributes.delete(:id), global: attributes.delete(:global), association: attributes.delete(:association))
|
|
304
304
|
resource.attributes.merge!(attributes)
|
|
305
|
-
resource.save!(:
|
|
305
|
+
resource.save!(force_update: resource.is_a?(SingularResource), &)
|
|
306
306
|
resource
|
|
307
307
|
end
|
|
308
308
|
end
|
|
@@ -315,14 +315,14 @@ module ZendeskAPI
|
|
|
315
315
|
# @param [Hash] attributes The attributes to update resources with
|
|
316
316
|
# @return [JobStatus] the {JobStatus} instance for this destroy job
|
|
317
317
|
def update_many!(client, ids_or_attributes, attributes = {})
|
|
318
|
-
association = attributes.delete(:association) || Association.new(:
|
|
318
|
+
association = attributes.delete(:association) || Association.new(class: self)
|
|
319
319
|
|
|
320
320
|
response = client.connection.put("#{association.generate_path}/update_many") do |req|
|
|
321
321
|
if attributes == {}
|
|
322
|
-
req.body = {
|
|
322
|
+
req.body = {resource_name => ids_or_attributes}
|
|
323
323
|
else
|
|
324
|
-
req.params = {
|
|
325
|
-
req.body = {
|
|
324
|
+
req.params = {ids: ids_or_attributes.join(",")}
|
|
325
|
+
req.body = {singular_resource_name => attributes}
|
|
326
326
|
end
|
|
327
327
|
|
|
328
328
|
yield req if block_given?
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
require_relative "helpers"
|
|
2
2
|
|
|
3
3
|
module ZendeskAPI
|
|
4
4
|
# Represents an association between two resources
|
|
@@ -43,7 +43,7 @@ module ZendeskAPI
|
|
|
43
43
|
# * with_parent - Include the parent path (false by default)
|
|
44
44
|
# * with_id - Include the instance id, if possible (true)
|
|
45
45
|
def generate_path(*args)
|
|
46
|
-
options = SilentMash.new(:
|
|
46
|
+
options = SilentMash.new(with_id: true)
|
|
47
47
|
if args.last.is_a?(Hash)
|
|
48
48
|
original_options = args.pop
|
|
49
49
|
options.merge!(original_options)
|
|
@@ -66,7 +66,7 @@ module ZendeskAPI
|
|
|
66
66
|
namespace[0] = @options.path || @options[:class].resource_path
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
if id = extract_id(instance, options, original_options)
|
|
69
|
+
if (id = extract_id(instance, options, original_options))
|
|
70
70
|
namespace << id
|
|
71
71
|
end
|
|
72
72
|
|
|
@@ -93,7 +93,7 @@ module ZendeskAPI
|
|
|
93
93
|
|
|
94
94
|
# @return [Array<String>] ['ZendeskAPI', 'Voice', etc.. ]
|
|
95
95
|
def ignorable_namespace_strings
|
|
96
|
-
ZendeskAPI::DataNamespace.descendants.map { |klass| klass.to_s.split(
|
|
96
|
+
ZendeskAPI::DataNamespace.descendants.map { |klass| klass.to_s.split("::") }.flatten.uniq
|
|
97
97
|
end
|
|
98
98
|
|
|
99
99
|
def _side_load(resource, side_loads)
|
|
@@ -106,10 +106,10 @@ module ZendeskAPI
|
|
|
106
106
|
end
|
|
107
107
|
end
|
|
108
108
|
|
|
109
|
-
def side_load_from_parent_id(resource, side_loads,
|
|
109
|
+
def side_load_from_parent_id(resource, side_loads, _key)
|
|
110
110
|
key = "#{resource.class.singular_resource_name}_id"
|
|
111
111
|
|
|
112
|
-
resource.send("#{options.name}=", _side_load(resource, side_loads.select {|side_load|
|
|
112
|
+
resource.send("#{options.name}=", _side_load(resource, side_loads.select { |side_load|
|
|
113
113
|
side_load[key] == resource.id
|
|
114
114
|
}))
|
|
115
115
|
end
|
|
@@ -117,7 +117,7 @@ module ZendeskAPI
|
|
|
117
117
|
def side_load_from_child_ids(resource, side_loads, plural_key)
|
|
118
118
|
ids = resource.send(plural_key)
|
|
119
119
|
|
|
120
|
-
resource.send("#{options.name}=", _side_load(resource, side_loads.select {|side_load|
|
|
120
|
+
resource.send("#{options.name}=", _side_load(resource, side_loads.select { |side_load|
|
|
121
121
|
ids.include?(side_load[options.include_key])
|
|
122
122
|
}))
|
|
123
123
|
end
|
|
@@ -178,7 +178,7 @@ module ZendeskAPI
|
|
|
178
178
|
|
|
179
179
|
def extract_id(instance, options, original_options)
|
|
180
180
|
if options[:with_id] && !@options[:class].ancestors.include?(SingularResource)
|
|
181
|
-
if instance
|
|
181
|
+
if instance&.id
|
|
182
182
|
instance.id
|
|
183
183
|
elsif options[:id]
|
|
184
184
|
original_options.delete(:id) || original_options.delete("id")
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
require_relative "helpers"
|
|
2
2
|
|
|
3
3
|
module ZendeskAPI
|
|
4
4
|
# This module holds association method for resources.
|
|
@@ -14,14 +14,14 @@ module ZendeskAPI
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def wrap_resource(resource, class_level_association, options = {})
|
|
17
|
-
instance_association = Association.new(class_level_association.merge(:
|
|
17
|
+
instance_association = Association.new(class_level_association.merge(parent: self))
|
|
18
18
|
klass = class_level_association[:class]
|
|
19
19
|
|
|
20
20
|
case resource
|
|
21
21
|
when Hash
|
|
22
|
-
klass.new(@client, resource.merge(:
|
|
22
|
+
klass.new(@client, resource.merge(association: instance_association))
|
|
23
23
|
when String, Integer
|
|
24
|
-
klass.new(@client,
|
|
24
|
+
klass.new(@client, options[:include_key] || :id => resource, :association => instance_association)
|
|
25
25
|
else
|
|
26
26
|
resource.association = instance_association
|
|
27
27
|
resource
|
|
@@ -40,12 +40,10 @@ module ZendeskAPI
|
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
def associated_with(name)
|
|
43
|
-
associations.
|
|
43
|
+
associations.each_with_object([]) do |association, associated_with|
|
|
44
44
|
if association[:include] == name.to_s
|
|
45
45
|
associated_with.push(Association.new(association))
|
|
46
46
|
end
|
|
47
|
-
|
|
48
|
-
associated_with
|
|
49
47
|
end
|
|
50
48
|
end
|
|
51
49
|
|
|
@@ -53,14 +51,14 @@ module ZendeskAPI
|
|
|
53
51
|
|
|
54
52
|
def build_association(klass, resource_name, options)
|
|
55
53
|
{
|
|
56
|
-
:
|
|
57
|
-
:
|
|
58
|
-
:
|
|
59
|
-
:
|
|
60
|
-
:
|
|
61
|
-
:
|
|
62
|
-
:
|
|
63
|
-
:
|
|
54
|
+
class: klass,
|
|
55
|
+
name: resource_name,
|
|
56
|
+
inline: options.delete(:inline),
|
|
57
|
+
path: options.delete(:path),
|
|
58
|
+
include: (options.delete(:include) || klass.resource_name).to_s,
|
|
59
|
+
include_key: (options.delete(:include_key) || :id).to_s,
|
|
60
|
+
singular: options.delete(:singular),
|
|
61
|
+
extensions: Array(options.delete(:extend))
|
|
64
62
|
}
|
|
65
63
|
end
|
|
66
64
|
|
|
@@ -75,7 +73,7 @@ module ZendeskAPI
|
|
|
75
73
|
# @param [Symbol] resource_name_or_class The underlying resource name or a class to get it from
|
|
76
74
|
# @param [Hash] class_level_options The options to pass to the method definition.
|
|
77
75
|
def has(resource_name_or_class, class_level_options = {})
|
|
78
|
-
if klass = class_level_options.delete(:class)
|
|
76
|
+
if (klass = class_level_options.delete(:class))
|
|
79
77
|
resource_name = resource_name_or_class
|
|
80
78
|
else
|
|
81
79
|
klass = resource_name_or_class
|
|
@@ -83,7 +81,8 @@ module ZendeskAPI
|
|
|
83
81
|
end
|
|
84
82
|
|
|
85
83
|
class_level_association = build_association(klass, resource_name, class_level_options)
|
|
86
|
-
class_level_association
|
|
84
|
+
class_level_association[:singular] = true
|
|
85
|
+
class_level_association[:id_column] = "#{resource_name}_id"
|
|
87
86
|
|
|
88
87
|
associations << class_level_association
|
|
89
88
|
|
|
@@ -105,14 +104,14 @@ module ZendeskAPI
|
|
|
105
104
|
return cached if cached && !instance_options[:reload]
|
|
106
105
|
|
|
107
106
|
# find and cache association
|
|
108
|
-
instance_association = Association.new(association.merge(:
|
|
109
|
-
resource = if klass.respond_to?(:find) && resource_id = method_missing(association[:id_column])
|
|
110
|
-
klass.find(@client, :
|
|
111
|
-
elsif found = method_missing(association[:name].to_sym)
|
|
112
|
-
wrap_resource(found, association, :
|
|
107
|
+
instance_association = Association.new(association.merge(parent: self))
|
|
108
|
+
resource = if klass.respond_to?(:find) && (resource_id = method_missing(association[:id_column]))
|
|
109
|
+
klass.find(@client, id: resource_id, association: instance_association)
|
|
110
|
+
elsif (found = method_missing(association[:name].to_sym))
|
|
111
|
+
wrap_resource(found, association, include_key: association[:include_key])
|
|
113
112
|
elsif klass.superclass == DataResource && !association[:inline]
|
|
114
|
-
response = @client.connection.get(instance_association.generate_path(:
|
|
115
|
-
klass.new(@client, response.body[klass.singular_resource_name].merge(:
|
|
113
|
+
response = @client.connection.get(instance_association.generate_path(with_parent: true))
|
|
114
|
+
klass.new(@client, response.body[klass.singular_resource_name].merge(association: instance_association))
|
|
116
115
|
end
|
|
117
116
|
|
|
118
117
|
send("#{association[:id_column]}=", resource.id) if resource && has_key?(association[:id_column])
|
|
@@ -134,7 +133,7 @@ module ZendeskAPI
|
|
|
134
133
|
# @param [Symbol] resource_name_or_class The underlying resource name or class to get it from
|
|
135
134
|
# @param [Hash] class_level_options The options to pass to the method definition.
|
|
136
135
|
def has_many(resource_name_or_class, class_level_options = {})
|
|
137
|
-
if klass = class_level_options.delete(:class)
|
|
136
|
+
if (klass = class_level_options.delete(:class))
|
|
138
137
|
resource_name = resource_name_or_class
|
|
139
138
|
else
|
|
140
139
|
klass = resource_name_or_class
|
|
@@ -142,7 +141,8 @@ module ZendeskAPI
|
|
|
142
141
|
end
|
|
143
142
|
|
|
144
143
|
class_level_association = build_association(klass, resource_name, class_level_options)
|
|
145
|
-
class_level_association
|
|
144
|
+
class_level_association[:singular] = false
|
|
145
|
+
class_level_association[:id_column] = "#{resource_name}_ids"
|
|
146
146
|
|
|
147
147
|
associations << class_level_association
|
|
148
148
|
|
|
@@ -164,12 +164,12 @@ module ZendeskAPI
|
|
|
164
164
|
return cached if cached && !instance_opts[:reload]
|
|
165
165
|
|
|
166
166
|
# find and cache association
|
|
167
|
-
instance_association = Association.new(association.merge(:
|
|
167
|
+
instance_association = Association.new(association.merge(parent: self))
|
|
168
168
|
singular_resource_name = Inflection.singular(association[:name].to_s)
|
|
169
169
|
|
|
170
170
|
resources = if (ids = method_missing("#{singular_resource_name}_ids")) && ids.any?
|
|
171
171
|
ids.map do |id|
|
|
172
|
-
klass.find(@client, :
|
|
172
|
+
klass.find(@client, id: id, association: instance_association)
|
|
173
173
|
end.compact
|
|
174
174
|
elsif (resources = method_missing(association[:name].to_sym)) && resources.any?
|
|
175
175
|
resources.map { |res| wrap_resource(res, association) }
|
|
@@ -177,7 +177,7 @@ module ZendeskAPI
|
|
|
177
177
|
[]
|
|
178
178
|
end
|
|
179
179
|
|
|
180
|
-
collection = ZendeskAPI::Collection.new(@client, klass, instance_opts.merge(:
|
|
180
|
+
collection = ZendeskAPI::Collection.new(@client, klass, instance_opts.merge(association: instance_association))
|
|
181
181
|
|
|
182
182
|
if association[:extensions].any?
|
|
183
183
|
collection.extend(*association[:extensions])
|
|
@@ -198,7 +198,7 @@ module ZendeskAPI
|
|
|
198
198
|
wrapped = resources.map { |attr| wrap_resource(attr, association) }
|
|
199
199
|
send(association[:name]).replace(wrapped)
|
|
200
200
|
else
|
|
201
|
-
resources.association = Association.new(association.merge(:
|
|
201
|
+
resources.association = Association.new(association.merge(parent: self))
|
|
202
202
|
instance_variable_set("@#{association[:name]}", resources)
|
|
203
203
|
end
|
|
204
204
|
|
data/lib/zendesk_api/client.rb
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
require_relative "version"
|
|
2
|
+
require_relative "sideloading"
|
|
3
|
+
require_relative "configuration"
|
|
4
|
+
require_relative "collection"
|
|
5
|
+
require_relative "lru_cache"
|
|
6
|
+
require_relative "silent_mash"
|
|
7
|
+
require_relative "middleware/request/etag_cache"
|
|
8
|
+
require_relative "middleware/request/retry"
|
|
9
|
+
require_relative "middleware/request/raise_rate_limited"
|
|
10
|
+
require_relative "middleware/request/upload"
|
|
11
|
+
require_relative "middleware/request/encode_json"
|
|
12
|
+
require_relative "middleware/request/api_token_impersonate"
|
|
13
|
+
require_relative "middleware/request/url_based_access_token"
|
|
14
|
+
require_relative "middleware/response/callback"
|
|
15
|
+
require_relative "middleware/response/deflate"
|
|
16
|
+
require_relative "middleware/response/gzip"
|
|
17
|
+
require_relative "middleware/response/sanitize_response"
|
|
18
|
+
require_relative "middleware/response/parse_iso_dates"
|
|
19
|
+
require_relative "middleware/response/parse_json"
|
|
20
|
+
require_relative "middleware/response/raise_error"
|
|
21
|
+
require_relative "middleware/response/logger"
|
|
22
|
+
require_relative "middleware/response/zendesk_request_event"
|
|
23
|
+
require_relative "delegator"
|
|
22
24
|
|
|
23
25
|
module ZendeskAPI
|
|
24
26
|
# The top-level class that handles configuration and connection to the Zendesk API.
|
|
@@ -34,7 +36,7 @@ module ZendeskAPI
|
|
|
34
36
|
# Handles resources such as 'tickets'. Any options are passed to the underlying collection, except reload which disregards
|
|
35
37
|
# memoization and creates a new Collection instance.
|
|
36
38
|
# @return [Collection] Collection instance for resource
|
|
37
|
-
def method_missing(method, *args, &
|
|
39
|
+
def method_missing(method, *args, &)
|
|
38
40
|
method = method.to_s
|
|
39
41
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
40
42
|
unless config.use_resource_cache
|
|
@@ -43,7 +45,7 @@ module ZendeskAPI
|
|
|
43
45
|
return ZendeskAPI::Collection.new(self, resource_class, options)
|
|
44
46
|
end
|
|
45
47
|
|
|
46
|
-
@resource_cache[method] ||= {
|
|
48
|
+
@resource_cache[method] ||= {class: nil, cache: ZendeskAPI::LRUCache.new}
|
|
47
49
|
if !options.delete(:reload) && (cached = @resource_cache[method][:cache].read(options.hash))
|
|
48
50
|
cached
|
|
49
51
|
else
|
|
@@ -62,21 +64,21 @@ module ZendeskAPI
|
|
|
62
64
|
# @return [ZendeskAPI::User] Current user or nil
|
|
63
65
|
def current_user(reload = false)
|
|
64
66
|
return @current_user if @current_user && !reload
|
|
65
|
-
@current_user = users.find(:
|
|
67
|
+
@current_user = users.find(id: "me")
|
|
66
68
|
end
|
|
67
69
|
|
|
68
70
|
# Returns the current account
|
|
69
71
|
# @return [Hash] The attributes of the current account or nil
|
|
70
72
|
def current_account(reload = false)
|
|
71
73
|
return @current_account if @current_account && !reload
|
|
72
|
-
@current_account = SilentMash.new(connection.get(
|
|
74
|
+
@current_account = SilentMash.new(connection.get("account/resolve").body)
|
|
73
75
|
end
|
|
74
76
|
|
|
75
77
|
# Returns the current locale
|
|
76
78
|
# @return [ZendeskAPI::Locale] Current locale or nil
|
|
77
79
|
def current_locale(reload = false)
|
|
78
80
|
return @locale if @locale && !reload
|
|
79
|
-
@locale = locales.find(:
|
|
81
|
+
@locale = locales.find(id: "current")
|
|
80
82
|
end
|
|
81
83
|
|
|
82
84
|
# Creates a new {Client} instance and yields {#config}.
|
|
@@ -95,6 +97,7 @@ module ZendeskAPI
|
|
|
95
97
|
@resource_cache = {}
|
|
96
98
|
|
|
97
99
|
check_url
|
|
100
|
+
check_instrumentation
|
|
98
101
|
|
|
99
102
|
config.retry = !!config.retry # nil -> false
|
|
100
103
|
|
|
@@ -104,6 +107,25 @@ module ZendeskAPI
|
|
|
104
107
|
add_warning_callback
|
|
105
108
|
end
|
|
106
109
|
|
|
110
|
+
# token impersonation for the scope of the block
|
|
111
|
+
# @param [String] username The username (email) of the user to impersonate
|
|
112
|
+
# @yield The block to run while impersonating the user
|
|
113
|
+
# @example
|
|
114
|
+
# client.api_token_impersonate("otheruser@yourcompany.com") do
|
|
115
|
+
# client.tickets.create(:subject => "Help!")
|
|
116
|
+
# end
|
|
117
|
+
#
|
|
118
|
+
# # creates a ticket on behalf of otheruser
|
|
119
|
+
# @return
|
|
120
|
+
# yielded value
|
|
121
|
+
def api_token_impersonate(username)
|
|
122
|
+
avant = Thread.current[:zendesk_thread_local_username]
|
|
123
|
+
Thread.current[:zendesk_thread_local_username] = username
|
|
124
|
+
yield
|
|
125
|
+
ensure
|
|
126
|
+
Thread.current[:zendesk_thread_local_username] = avant
|
|
127
|
+
end
|
|
128
|
+
|
|
107
129
|
# Creates a connection if there is none, otherwise returns the existing connection.
|
|
108
130
|
#
|
|
109
131
|
# @return [Faraday::Connection] Faraday connection for the client
|
|
@@ -146,6 +168,7 @@ module ZendeskAPI
|
|
|
146
168
|
Faraday.new(config.options) do |builder|
|
|
147
169
|
# response
|
|
148
170
|
builder.use ZendeskAPI::Middleware::Response::RaiseError
|
|
171
|
+
builder.use ZendeskAPI::Middleware::Response::ZendeskRequestEvent, instrumentation: config.instrumentation, logger: config.logger if config.instrumentation
|
|
149
172
|
builder.use ZendeskAPI::Middleware::Response::Callback, self
|
|
150
173
|
builder.use ZendeskAPI::Middleware::Response::Logger, config.logger if config.logger
|
|
151
174
|
builder.use ZendeskAPI::Middleware::Response::ParseIsoDates
|
|
@@ -161,7 +184,7 @@ module ZendeskAPI
|
|
|
161
184
|
set_authentication(builder, config)
|
|
162
185
|
|
|
163
186
|
if config.cache
|
|
164
|
-
builder.use ZendeskAPI::Middleware::Request::EtagCache, :cache
|
|
187
|
+
builder.use ZendeskAPI::Middleware::Request::EtagCache, cache: config.cache, instrumentation: config.instrumentation
|
|
165
188
|
end
|
|
166
189
|
|
|
167
190
|
builder.use ZendeskAPI::Middleware::Request::Upload
|
|
@@ -171,31 +194,41 @@ module ZendeskAPI
|
|
|
171
194
|
# Should always be first in the stack
|
|
172
195
|
if config.retry
|
|
173
196
|
builder.use ZendeskAPI::Middleware::Request::Retry,
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
197
|
+
logger: config.logger,
|
|
198
|
+
retry_codes: config.retry_codes,
|
|
199
|
+
retry_on_exception: config.retry_on_exception,
|
|
200
|
+
instrumentation: config.instrumentation
|
|
177
201
|
end
|
|
178
202
|
if config.raise_error_when_rate_limited
|
|
179
|
-
builder.use ZendeskAPI::Middleware::Request::RaiseRateLimited, :
|
|
203
|
+
builder.use ZendeskAPI::Middleware::Request::RaiseRateLimited, logger: config.logger
|
|
180
204
|
end
|
|
181
205
|
|
|
182
206
|
builder.adapter(*adapter, &config.adapter_proc)
|
|
207
|
+
builder.use ZendeskAPI::Middleware::Request::ApiTokenImpersonate
|
|
183
208
|
end
|
|
184
209
|
end
|
|
185
210
|
|
|
186
211
|
private
|
|
187
212
|
|
|
188
213
|
def resource_class_for(method)
|
|
189
|
-
resource_name = ZendeskAPI::Helpers.modulize_string(Inflection.singular(method.to_s.gsub(/\W/,
|
|
214
|
+
resource_name = ZendeskAPI::Helpers.modulize_string(Inflection.singular(method.to_s.gsub(/\W/, "")))
|
|
190
215
|
ZendeskAPI::Association.class_from_namespace(resource_name)
|
|
191
216
|
end
|
|
192
217
|
|
|
193
218
|
def check_url
|
|
194
|
-
if !config.allow_http && !config.url.start_with?(
|
|
219
|
+
if !config.allow_http && !config.url.start_with?("https://")
|
|
195
220
|
raise ArgumentError, "zendesk_api is ssl only; url must begin with https://"
|
|
196
221
|
end
|
|
197
222
|
end
|
|
198
223
|
|
|
224
|
+
def check_instrumentation
|
|
225
|
+
return unless config.instrumentation
|
|
226
|
+
|
|
227
|
+
unless config.instrumentation.respond_to?(:instrument)
|
|
228
|
+
raise ArgumentError, "instrumentation must respond to #instrument"
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
199
232
|
def set_raise_error_when_rated_limited
|
|
200
233
|
config.raise_error_when_rate_limited = if config.retry
|
|
201
234
|
false
|
|
@@ -217,17 +250,17 @@ module ZendeskAPI
|
|
|
217
250
|
|
|
218
251
|
def set_default_logger
|
|
219
252
|
if config.logger.nil? || config.logger == true
|
|
220
|
-
require
|
|
253
|
+
require "logger"
|
|
221
254
|
config.logger = Logger.new($stderr)
|
|
222
255
|
config.logger.level = Logger::WARN
|
|
223
256
|
end
|
|
224
257
|
end
|
|
225
258
|
|
|
226
259
|
def add_warning_callback
|
|
227
|
-
return unless logger = config.logger
|
|
260
|
+
return unless (logger = config.logger)
|
|
228
261
|
|
|
229
262
|
insert_callback do |env|
|
|
230
|
-
if warning = env[:response_headers]["X-Zendesk-API-Warn"]
|
|
263
|
+
if (warning = env[:response_headers]["X-Zendesk-API-Warn"])
|
|
231
264
|
logger.warn "WARNING: #{warning}"
|
|
232
265
|
end
|
|
233
266
|
end
|