discourse_zendesk_api 1.0.0

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.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +176 -0
  3. data/lib/zendesk_api/actions.rb +334 -0
  4. data/lib/zendesk_api/association.rb +195 -0
  5. data/lib/zendesk_api/associations.rb +212 -0
  6. data/lib/zendesk_api/client.rb +243 -0
  7. data/lib/zendesk_api/collection.rb +474 -0
  8. data/lib/zendesk_api/configuration.rb +79 -0
  9. data/lib/zendesk_api/core_ext/inflection.rb +3 -0
  10. data/lib/zendesk_api/delegator.rb +5 -0
  11. data/lib/zendesk_api/error.rb +49 -0
  12. data/lib/zendesk_api/helpers.rb +24 -0
  13. data/lib/zendesk_api/lru_cache.rb +39 -0
  14. data/lib/zendesk_api/middleware/request/encode_json.rb +26 -0
  15. data/lib/zendesk_api/middleware/request/etag_cache.rb +52 -0
  16. data/lib/zendesk_api/middleware/request/raise_rate_limited.rb +31 -0
  17. data/lib/zendesk_api/middleware/request/retry.rb +59 -0
  18. data/lib/zendesk_api/middleware/request/upload.rb +86 -0
  19. data/lib/zendesk_api/middleware/request/url_based_access_token.rb +26 -0
  20. data/lib/zendesk_api/middleware/response/callback.rb +21 -0
  21. data/lib/zendesk_api/middleware/response/deflate.rb +17 -0
  22. data/lib/zendesk_api/middleware/response/gzip.rb +19 -0
  23. data/lib/zendesk_api/middleware/response/logger.rb +44 -0
  24. data/lib/zendesk_api/middleware/response/parse_iso_dates.rb +30 -0
  25. data/lib/zendesk_api/middleware/response/parse_json.rb +23 -0
  26. data/lib/zendesk_api/middleware/response/raise_error.rb +26 -0
  27. data/lib/zendesk_api/middleware/response/sanitize_response.rb +11 -0
  28. data/lib/zendesk_api/resource.rb +208 -0
  29. data/lib/zendesk_api/resources.rb +971 -0
  30. data/lib/zendesk_api/sideloading.rb +58 -0
  31. data/lib/zendesk_api/silent_mash.rb +8 -0
  32. data/lib/zendesk_api/track_changes.rb +85 -0
  33. data/lib/zendesk_api/trackie.rb +13 -0
  34. data/lib/zendesk_api/verbs.rb +65 -0
  35. data/lib/zendesk_api/version.rb +3 -0
  36. data/lib/zendesk_api.rb +4 -0
  37. data/util/resource_handler.rb +74 -0
  38. data/util/verb_handler.rb +16 -0
  39. metadata +166 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6b93af5c412815bf61b05f472ef44d6eed4950bed97b91ed3dffa3863b245717
4
+ data.tar.gz: f41010a89c776b543dd20b7a24a87b2a743d854876829a6379d781f4b9a4629c
5
+ SHA512:
6
+ metadata.gz: 2046f5546c29378a80bff6341cd9526551475e3051e70246d29c76031fc81ee74742723194c8bc62af3b232cf06360e487c814b860df4f7c81abc0f2bf2e8a15
7
+ data.tar.gz: 93c60d666ef7862e2b7c5ed8fed5e6c5ebe7ceb0090567eb7a6e0c51a8ee89212097fc014d7f5f72de391b7a91d7fccd4ab9d7e496b525312c111ee6b4683ab1
data/LICENSE ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,334 @@
1
+ module ZendeskAPI
2
+ module ResponseHandler
3
+ def handle_response(response)
4
+ if response.body.is_a?(Hash) && response.body[self.class.singular_resource_name]
5
+ @attributes.replace(@attributes.deep_merge(response.body[self.class.singular_resource_name]))
6
+ end
7
+ end
8
+ end
9
+
10
+ module Save
11
+ include ResponseHandler
12
+
13
+ # If this resource hasn't been deleted, then create or save it.
14
+ # Executes a POST if it is a {Data#new_record?}, otherwise a PUT.
15
+ # Merges returned attributes on success.
16
+ # @return [Boolean] Success?
17
+ def save!(options = {})
18
+ return false if respond_to?(:destroyed?) && destroyed?
19
+
20
+ if new_record? && !options[:force_update]
21
+ method = :post
22
+ req_path = path
23
+ else
24
+ method = :put
25
+ req_path = url || path
26
+ end
27
+
28
+ req_path = options[:path] if options[:path]
29
+
30
+ save_associations
31
+
32
+ @response = @client.connection.send(method, req_path) do |req|
33
+ req.body = attributes_for_save.merge(@global_params)
34
+
35
+ yield req if block_given?
36
+ end
37
+
38
+ handle_response(@response)
39
+
40
+ @attributes.clear_changes
41
+ clear_associations
42
+ true
43
+ end
44
+
45
+ # Saves, returning false if it fails and attaching the errors
46
+ def save(options = {}, &block)
47
+ save!(options, &block)
48
+ rescue ZendeskAPI::Error::RecordInvalid => e
49
+ @errors = e.errors
50
+ false
51
+ rescue ZendeskAPI::Error::ClientError
52
+ false
53
+ end
54
+
55
+ # Removes all cached associations
56
+ def clear_associations
57
+ self.class.associations.each do |association_data|
58
+ name = association_data[:name]
59
+ instance_variable_set("@#{name}", nil) if instance_variable_defined?("@#{name}")
60
+ end
61
+ end
62
+
63
+ # Saves associations
64
+ # Takes into account inlining, collections, and id setting on the parent resource.
65
+ def save_associations
66
+ self.class.associations.each do |association_data|
67
+ association_name = association_data[:name]
68
+
69
+ next unless send("#{association_name}_used?") && association = send(association_name)
70
+
71
+ inline_creation = association_data[:inline] == :create && new_record?
72
+ changed = association.is_a?(Collection) || association.changed?
73
+
74
+ if association.respond_to?(:save) && changed && !inline_creation && association.save
75
+ send("#{association_name}=", association) # set id/ids columns
76
+ end
77
+
78
+ if (association_data[:inline] == true || inline_creation) && changed
79
+ attributes[association_name] = association.to_param
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ module Read
86
+ include ResponseHandler
87
+ include ZendeskAPI::Sideloading
88
+
89
+ def self.included(base)
90
+ base.extend(ClassMethods)
91
+ end
92
+
93
+ # Reloads a resource.
94
+ def reload!
95
+ response = @client.connection.get(path) do |req|
96
+ yield req if block_given?
97
+ end
98
+
99
+ handle_response(response)
100
+ attributes.clear_changes
101
+ self
102
+ end
103
+
104
+ module ClassMethods
105
+ # Finds a resource by an id and any options passed in.
106
+ # A custom path to search at can be passed into opts. It defaults to the {Data.resource_name} of the class.
107
+ # @param [Client] client The {Client} object to be used
108
+ # @param [Hash] options Any additional GET parameters to be added
109
+ def find!(client, options = {})
110
+ @client = client # so we can use client.logger in rescue
111
+
112
+ raise ArgumentError, "No :id given" unless options[:id] || options["id"] || ancestors.include?(SingularResource)
113
+ association = options.delete(:association) || Association.new(:class => self)
114
+
115
+ includes = Array(options[:include])
116
+ options[:include] = includes.join(",") if includes.any?
117
+
118
+ response = client.connection.get(association.generate_path(options)) do |req|
119
+ req.params = options
120
+
121
+ yield req if block_given?
122
+ end
123
+
124
+ new_from_response(client, response, includes)
125
+ end
126
+
127
+ # Finds, returning nil if it fails
128
+ # @param [Client] client The {Client} object to be used
129
+ # @param [Hash] options Any additional GET parameters to be added
130
+ def find(client, options = {}, &block)
131
+ find!(client, options, &block)
132
+ rescue ZendeskAPI::Error::ClientError
133
+ nil
134
+ end
135
+ end
136
+ end
137
+
138
+ module Create
139
+ include Save
140
+
141
+ def self.included(base)
142
+ base.extend(ClassMethods)
143
+ end
144
+
145
+ module ClassMethods
146
+ # Create a resource given the attributes passed in.
147
+ # @param [Client] client The {Client} object to be used
148
+ # @param [Hash] attributes The attributes to create.
149
+ def create!(client, attributes = {}, &block)
150
+ ZendeskAPI::Client.check_deprecated_namespace_usage attributes, singular_resource_name
151
+
152
+ new(client, attributes).tap do |resource|
153
+ resource.save!(&block)
154
+ end
155
+ end
156
+
157
+ # Creates, returning nil if it fails
158
+ # @param [Client] client The {Client} object to be used
159
+ # @param [Hash] options Any additional GET parameters to be added
160
+ def create(client, attributes = {}, &block)
161
+ create!(client, attributes, &block)
162
+ rescue ZendeskAPI::Error::ClientError
163
+ nil
164
+ end
165
+ end
166
+ end
167
+
168
+ module CreateMany
169
+ # Creates multiple resources using the create_many endpoint.
170
+ # @param [Client] client The {Client} object to be used
171
+ # @param [Array] attributes_array An array of resources to be created.
172
+ # @return [JobStatus] the {JobStatus} instance for this create job
173
+ def create_many!(client, attributes_array, association = Association.new(:class => self))
174
+ response = client.connection.post("#{association.generate_path}/create_many") do |req|
175
+ req.body = { resource_name => attributes_array }
176
+
177
+ yield req if block_given?
178
+ end
179
+
180
+ JobStatus.new_from_response(client, response)
181
+ end
182
+ end
183
+
184
+ module CreateOrUpdate
185
+ # Creates or updates resource using the create_or_update endpoint.
186
+ # @param [Client] client The {Client} object to be used
187
+ # @param [Hash] attributes The attributes to create.
188
+ def create_or_update!(client, attributes, association = Association.new(:class => self))
189
+ response = client.connection.post("#{association.generate_path}/create_or_update") do |req|
190
+ req.body = { singular_resource_name => attributes }
191
+
192
+ yield req if block_given?
193
+ end
194
+
195
+ new_from_response(client, response, Array(association.options[:include]))
196
+ end
197
+ end
198
+
199
+ module CreateOrUpdateMany
200
+ # Creates or updates multiple resources using the create_or_update_many endpoint.
201
+ #
202
+ # @param [Client] client The {Client} object to be used
203
+ # @param [Array<Hash>] attributes The attributes to update resources with
204
+ #
205
+ # @return [JobStatus] the {JobStatus} instance for this destroy job
206
+ def create_or_update_many!(client, attributes)
207
+ association = Association.new(:class => self)
208
+
209
+ response = client.connection.post("#{association.generate_path}/create_or_update_many") do |req|
210
+ req.body = { resource_name => attributes }
211
+
212
+ yield req if block_given?
213
+ end
214
+
215
+ JobStatus.new_from_response(client, response)
216
+ end
217
+ end
218
+
219
+ module Destroy
220
+ def self.included(klass)
221
+ klass.extend(ClassMethod)
222
+ end
223
+
224
+ # Has this object been deleted?
225
+ def destroyed?
226
+ @destroyed ||= false
227
+ end
228
+
229
+ # If this resource hasn't already been deleted, then do so.
230
+ # @return [Boolean] Successful?
231
+ def destroy!
232
+ return false if destroyed? || new_record?
233
+
234
+ @client.connection.delete(url || path) do |req|
235
+ yield req if block_given?
236
+ end
237
+
238
+ @destroyed = true
239
+ end
240
+
241
+ # Destroys, returning false on error.
242
+ def destroy(&block)
243
+ destroy!(&block)
244
+ rescue ZendeskAPI::Error::ClientError
245
+ false
246
+ end
247
+
248
+ module ClassMethod
249
+ # Deletes a resource given the id passed in.
250
+ # @param [Client] client The {Client} object to be used
251
+ # @param [Hash] opts The optional parameters to pass. Defaults to {}
252
+ def destroy!(client, opts = {}, &block)
253
+ new(client, opts).destroy!(&block)
254
+
255
+ true
256
+ end
257
+
258
+ # Destroys, returning false on error.
259
+ def destroy(client, attributes = {}, &block)
260
+ destroy!(client, attributes, &block)
261
+ rescue ZendeskAPI::Error::ClientError
262
+ false
263
+ end
264
+ end
265
+ end
266
+
267
+ module DestroyMany
268
+ # Destroys multiple resources using the destroy_many endpoint.
269
+ # @param [Client] client The {Client} object to be used
270
+ # @param [Array] ids An array of ids to destroy
271
+ # @return [JobStatus] the {JobStatus} instance for this destroy job
272
+ def destroy_many!(client, ids, association = Association.new(:class => self))
273
+ response = client.connection.delete("#{association.generate_path}/destroy_many") do |req|
274
+ req.params = { :ids => ids.join(',') }
275
+
276
+ yield req if block_given?
277
+ end
278
+
279
+ JobStatus.new_from_response(client, response)
280
+ end
281
+ end
282
+
283
+ module Update
284
+ include Save
285
+
286
+ def self.included(klass)
287
+ klass.extend(ClassMethod)
288
+ end
289
+
290
+ module ClassMethod
291
+ # Updates, returning false on error.
292
+ def update(client, attributes = {}, &block)
293
+ update!(client, attributes, &block)
294
+ rescue ZendeskAPI::Error::ClientError
295
+ false
296
+ end
297
+
298
+ # Updates a resource given the id passed in.
299
+ # @param [Client] client The {Client} object to be used
300
+ # @param [Hash] attributes The attributes to update. Default to {
301
+ def update!(client, attributes = {}, &block)
302
+ ZendeskAPI::Client.check_deprecated_namespace_usage attributes, singular_resource_name
303
+ resource = new(client, :id => attributes.delete(:id), :global => attributes.delete(:global), :association => attributes.delete(:association))
304
+ resource.attributes.merge!(attributes)
305
+ resource.save!(:force_update => resource.is_a?(SingularResource), &block)
306
+ resource
307
+ end
308
+ end
309
+ end
310
+
311
+ module UpdateMany
312
+ # Updates multiple resources using the update_many endpoint.
313
+ # @param [Client] client The {Client} object to be used
314
+ # @param [Array] ids_or_attributes An array of ids or arributes including ids to update
315
+ # @param [Hash] attributes The attributes to update resources with
316
+ # @return [JobStatus] the {JobStatus} instance for this destroy job
317
+ def update_many!(client, ids_or_attributes, attributes = {})
318
+ association = attributes.delete(:association) || Association.new(:class => self)
319
+
320
+ response = client.connection.put("#{association.generate_path}/update_many") do |req|
321
+ if attributes == {}
322
+ req.body = { resource_name => ids_or_attributes }
323
+ else
324
+ req.params = { :ids => ids_or_attributes.join(',') }
325
+ req.body = { singular_resource_name => attributes }
326
+ end
327
+
328
+ yield req if block_given?
329
+ end
330
+
331
+ JobStatus.new_from_response(client, response)
332
+ end
333
+ end
334
+ end