gitlab-customer-support-operations_zendesk 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 (58) hide show
  1. checksums.yaml +7 -0
  2. data/lib/support_ops_zendesk/packages.rb +89 -0
  3. data/lib/support_ops_zendesk/zendesk/app_installations.rb +62 -0
  4. data/lib/support_ops_zendesk/zendesk/app_job_statuses.rb +157 -0
  5. data/lib/support_ops_zendesk/zendesk/apps.rb +476 -0
  6. data/lib/support_ops_zendesk/zendesk/articles.rb +362 -0
  7. data/lib/support_ops_zendesk/zendesk/audit_logs.rb +166 -0
  8. data/lib/support_ops_zendesk/zendesk/automations.rb +390 -0
  9. data/lib/support_ops_zendesk/zendesk/base.rb +472 -0
  10. data/lib/support_ops_zendesk/zendesk/brands.rb +172 -0
  11. data/lib/support_ops_zendesk/zendesk/client.rb +91 -0
  12. data/lib/support_ops_zendesk/zendesk/comments.rb +37 -0
  13. data/lib/support_ops_zendesk/zendesk/configuration.rb +138 -0
  14. data/lib/support_ops_zendesk/zendesk/custom_roles.rb +224 -0
  15. data/lib/support_ops_zendesk/zendesk/dynamic_content.rb +297 -0
  16. data/lib/support_ops_zendesk/zendesk/dynamic_content_variants.rb +309 -0
  17. data/lib/support_ops_zendesk/zendesk/group_memberships.rb +337 -0
  18. data/lib/support_ops_zendesk/zendesk/groups.rb +385 -0
  19. data/lib/support_ops_zendesk/zendesk/help_center_categories.rb +332 -0
  20. data/lib/support_ops_zendesk/zendesk/help_center_content_tags.rb +237 -0
  21. data/lib/support_ops_zendesk/zendesk/help_center_management_permission_groups.rb +271 -0
  22. data/lib/support_ops_zendesk/zendesk/help_center_sections.rb +378 -0
  23. data/lib/support_ops_zendesk/zendesk/help_center_topics.rb +274 -0
  24. data/lib/support_ops_zendesk/zendesk/help_center_user_segments.rb +279 -0
  25. data/lib/support_ops_zendesk/zendesk/job_statuses.rb +231 -0
  26. data/lib/support_ops_zendesk/zendesk/locales.rb +326 -0
  27. data/lib/support_ops_zendesk/zendesk/macros.rb +407 -0
  28. data/lib/support_ops_zendesk/zendesk/oauth_clients.rb +186 -0
  29. data/lib/support_ops_zendesk/zendesk/oauth_tokens.rb +114 -0
  30. data/lib/support_ops_zendesk/zendesk/organization_fields.rb +282 -0
  31. data/lib/support_ops_zendesk/zendesk/organization_memberships.rb +336 -0
  32. data/lib/support_ops_zendesk/zendesk/organizations.rb +568 -0
  33. data/lib/support_ops_zendesk/zendesk/requester_roles.rb +58 -0
  34. data/lib/support_ops_zendesk/zendesk/satisfaction_reasons.rb +161 -0
  35. data/lib/support_ops_zendesk/zendesk/schedule_holidays.rb +27 -0
  36. data/lib/support_ops_zendesk/zendesk/schedules.rb +192 -0
  37. data/lib/support_ops_zendesk/zendesk/search.rb +185 -0
  38. data/lib/support_ops_zendesk/zendesk/sla_policies.rb +302 -0
  39. data/lib/support_ops_zendesk/zendesk/targets.rb +96 -0
  40. data/lib/support_ops_zendesk/zendesk/theme_job_statuses.rb +154 -0
  41. data/lib/support_ops_zendesk/zendesk/themes.rb +328 -0
  42. data/lib/support_ops_zendesk/zendesk/ticket_field_options.rb +154 -0
  43. data/lib/support_ops_zendesk/zendesk/ticket_fields.rb +357 -0
  44. data/lib/support_ops_zendesk/zendesk/ticket_forms.rb +370 -0
  45. data/lib/support_ops_zendesk/zendesk/ticket_user_types.rb +67 -0
  46. data/lib/support_ops_zendesk/zendesk/tickets.rb +837 -0
  47. data/lib/support_ops_zendesk/zendesk/translations.rb +310 -0
  48. data/lib/support_ops_zendesk/zendesk/trigger_categories.rb +275 -0
  49. data/lib/support_ops_zendesk/zendesk/triggers.rb +427 -0
  50. data/lib/support_ops_zendesk/zendesk/user_field_options.rb +153 -0
  51. data/lib/support_ops_zendesk/zendesk/user_fields.rb +312 -0
  52. data/lib/support_ops_zendesk/zendesk/users.rb +889 -0
  53. data/lib/support_ops_zendesk/zendesk/via_types.rb +137 -0
  54. data/lib/support_ops_zendesk/zendesk/views.rb +636 -0
  55. data/lib/support_ops_zendesk/zendesk/webhooks.rb +206 -0
  56. data/lib/support_ops_zendesk/zendesk.rb +66 -0
  57. data/lib/support_ops_zendesk.rb +29 -0
  58. metadata +274 -0
@@ -0,0 +1,407 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Defines the module SupportOps.
4
+ module SupportOps
5
+ # Defines the module Zendesk
6
+ module Zendesk
7
+ ##
8
+ # Defines the class Macros within the module {SupportOps::Zendesk}.
9
+ #
10
+ # @author Jason Colyer
11
+ # @since 1.0.0
12
+ # @attr [Array] actions Each action describes what the macro will do
13
+ # @attr [Boolean] active Useful for determining if the macro should be displayed
14
+ # @attr [String] created_at The time the macro was created
15
+ # @attr [Boolean] default If true, the macro is a default macro
16
+ # @attr [String] description The description of the macro
17
+ # @attr [Integer] id The ID automatically assigned when a macro is created
18
+ # @attr [Integer] position The position of the macro
19
+ # @attr [Hash] restriction Access to this macro, a null value allows unrestricted access for all users in the account
20
+ # @attr [String] title The title of the macro
21
+ # @attr [String] updated_at The time of the last update of the macro
22
+ # @todo Search Macros => https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#search-macros
23
+ # @todo List Macro Categories => https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#list-macro-categories
24
+ # @todo List Supported Actions for Macros => https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#list-supported-actions-for-macros
25
+ # @todo List Macro Action Definitions => https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#list-macro-action-definitions
26
+ # @todo Show Macro Replica => https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#show-macro-replica
27
+ # @todo List Macro Attachments => https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#list-macro-attachments
28
+ # @todo Show Macro Attachment => https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#show-macro-attachment
29
+ # @todo Create Macro Attachment => https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#create-macro-attachment
30
+ # @todo Create Unassociated Macro Attachment => https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#create-unassociated-macro-attachment
31
+ # @todo Show Changes to Ticket => https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#show-changes-to-ticket
32
+ # @todo Show Ticket After Changes => https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#show-ticket-after-changes
33
+ class Macros < SupportOps::Zendesk::Base
34
+ # @!parse
35
+ # # Creates/updates a macro
36
+ # #
37
+ # # @author Jason Colyer
38
+ # # @since 1.0.0
39
+ # # @return [Object] Instance of {SupportOps::Zendesk::Automations}
40
+ # # @note This is inherited from {SupportOps::Zendesk::Base#save!}
41
+ # # @see
42
+ # # https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#create-macro
43
+ # # Zendesk API > Macros > Create Macro
44
+ # # @see
45
+ # # https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#update-macro
46
+ # # Zendesk API > Macros > Update Macro
47
+ # # @example
48
+ # # require 'support_ops_zendesk'
49
+ # #
50
+ # # SupportOps::Zendesk::Configuration.configure do |config|
51
+ # # config.url = 'https://gitlab.zendesk.com/api/v2'
52
+ # # config.username = 'jason@example.com'
53
+ # # config.token = 'abc123'
54
+ # # end
55
+ # #
56
+ # # new_macro = SupportOps::Zendesk::Macros.new
57
+ # # new_macro.title = 'General::Add replicating tag'
58
+ # # new_macro.actions = [
59
+ # # {
60
+ # # field: 'current_tags',
61
+ # # value: 'replicating'
62
+ # # }
63
+ # # ]
64
+ # #
65
+ # # new_macro.save!
66
+ # #
67
+ # # pp new_macro.id
68
+ # # # => 23409462
69
+ # # @example
70
+ # # require 'support_ops_zendesk'
71
+ # #
72
+ # # SupportOps::Zendesk::Configuration.configure do |config|
73
+ # # config.url = 'https://gitlab.zendesk.com/api/v2'
74
+ # # config.username = 'jason@example.com'
75
+ # # config.token = 'abc123'
76
+ # # end
77
+ # #
78
+ # # existing_macro = SupportOps::Zendesk::Macros.get!(23409462)
79
+ # # existing_macro.title = 'Support::Add replicating tag'
80
+ # #
81
+ # # existing_macro.save!
82
+ # #
83
+ # # pp existing_macro.title
84
+ # # # => "Support::Add replicating tag"
85
+ # def save!; end
86
+ # @!parse
87
+ # # Deletes a macro
88
+ # #
89
+ # # @author Jason Colyer
90
+ # # @since 1.0.0
91
+ # # @return [Boolean]
92
+ # # @note This is inherited from {SupportOps::Zendesk::Base#delete!}
93
+ # # @see
94
+ # # https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#delete-macro
95
+ # # Zendesk API > Macros > Delete Macro
96
+ # # @example
97
+ # # require 'support_ops_zendesk'
98
+ # #
99
+ # # SupportOps::Zendesk::Configuration.configure do |config|
100
+ # # config.url = 'https://gitlab.zendesk.com/api/v2'
101
+ # # config.username = 'jason@example.com'
102
+ # # config.token = 'abc123'
103
+ # # end
104
+ # #
105
+ # # existing_macro = SupportOps::Zendesk::Macros.get!(23409462)
106
+ # # existing_macro.delete!
107
+ # def delete!; end
108
+ define_attributes :actions, :active, :created_at, :default, :description,
109
+ :id, :position, :restriction, :title, :updated_at
110
+ readonly_attributes :created_at, :default, :id, :updated_at
111
+
112
+ ##
113
+ # Lists macros in the Zendesk system
114
+ #
115
+ # @author Jason Colyer
116
+ # @since 1.0.0
117
+ # @overload list(key: value)
118
+ # @param access [String optional] Filter macros by access, possible
119
+ # values are "personal", "agents", "shared", or "account" (the
120
+ # "agents" value returns all personal macros for the account's agents
121
+ # and is only available to admins)
122
+ # @param active [Boolean optional] Filter by active macros if true or
123
+ # inactive macros if false
124
+ # @param category [Integer optional] Filter macros by category
125
+ # @param group_id [Integer optiona] Filter macros by group
126
+ # @param only_viewable [Boolean optional] If true, returns only macros
127
+ # that can be applied to tickets; If false, returns all macros the
128
+ # current user can manage
129
+ # @param sort_by [String optional] Possible values are "alphabetical",
130
+ # "created_at", "updated_at", "usage_1h", "usage_24h", "usage_7d", or
131
+ # "usage_30d" (defaults to alphabetical)
132
+ # @param sort_order [String optional] One of "asc" or "desc" (defaults
133
+ # to "asc" for alphabetical and position sort, "desc" for all others)
134
+ # @param limit [Integer optional] The limit to the number of macros
135
+ # returned. Default to 0 (i.e. no limit)
136
+ # @return [Array]
137
+ # @see
138
+ # https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#list-macros
139
+ # Zendesk API > Macros > List Macros
140
+ # @see SupportOps::Zendesk::Configuration Setting up a client
141
+ # @example
142
+ # require 'support_ops_zendesk'
143
+ #
144
+ # SupportOps::Zendesk::Configuration.configure do |config|
145
+ # config.url = 'https://gitlab.zendesk.com/api/v2'
146
+ # config.username = 'jason@example.com'
147
+ # config.token = 'abc123'
148
+ # end
149
+ #
150
+ # macros = SupportOps::Zendesk::Macros.list(limit: 10)
151
+ # pp macros.count
152
+ # # => 10
153
+ # pp macros.last.title
154
+ # # => "I'm the 10th macro"
155
+ def self.list(**args)
156
+ args[:limit] = 0 unless args[:limit]
157
+ sort_order = (args[:sort_order] == 'desc' ? '-' : '')
158
+ array = []
159
+ opts = 'page[size]=100'
160
+ opts += "&access=#{args[:access]}" if args[:access]
161
+ opts += "&active=#{args[:active]}" if args[:active]
162
+ opts += "&category=#{args[:category]}" if args[:category]
163
+ opts += "&group_id=#{args[:group_id]}" if args[:group_id]
164
+ opts += "&only_viewable=#{args[:only_viewable]}" if args[:only_viewable]
165
+ opts += "&sort=#{sort_order}#{args[:sort_by]}" if args[:sort_by]
166
+ loop do
167
+ response = client.connection.get("macros?#{opts}")
168
+ body = Oj.load(response.body)
169
+ array += body['macros'].map { |m| Macros.new(m) }
170
+ break if args[:limit].to_i.positive? && array.count >= args[:limit].to_i
171
+ break unless body['meta']['has_more']
172
+
173
+ opts = body['links']['next'].split('?').last
174
+ end
175
+ return array if args[:limit].to_i.zero?
176
+
177
+ array.first(args[:limit].to_i)
178
+ end
179
+
180
+ ##
181
+ # Lists macros in the Zendesk system
182
+ #
183
+ # @author Jason Colyer
184
+ # @since 1.0.0
185
+ # @overload list_active(key: value)
186
+ # @param access [String optional] Filter macros by access, possible
187
+ # values are "personal", "agents", "shared", or "account" (the
188
+ # "agents" value returns all personal macros for the account's agents
189
+ # and is only available to admins)
190
+ # @param category [Integer optional] Filter macros by category
191
+ # @param group_id [Integer optiona] Filter macros by group
192
+ # @param sort_by [String optional] Possible values are "alphabetical",
193
+ # "created_at", "updated_at", "usage_1h", "usage_24h", "usage_7d", or
194
+ # "usage_30d" (defaults to alphabetical)
195
+ # @param sort_order [String optional] One of "asc" or "desc" (defaults
196
+ # to "asc" for alphabetical and position sort, "desc" for all others)
197
+ # @param limit [Integer optional] The limit to the number of macros
198
+ # returned. Default to 0 (i.e. no limit)
199
+ # @return [Array]
200
+ # @see
201
+ # https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#list-macros
202
+ # Zendesk API > Macros > List Macros
203
+ # @see SupportOps::Zendesk::Configuration Setting up a client
204
+ # @example
205
+ # require 'support_ops_zendesk'
206
+ #
207
+ # SupportOps::Zendesk::Configuration.configure do |config|
208
+ # config.url = 'https://gitlab.zendesk.com/api/v2'
209
+ # config.username = 'jason@example.com'
210
+ # config.token = 'abc123'
211
+ # end
212
+ #
213
+ # macros = SupportOps::Zendesk::Macros.list_active(limit: 10)
214
+ # pp macros.count
215
+ # # => 10
216
+ # pp macros.last.title
217
+ # # => "I'm the 10th macro"
218
+ def self.list_active(**args)
219
+ args[:limit] = 0 unless args[:limit]
220
+ sort_order = (args[:sort_order] == 'desc' ? '-' : '')
221
+ array = []
222
+ opts = 'page[size]=100'
223
+ opts += "&access=#{args[:access]}" if args[:access]
224
+ opts += "&category=#{args[:category]}" if args[:category]
225
+ opts += "&group_id=#{args[:group_id]}" if args[:group_id]
226
+ opts += "&sort=#{sort_order}#{args[:sort_by]}" if args[:sort_by]
227
+ loop do
228
+ response = client.connection.get("macros/active?#{opts}")
229
+ body = Oj.load(response.body)
230
+ array += body['macros'].map { |m| Macros.new(m) }
231
+ break if args[:limit].to_i.positive? && array.count >= args[:limit].to_i
232
+ break unless body['meta']['has_more']
233
+
234
+ opts = body['links']['next'].split('?').last
235
+ end
236
+ return array if args[:limit].to_i.zero?
237
+
238
+ array.first(args[:limit].to_i)
239
+ end
240
+
241
+ ##
242
+ # Locates a specific macro in the Zendesk system
243
+ #
244
+ # @author Jason Colyer
245
+ # @since 1.0.0
246
+ # @see
247
+ # https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#show-macro
248
+ # Zendesk API > Macros > Show Macro
249
+ # @see SupportOps::Zendesk::Configuration Setting up a client
250
+ # @example
251
+ # require 'support_ops_zendesk'
252
+ #
253
+ # SupportOps::Zendesk::Configuration.configure do |config|
254
+ # config.url = 'https://gitlab.zendesk.com/api/v2'
255
+ # config.username = 'jason@example.com'
256
+ # config.token = 'abc123'
257
+ # end
258
+ #
259
+ # macro = SupportOps::Zendesk::Macros.get(25)
260
+ # pp macro.title
261
+ # # => "I'm the 25th macro"
262
+ def self.get(object)
263
+ if object.is_a? Macros
264
+ Macros.new(id: id).find
265
+ else
266
+ Macros.new(id: object).find
267
+ end
268
+ end
269
+
270
+ ##
271
+ # Locates a specific macro in the Zendesk system
272
+ #
273
+ # @author Jason Colyer
274
+ # @since 1.0.0
275
+ # @see
276
+ # https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#show-macro
277
+ # Zendesk API > Macros > Show Macro
278
+ # @see SupportOps::Zendesk::Configuration Setting up a client
279
+ # @example
280
+ # require 'support_ops_zendesk'
281
+ #
282
+ # SupportOps::Zendesk::Configuration.configure do |config|
283
+ # config.url = 'https://gitlab.zendesk.com/api/v2'
284
+ # config.username = 'jason@example.com'
285
+ # config.token = 'abc123'
286
+ # end
287
+ #
288
+ # macro = SupportOps::Zendesk::Macros.get!(25)
289
+ # pp macro.title
290
+ # # => "I'm the 25th macro"
291
+ def self.get!(object)
292
+ if object.is_a? Macros
293
+ Macros.new(id: id).find!
294
+ else
295
+ Macros.new(id: object).find!
296
+ end
297
+ end
298
+
299
+ ##
300
+ # Updates multiple macros via a batch job
301
+ #
302
+ # @author Jason Colyer
303
+ # @since 1.0.0
304
+ # @param macros [Array] An array of {SupportOps::Zendesk::Macros} instances
305
+ # @return [object] A {SupportOps::Zendesk::JobStatuses} instance
306
+ # @see
307
+ # https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#update-many-macros
308
+ # Zendesk API > Macros > Update Many Macros
309
+ # @example
310
+ # require 'support_ops_zendesk'
311
+ #
312
+ # SupportOps::Zendesk::Configuration.configure do |config|
313
+ # config.url = 'https://gitlab.zendesk.com/api/v2'
314
+ # config.username = 'jason@example.com'
315
+ # config.token = 'abc123'
316
+ # end
317
+ #
318
+ # macro1 = SupportOps::Zendesk::Macros.get!(123)
319
+ # macro1.active = false
320
+ # macro2 = SupportOps::Zendesk::Macros.get!(456)
321
+ # macro2.active = true
322
+ #
323
+ # updates = SupportOps::Zendesk::Macros.update_many!([macro1, macro2])
324
+ # pp updates.id
325
+ # # => "82de0b044094f0c67893ac9fe64f1a99"
326
+ def self.update_many!(macros)
327
+ raise 'The parameter must be an Array' unless macros.is_a? Array
328
+ raise 'You can only update a maximum of 100 macros at a time' if macros.count > 100
329
+ raise 'You can only use SupportOps::Zendesk::Macros instances in the Array' unless macros.reject { |o| o.is_a? SupportOps::Zendesk::Macros }.count.zero?
330
+
331
+ data = { macros: macros.map { |a| to_hash(a).compact } }.to_json
332
+ response = client.connection.put('macros/update_many', data)
333
+ raise "Failed to update macros => #{body['details']}" if response.status != 200
334
+ JobStatuses.new(Oj.load(response.body)['job_status'])
335
+ end
336
+
337
+ ##
338
+ # Deletes multiple macros via a batch job
339
+ #
340
+ # @author Jason Colyer
341
+ # @since 1.0.0
342
+ # @param macro_ids [Array] An array of macro IDs
343
+ # @return [object] A {SupportOps::Zendesk::JobStatuses} instance
344
+ # @see
345
+ # https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#bulk-delete-macros
346
+ # Zendesk API > Macros > Bulk Delete Macros
347
+ # @example
348
+ # require 'support_ops_zendesk'
349
+ #
350
+ # SupportOps::Zendesk::Configuration.configure do |config|
351
+ # config.url = 'https://gitlab.zendesk.com/api/v2'
352
+ # config.username = 'jason@example.com'
353
+ # config.token = 'abc123'
354
+ # end
355
+ #
356
+ # delete = SupportOps::Zendesk::Macros.delete_many!([123, 456])
357
+ # pp delete.id
358
+ # # => "82de0b044094f0c67893ac9fe64f1a99"
359
+ def self.delete_many!(macro_ids)
360
+ raise 'The parameter must be an Array' unless macro_ids.is_a? Array
361
+ raise 'You can only delete a maximum of 100 macros at a time' if macro_ids.count > 100
362
+
363
+ response = client.connection.delete("macros/destroy_many?ids=#{macro_ids.join(',')}")
364
+ raise "Failed to delete macros => #{body['details']}" if response.status != 200
365
+ JobStatuses.new(Oj.load(response.body)['job_status'])
366
+ end
367
+
368
+ private
369
+
370
+ ##
371
+ # @private
372
+ def get_record
373
+ response = self.client.connection.get("macros/#{self.id}")
374
+ return nil if response.status != 200
375
+
376
+ Oj.load(response.body)['macro']
377
+ end
378
+
379
+ ##
380
+ # @private
381
+ def create_record
382
+ response = self.client.connection.post("macros", { macro: attributes_for_save }.to_json)
383
+ body = Oj.load(response.body)
384
+ raise "Failed to create macro => #{body['details']}" if response.status != 201
385
+ body['macro']
386
+ end
387
+
388
+ ##
389
+ # @private
390
+ def update_record
391
+ raise "Failed to update macro => You didn't change anything in the object" if attributes_for_save.keys == [:id]
392
+ response = self.client.connection.put("macros/#{self.id}", { macro: attributes_for_save }.to_json)
393
+ body = Oj.load(response.body)
394
+ raise "Failed to update macro #{self.id} => #{body['details']}" if response.status != 200
395
+ body['macro']
396
+ end
397
+
398
+ ##
399
+ # @private
400
+ def delete_record
401
+ response = self.client.connection.delete("macros/#{self.id}")
402
+ raise "Failed to delete macro => #{body['details']}" if response.status != 204
403
+ true
404
+ end
405
+ end
406
+ end
407
+ end
@@ -0,0 +1,186 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Defines the module SupportOps.
4
+ module SupportOps
5
+ # Defines the module Zendesk
6
+ module Zendesk
7
+ ##
8
+ # Defines the class OAuthClients within the module {SupportOps::Zendesk}.
9
+ #
10
+ # @author Jason Colyer
11
+ # @since 1.0.0
12
+ # @attr [String] company The company name displayed when users are asked to grant access to your application.
13
+ # @attr [String] created_at The time the client was created
14
+ # @attr [String] description A short description of your client that is displayed to users when they are considering approving access to your application
15
+ # @attr [Boolean] global Whether this client is globally accessible.
16
+ # @attr [Integer] id Automatically assigned upon creation
17
+ # @attr [String] identifier The unique identifier for this client
18
+ # @attr [String] kind Either "public" or "confidential". Specifies whether the OAuth client operates in a public environment where credentials cannot be securely stored, or on secure servers that can safely store credentials.
19
+ # @attr [String] logo_url The API logo url of this record
20
+ # @attr [String] name The name of this client
21
+ # @attr [Array] redirect_url An array of the valid redirect URIs for this client
22
+ # @attr [String] secret The client secret. Generated automatically on creation and returned in full only at that time
23
+ # @attr [String] updated_at The time of the last update of the client
24
+ # @attr [Integer] user_id The id of the admin who created the client
25
+ # @todo Create Client => https://developer.zendesk.com/api-reference/ticketing/oauth/oauth_clients/#create-client
26
+ # @todo Update Client => https://developer.zendesk.com/api-reference/ticketing/oauth/oauth_clients/#update-client
27
+ # @todo Delete Client => https://developer.zendesk.com/api-reference/ticketing/oauth/oauth_clients/#delete-client
28
+ # @todo Generate Secret => https://developer.zendesk.com/api-reference/ticketing/oauth/oauth_clients/#generate-secret
29
+ class OAuthClients < SupportOps::Zendesk::Base
30
+ define_attributes :company, :created_at, :description, :global, :id,
31
+ :identifier, :kind, :logo_url, :name, :redirect_url,
32
+ :secret, :updated_at, :user_id
33
+ readonly_attributes :created_at, :global, :id, :logo_url, :secret,
34
+ :updated_at
35
+
36
+ ##
37
+ # Lists OAuth clients in the Zendesk system
38
+ #
39
+ # @author Jason Colyer
40
+ # @since 1.0.0
41
+ # @overload list(key: value)
42
+ # @param limit [Integer optional] The limit to the number of
43
+ # oauth clients returned. Default to 0 (i.e. no limit)
44
+ # @return [Array]
45
+ # @see
46
+ # https://developer.zendesk.com/api-reference/ticketing/oauth/oauth_clients/#list-clients
47
+ # Zendesk API > OAuth Clients > List Clients
48
+ # @see SupportOps::Zendesk::Configuration Setting up a client
49
+ # @example
50
+ # require 'support_ops_zendesk'
51
+ #
52
+ # SupportOps::Zendesk::Configuration.configure do |config|
53
+ # config.url = 'https://gitlab.zendesk.com/api/v2'
54
+ # config.username = 'jason@example.com'
55
+ # config.token = 'abc123'
56
+ # end
57
+ #
58
+ # oauth_clients = SupportOps::Zendesk::OAuthClients.list(limit: 25)
59
+ # pp oauth_clients.count
60
+ # # => 25
61
+ # pp oauth_clients.last.name
62
+ # # => "25th OAuth Client"
63
+ def self.list(**args)
64
+ args[:limit] = 0 unless args[:limit]
65
+ array = []
66
+ opts = "page[size]=100&sort=#{args[:sort]}"
67
+ loop do
68
+ response = client.connection.get("oauth/clients?#{opts}")
69
+ body = Oj.load(response.body)
70
+ array += body['clients'].map { |c| OAuthClients.new(c) }
71
+ break if args[:limit].to_i.positive? && array.count >= args[:limit].to_i
72
+ break unless body['meta']['has_more']
73
+
74
+ opts = body['links']['next'].split('?').last
75
+ end
76
+ return array if args[:limit].to_i.zero?
77
+
78
+ array.first(args[:limit].to_i)
79
+ end
80
+
81
+ ##
82
+ # Locates a OAuth client within Zendesk by name. Can utilize a cache for quicker results
83
+ #
84
+ # @author Jason Colyer
85
+ # @since 1.0.0
86
+ # @overload find_by_name(key: value)
87
+ # @param name [String required] The name to search for
88
+ # @param cache [Array] An Array of SupportOps::Zendesk::OAuthClients to use as a cache
89
+ # @return [Object] An instance of {SupportOps::Zendesk::OAuthClients}
90
+ # @example
91
+ # require 'support_ops_zendesk'
92
+ #
93
+ # SupportOps::Zendesk::Configuration.configure do |config|
94
+ # config.url = 'https://gitlab.zendesk.com/api/v2'
95
+ # config.username = 'jason@example.com'
96
+ # config.token = 'abc123'
97
+ # end
98
+ #
99
+ # oauth_clients = SupportOps::Zendesk::OAuthClients.list
100
+ # oauth_client = SupportOps::Zendesk::OAuthClients.find_by_name(name: '25th OAuth Client', cache: oauth_clients)
101
+ # pp oauth_client.name
102
+ # # => "25th OAuth Client"
103
+ # pp oauth_client.id
104
+ # # => 125
105
+ def self.find_by_name(**args)
106
+ raise 'You have to provide a name' unless args[:name]
107
+ if args[:cache]
108
+ raise 'Provided cache is not an Array' unless args[:cache].is_a? Array
109
+ oauth_clients = args[:cache]
110
+ else
111
+ oauth_clients = list
112
+ end
113
+ oauth_clients.detect { |c| c.name.downcase == args[:name].to_s.downcase }
114
+ end
115
+
116
+ ##
117
+ # Locates a specific OAuth client in the Zendesk system
118
+ #
119
+ # @author Jason Colyer
120
+ # @since 1.0.0
121
+ # @see
122
+ # https://developer.zendesk.com/api-reference/ticketing/oauth/oauth_clients/#show-client
123
+ # Zendesk API > OAuth Clients > Show Client
124
+ # @see SupportOps::Zendesk::Configuration Setting up a client
125
+ # @example
126
+ # require 'support_ops_zendesk'
127
+ #
128
+ # SupportOps::Zendesk::Configuration.configure do |config|
129
+ # config.url = 'https://gitlab.zendesk.com/api/v2'
130
+ # config.username = 'jason@example.com'
131
+ # config.token = 'abc123'
132
+ # end
133
+ #
134
+ # oauth_client = SupportOps::Zendesk::OAuthClients.get(125)
135
+ # pp oauth_client.name
136
+ # # => "25th OAuth Client"
137
+ def self.get(object)
138
+ if object.is_a? OAuthClients
139
+ OAuthClients.new(id: id).find
140
+ else
141
+ OAuthClients.new(id: object).find
142
+ end
143
+ end
144
+
145
+ ##
146
+ # Locates a specific OAuth client in the Zendesk system
147
+ #
148
+ # @author Jason Colyer
149
+ # @since 1.0.0
150
+ # @see
151
+ # https://developer.zendesk.com/api-reference/ticketing/oauth/oauth_clients/#show-client
152
+ # Zendesk API > OAuth Clients > Show Client
153
+ # @see SupportOps::Zendesk::Configuration Setting up a client
154
+ # @example
155
+ # require 'support_ops_zendesk'
156
+ #
157
+ # SupportOps::Zendesk::Configuration.configure do |config|
158
+ # config.url = 'https://gitlab.zendesk.com/api/v2'
159
+ # config.username = 'jason@example.com'
160
+ # config.token = 'abc123'
161
+ # end
162
+ #
163
+ # oauth_client = SupportOps::Zendesk::OAuthClients.get!(125)
164
+ # pp oauth_client.name
165
+ # # => "25th OAuth Client"
166
+ def self.get!(object)
167
+ if object.is_a? OAuthClients
168
+ OAuthClients.new(id: id).find!
169
+ else
170
+ OAuthClients.new(id: object).find!
171
+ end
172
+ end
173
+
174
+ private
175
+
176
+ ##
177
+ # @private
178
+ def get_record
179
+ response = self.client.connection.get("oauth/clients/#{self.id}")
180
+ return nil if response.status != 200
181
+
182
+ Oj.load(response.body)['client']
183
+ end
184
+ end
185
+ end
186
+ end