zendesk_support_api 0.5.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.
@@ -0,0 +1,335 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZendeskSupportAPI
4
+ # Tickets class - https://developer.zendesk.com/rest_api/docs/support/tickets
5
+ class Tickets
6
+ # Determines if given string is a valid sort function
7
+ #
8
+ # @param sort [String] The sort string to use
9
+ # @return [Boolean]
10
+ #
11
+ # @example
12
+ # ZendeskSupportAPI::Tickets.sort_valid? 'assignee' #=> true
13
+ # ZendeskSupportAPI::Tickets.sort_valid? 'blah' #=> false
14
+
15
+ def self.sort_valid?(sort)
16
+ valid = %w[assignee assignee_name created_at group id locale requester
17
+ requester.name status subject updated_at]
18
+ valid.include?(sort)
19
+ end
20
+
21
+ # Prints out the include string
22
+ #
23
+ # @return [String]
24
+ #
25
+ # @example
26
+ # ZendeskSupportAPI::Tickets.includes
27
+ # #=> '?include=users,organizations,ticket_forms,comment_count'
28
+
29
+ def self.includes
30
+ '?include=users,organizations,ticket_forms,comment_count'
31
+ end
32
+
33
+ # Prints out the sort_by and order_by string for the url
34
+ #
35
+ # @param sort [String] The sort string to use
36
+ # @param order [String] The order string to use
37
+ # @return [String]
38
+ #
39
+ # @example
40
+ # ZendeskSupportAPI::Tickets.sort_order('id', 'desc')
41
+ # #=> '&sort_by=id&order_by=desc'
42
+
43
+ def self.sort_order(sort, order)
44
+ "&sort_by=#{sort}&order_by=#{order}"
45
+ end
46
+
47
+ # Maps users into user_objects
48
+ #
49
+ # @param tickets [Array] The Array of tickets to map
50
+ # @param res [Hash] The has containing the response from a request
51
+ # @return [Hash]
52
+
53
+ def self.ticket_map(tickets, res)
54
+ tickets.map { |t| ticket_object(t, res) }
55
+ end
56
+
57
+ # Returns the string of the next_page for pagination
58
+ #
59
+ # @param res [Hash] The Hash containing the response from a request
60
+ # @return [nil|String]
61
+
62
+ def self.next_page(res)
63
+ (res['next_page'].nil? ? nil : res['next_page'].split('/').last)
64
+ end
65
+
66
+ # Prints out organization_id
67
+ #
68
+ # @return [String]
69
+ #
70
+ # @example
71
+ # ZendeskSupportAPI::Tickets.org_id #=> 'organization_id'
72
+
73
+ def self.org_id
74
+ 'organization_id'
75
+ end
76
+
77
+ # Creates a ticket object
78
+ #
79
+ # @param ticket [Hash] The ticket Hash to use
80
+ # @param res [Array] The response to use for mapping
81
+ # @return [Hash]
82
+
83
+ def self.ticket_object(ticket, res)
84
+ ticket.merge(
85
+ organization: select_obj(res['organizations'], ticket[org_id]),
86
+ form: select_obj(res['ticket_forms'], ticket['form_id']),
87
+ requester: select_obj(res['users'], ticket['requester_id']),
88
+ assignee: select_obj(res['users'], ticket['assignee_id'])
89
+ )
90
+ end
91
+
92
+ # Selects an object from an array based on a user ID
93
+ #
94
+ # @param array [Array] An array of to look in
95
+ # @param id [Integer] The ID to use
96
+ # @return [Hash]
97
+
98
+ def self.select_obj(array, id)
99
+ array.select { |a| a['id'] == id }.first
100
+ end
101
+
102
+ # List out tickets (first 100)
103
+ #
104
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
105
+ # @param sort [String] The sort method to use
106
+ # @param order [String] The order method to use
107
+ # @return [Array]
108
+ #
109
+ # @example
110
+ # ZendeskSupportAPI::Tickets.list(client)
111
+ # #=> [
112
+ # #=> {
113
+ # #=> "id": 35436,
114
+ # #=> "subject": "Help I need somebody!",
115
+ # #=> ...
116
+ # #=> },
117
+ # #=> {
118
+ # #=> "id": 20057623,
119
+ # #=> "subject": "Not just anybody!",
120
+ # #=> ...
121
+ # #=> },
122
+ # #=> ...
123
+ # #=> ]
124
+
125
+ def self.list(client, sort = 'id', order = 'asc')
126
+ return "Invalid sort '#{sort}'" unless sort_valid?(sort)
127
+ return "Invalid order '#{order}'" unless %w[asc desc].include?(order)
128
+
129
+ url = "tickets.json#{includes}#{sort_order(sort, order)}"
130
+ res = client.request(:get, url)
131
+ res['tickets'].map { |t| ticket_object(t, res) }
132
+ end
133
+
134
+ # Lists out all tickets (paginates over every page)
135
+ #
136
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
137
+ # @return [Array]
138
+ #
139
+ # @example
140
+ # ZendeskSupportAPI::Tickets.all(client)
141
+ # #=> Grabbing tickets (total: 215336)... / ...done
142
+ # #=> [{ticket1},{ticket2}...{ticket215336}]
143
+
144
+ def self.all(client)
145
+ tickets = []
146
+ page = "tickets.json#{includes}&page=1"
147
+ until page.nil?
148
+ res = client.request(:get, page)
149
+ client.spinner("tickets (total: #{res['count']})", page.split('=').last)
150
+ tickets += ticket_map(res['tickets'], res)
151
+ page = next_page(res)
152
+ end
153
+ puts ' ...done'
154
+ tickets
155
+ end
156
+
157
+ # Shows details about a specific ticket
158
+ #
159
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
160
+ # @param tid [Integer] The Ticket ID to use
161
+ # @return [Hash]
162
+ #
163
+ # @example
164
+ # ZendeskSupportAPI::Tickets.show(client, 35436)
165
+ # #=> {
166
+ # #=> "id": 35436,
167
+ # #=> "subject": "My printer is on fire!",
168
+ # #=> ...
169
+ # #=> }
170
+
171
+ def self.show(client, tid)
172
+ url = "tickets/#{tid}.json#{includes}"
173
+ res = client.request(:get, url)
174
+ res['ticket'].map { |t| ticket_object(t, res) }
175
+ end
176
+
177
+ # Show details about many tickets
178
+ #
179
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
180
+ # @param tids [Array] An array of ticket IDs
181
+ # @return [Array]
182
+
183
+ def self.show_many(client, tids)
184
+ url = "tickets/show_many.json#{includes}&ids=#{tids.join(',')}"
185
+ res = client.request(:get, url)
186
+ res['tickets'].map { |t| ticket_object(t, res) }
187
+ end
188
+
189
+ # Create a ticket
190
+ #
191
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
192
+ # @param ticket [Hash] The ticket hash to use for creation
193
+ # @return [Hash|String]
194
+ #
195
+ # @example
196
+ # ticket = {
197
+ # subject: "This is a test ticket",
198
+ # followers: [
199
+ # { user_email: 'albert@example.com', action: 'put' },
200
+ # { user_id: 123, action: 'put' }
201
+ # ],
202
+ # comment: {
203
+ # body: "This goes into the test ticket body!"
204
+ # },
205
+ # tags: ["free_user", "ignore"]
206
+ # }
207
+ # ZendeskSupportAPI::Tickets.create(client, ticket)
208
+ # #=> {
209
+ # #=> "id": 1,
210
+ # #=> "subject": "This is a test ticket",
211
+ # #=> ...
212
+ # #=> }
213
+
214
+ def self.create(client, ticket)
215
+ res = client.request(:post, 'tickets.json', ticket: ticket)
216
+ return "Creation failed: #{res['details']}" if res['error']
217
+
218
+ res
219
+ end
220
+
221
+ # Creates many tickets
222
+ #
223
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
224
+ # @param tickets [Array] The array of ticket hashes to use for creation
225
+ # @return [ZendeskSupportAPI::Client.handle_job]
226
+
227
+ def self.create_many(client, tickets)
228
+ res = client.request(:post, 'tickets/create_many.json', tickets: tickets)
229
+ client.handle_job(res)
230
+ end
231
+
232
+ # Updates a ticket
233
+ #
234
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
235
+ # @param tid [Integer] The ticket ID to update
236
+ # @param ticket [Hash] The ticket hash to use
237
+ # @return [Hash|String]
238
+
239
+ def self.update(client, tid, ticket)
240
+ res = client.request(:put, "tickets/#{tid}.json", ticket: ticket)
241
+ return "Update of #{tid} failed: #{res['error']}" if res['error']
242
+
243
+ res
244
+ end
245
+
246
+ # Updates many tickets
247
+ #
248
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
249
+ # @param tickets [Array] The array of ticket hashes to use for updates
250
+ # @return [ZendeskSupportAPI::Client.handle_job]
251
+ #
252
+ # @example
253
+ # tickets = [
254
+ # {
255
+ # id: 123,
256
+ # tags: ['ultimate', 'urgent']
257
+ # },
258
+ # {
259
+ # id: 124,
260
+ # followers: [
261
+ # { user_id: 123, action: 'delete' }
262
+ # ]
263
+ # },
264
+ # {
265
+ # id: 125,
266
+ # subject: 'Staging Instance having issues'
267
+ # }
268
+ # ]
269
+ # ZendeskSupportAPI::Tickets.update_many(client, tickets)
270
+
271
+ def self.update_many(client, tickets)
272
+ res = client.request(:put, 'tickets/update_many.json', tickets: tickets)
273
+ client.handle_job(res)
274
+ end
275
+
276
+ # Mark a ticket as spam and suspend the user
277
+ #
278
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
279
+ # @param tid [Integer] The ticket ID to use
280
+ # @return [String]
281
+
282
+ def self.mark_as_spam(client, tid)
283
+ res = client.request(:put, "tickets/#{tid}/mark_as_spam.json")
284
+ return "Unable to mark ticket as spam: #{res['error']}" if res['error']
285
+
286
+ "Ticket #{tid} marked as spam"
287
+ end
288
+
289
+ # Mark many tickets as spam and suspend their users
290
+ #
291
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
292
+ # @param tids [Array] The ticket IDs to use
293
+ # @return [ZendeskSupportAPI::Client.handle_job]
294
+
295
+ def self.mark_many_as_spam(client, tids)
296
+ url = "tickets/mark_many_as_spam.json?ids=#{tids.join(',')}"
297
+ res = client.request(:put, url)
298
+ client.handle_job(res)
299
+ end
300
+
301
+ # Merge Tickets into Target Ticket
302
+ #
303
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
304
+ # @param tid [Integer] The Ticket ID to merge into
305
+ # @param hash [Hash] The Hash to use for the merge (see example)
306
+ # @return [ZendeskSupportAPI::Client.handle_job]
307
+ #
308
+ # @example
309
+ # merge_hash = {
310
+ # ids: [112, 113, 114],
311
+ # source_comment: "Closing in favor of #111",
312
+ # target_comment: "Merging #112, #113, and #114 into this ticket"
313
+ # }
314
+ # ZendeskSupportAPI::Tickets.merge(client, 111, merge_hash)
315
+
316
+ def self.merge(client, tid, hash)
317
+ res = client.request(:post, "tickets/#{tid}/merge.json", hash)
318
+ client.handle_job(res)
319
+ end
320
+
321
+ # List followers of a ticket
322
+ #
323
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
324
+ # @param tid [Integer] The Ticket ID to use
325
+ # @return [Array]
326
+
327
+ def self.list_followers(client, tid)
328
+ res = client.request(:get, "tickets/#{tid}/followers")['users']
329
+ res['users'].each do |user|
330
+ user['organization'] = select_obj(res['organizations'], user[org_id])
331
+ end
332
+ res['users']
333
+ end
334
+ end
335
+ end
@@ -0,0 +1,317 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZendeskSupportAPI
4
+ # Users class - https://developer.zendesk.com/rest_api/docs/support/users
5
+ class Users
6
+ # Function to return a string that side-loads organizations
7
+ #
8
+ # @return [String]
9
+
10
+ def self.included
11
+ 'include=organizations'
12
+ end
13
+
14
+ # Maps users into user_objects
15
+ #
16
+ # @param users [Array] The Array of users to map
17
+ # @param orgs [Array] The Array of orgs to use in mapping
18
+ # @return [Hash]
19
+
20
+ def self.user_map(users, orgs)
21
+ users.map { |u| user_object(u, orgs) }
22
+ end
23
+
24
+ # Returns the string of the next_page for pagination
25
+ #
26
+ # @param res [Hash] The Hash containing the response from a request
27
+ # @return [nil|String]
28
+ #
29
+ # @example
30
+ # ZendeskSupportAPI::Users.next_page(response) #=> nil
31
+ # ZendeskSupportAPI::Users.next_page(response)
32
+ # #=> "users.json?include=organizations&page=56
33
+
34
+ def self.next_page(res)
35
+ (res['next_page'].nil? ? nil : res['next_page'].split('/').last)
36
+ end
37
+
38
+ # Lists out users (first 100)
39
+ #
40
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
41
+ # @return [Hash]
42
+ #
43
+ # @example
44
+ # ZendeskSupportAPI::Users.list(client)
45
+ # #=> {users:[{user1},{user2}...{user100}]}
46
+
47
+ def self.list(client)
48
+ res = client.request(:get, "users.json?#{included}")
49
+ res['users'].map { |u| user_object(u, res['organizations']) }
50
+ res['users']
51
+ end
52
+
53
+ # Lists out all users (paginates over every page)
54
+ #
55
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
56
+ # @return [Hash]
57
+ #
58
+ # @example
59
+ # ZendeskSupportAPI::Users.all(client)
60
+ # #=> Grabbing users (total: 215336)... / ...done
61
+ # #=> {users:[{user1},{user2}...{user201520}]}
62
+
63
+ def self.all(client)
64
+ users = []
65
+ page = "users.json?#{included}&page=1"
66
+ until page.nil?
67
+ res = client.request(:get, page)
68
+ client.spinner("users (total: #{res['count']})", page.split('=').last)
69
+ users += user_map(res['users'], res['organizations'])
70
+ page = next_page(res)
71
+ end
72
+ puts ' ...done'
73
+ users
74
+ end
75
+
76
+ # Grabs a specific user
77
+ #
78
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
79
+ # @param id [Integer] The User's ID to use
80
+ # @return [Hash]
81
+ #
82
+ # @example
83
+ # ZendeskSupportAPI::Users.show(client, 123)
84
+ # #=> {
85
+ # #=> "id"=>123,
86
+ # #=> "url"=>"https://zendesk.com/api/users/123.json",
87
+ # #=> "name"=>"Test User",
88
+ # #=> ...
89
+ # #=> }
90
+
91
+ def self.show(client, id)
92
+ res = client.request(:get, "users/#{id}.json?#{included}")
93
+ user_object(res['user'], res['organizations'])
94
+ end
95
+
96
+ # Shows many users
97
+ #
98
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
99
+ # @param ids [Array] An array of User IDs to use
100
+ # @return [Array]
101
+ #
102
+ # @example
103
+ # ZendeskSupportAPI::Users.show_many(client, [123, 456])
104
+ # #=> [
105
+ # #=> {
106
+ # #=> "id": 123,
107
+ # #=> "name": "Johnny Appleseed",
108
+ # #=> ...
109
+ # #=> },
110
+ # #=> {
111
+ # #=> "id": 456,
112
+ # #=> "name": "Rupert Root",
113
+ # #=> ...
114
+ # #=> }
115
+ # #=> ]
116
+
117
+ def self.show_many(client, ids)
118
+ ids = ids.join(',')
119
+ res = client.request(:get, "users/show_many.json?#{included}&ids=#{ids}")
120
+ res['users'].map { |u| user_object(u, res['organizations']) }
121
+ res['users']
122
+ end
123
+
124
+ # Creates a user hash (for mapping the org into the user Hash)
125
+ #
126
+ # @param user [Hash] The user details to use
127
+ # @param orgs [Array] The Array of orgs to use
128
+ # @return [Hash]
129
+
130
+ def self.user_object(user, orgs)
131
+ oid = 'organization_id'
132
+ user['organization'] = orgs.select { |o| o['id'] == user[oid] }
133
+ user
134
+ end
135
+
136
+ # Creates a user
137
+ #
138
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
139
+ # @param user [Hash] The user to create
140
+ # @return [Hash|String] Either the user details or the error
141
+ #
142
+ # @example
143
+ # user = {name: 'Roger Wilco', email: 'rw@example.com'}
144
+ # ZendeskSupportAPI::Users.create(client, user)
145
+ # #=> {
146
+ # #=> "user": {
147
+ # #=> "id": 9873843,
148
+ # #=> "name": "Roger Wilco",
149
+ # #=> "email": "rw@example.com"
150
+ # #=> ...
151
+ # #=> }
152
+ # #=> }
153
+ # ZendeskSupportAPI::User.create(client, user)
154
+ # #=> Creation failed: => "Creation failed: {\"email\"=>[{\"description\"
155
+ # #=> =>\"Email: rw@example.com is already being used by another user\",
156
+ # #=> \"error\"=>\"DuplicateValue\"}]}"
157
+
158
+ def self.create(client, user)
159
+ res = client.request(:post, 'users.json', user: user)
160
+ return "Creation failed: #{res['details']}" if res['error']
161
+
162
+ res
163
+ end
164
+
165
+ # Creates many users
166
+ #
167
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
168
+ # @param users [Array] The users to create
169
+ # @return [ZendeskSupportAPI::Client.handle_job]
170
+
171
+ def self.create_many(client, users)
172
+ res = client.request(:post, 'users/create_many.json', users: users)
173
+ client.handle_job(res)
174
+ end
175
+
176
+ # Creates or updates a user
177
+ #
178
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
179
+ # @param user [Hash] The user to create/update
180
+ # @return [Hash|String] Either the user details or the error
181
+ #
182
+ # @example
183
+ # ZendeskSupportAPI::User.create_or_update(client, user)
184
+ # #=> {
185
+ # #=> "user": {
186
+ # #=> "id": 9873843,
187
+ # #=> "name": "Roger Wilco",
188
+ # #=> "email": "rw@example.com"
189
+ # #=> ...
190
+ # #=> }
191
+ # #=> }
192
+
193
+ def self.create_or_update(client, user)
194
+ res = client.request(:post, 'users/create_or_update.json', user: user)
195
+ return "Create/Update failed: #{res['description']}" if res['error']
196
+
197
+ res
198
+ end
199
+
200
+ # Creates or updates many users
201
+ #
202
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
203
+ # @param users [Array] The users to create/update
204
+ # @return [ZendeskSupportAPI::Client.handle_job]
205
+
206
+ def self.create_or_update_many(client, users)
207
+ res = client.request(:post,
208
+ 'users/create_or_update_many.json',
209
+ users: users)
210
+ client.handle_job(res)
211
+ end
212
+
213
+ # Updates a user
214
+ #
215
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
216
+ # @param uid [Integer] The User's ID
217
+ # @param user [Hash] The user details to update
218
+ # @return [Hash|String] Either the user details or the error
219
+ #
220
+ # @example
221
+ # ZendeskSupportAPI::User.update(client, 123, user)
222
+ # #=> {user}
223
+
224
+ def self.update(client, uid, user)
225
+ res = client.request(:put, "users/#{uid}.json", user: user)
226
+ return "Update of #{uid} failed: #{res['error']}" if res['error']
227
+
228
+ res
229
+ end
230
+
231
+ # Updates many users
232
+ #
233
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
234
+ # @param users [Array] The users to update
235
+ # @return [ZendeskSupportAPI::Client.handle_job]
236
+
237
+ def self.update_many(client, users)
238
+ res = client.request(:put, 'users/update_many.json', users: users)
239
+ client.handle_job(res)
240
+ end
241
+
242
+ # Deletes a user
243
+ #
244
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
245
+ # @param uid [Integer] The User ID to delete
246
+ # @return [String]
247
+ #
248
+ # @example
249
+ # ZendeskSupportAPI::Users.delete(client, 123)
250
+ # #=> User 123 has been deleted
251
+ # ZendeskSupportAPI::Users.delete(client, 123)
252
+ # #=> "Deletion of 123 failed: RecordNotFound"
253
+
254
+ def self.delete(client, uid)
255
+ res = client.request(:delete, "users/#{uid}.json")
256
+ return "Deletion of #{uid} failed: #{res['error']}" if res['error']
257
+
258
+ "User #{uid} has been deleted"
259
+ end
260
+
261
+ # Deletes many users
262
+ #
263
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
264
+ # @param ids [Array] The array of User IDs to delete
265
+ # @return [ZendeskSupportAPI::Client.handle_job]
266
+
267
+ def self.delete_many(client, ids)
268
+ ids = ids.join(',')
269
+ res = client.request(:delete, "users/destroy_many.json?ids=#{ids}")
270
+ client.handle_job(res)
271
+ end
272
+
273
+ # Suspends a user
274
+ #
275
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
276
+ # @param id [Integer] The User ID to suspend
277
+ # @return [String] Either a success message or an error
278
+ #
279
+ # @example
280
+ # ZendeskSupportAPI::Users.suspend(client, 123)
281
+ # #=> User 123 is suspended
282
+
283
+ def self.suspend(client, id)
284
+ res = client.request(:put, "users/#{id}.json", user: { suspended: true })
285
+ return "Suspension of #{id} failed: #{res['error']}" if res['error']
286
+
287
+ "User #{id} suspended" if res['user']['suspended']
288
+ end
289
+
290
+ # Shows a users groups
291
+ #
292
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
293
+ # @param uid [Integer] The User ID to use
294
+ # @return [Array]
295
+ #
296
+ # @example
297
+ # ZendeskSupportAPI::Users.groups(client, 1234)
298
+ # #=> [
299
+ # #=> {
300
+ # #=> "name": "DJs",
301
+ # #=> "created_at": "2009-05-13T00:07:08Z",
302
+ # #=> "updated_at": "2011-07-22T00:11:12Z",
303
+ # #=> "id": 211
304
+ # #=> },
305
+ # #=> {
306
+ # #=> "name": "MCs",
307
+ # #=> "created_at": "2009-08-26T00:07:08Z",
308
+ # #=> "updated_at": "2010-05-13T00:07:08Z",
309
+ # #=> "id": 122
310
+ # #=> }
311
+ # #=> ]
312
+
313
+ def self.groups(client, uid)
314
+ client.request(:get, "users/#{uid}/groups.json")['groups']
315
+ end
316
+ end
317
+ end