Class: ZendeskSupportAPI::Users

Inherits:
Object
  • Object
show all
Defined in:
lib/zendesk_support_api/users.rb

Overview

Class Method Summary collapse

Class Method Details

.all(client) ⇒ Hash

Lists out all users (paginates over every page)

Examples:

ZendeskSupportAPI::Users.all(client)
#=> Grabbing users... / ...done
#=> {users:[{user1},{user2}...{user201520}]}

Parameters:

  • The client instance to use

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zendesk_support_api/users.rb', line 63

def self.all(client)
  users = []
  page = "users.json?#{included}&page=1"
  until page.nil?
    res = client.request(:get, page)
    client.spinner("users (total: #{res['count']})", page.split('=').last)
    users += user_map(res['users'], res['organizations'])
    page = next_page(res)
  end
  puts ' ...done'
  users
end

.create(client, user) ⇒ Hash|String

Creates a user

Examples:

ZendeskSupportAPI::User.create(client, user)
#=> {user}
ZendeskSupportAPI::User.create(client, user)
#=> Creation failed: => "Creation failed: {\"email\"=>[{\"description\"
#=> =>\"Email: [email protected] is already being used by another user\",
#=> \"error\"=>\"DuplicateValue\"}]}"

Parameters:

  • The client instance to use

  • The user to create

Returns:

  • Either the user details or the error



172
173
174
175
176
177
# File 'lib/zendesk_support_api/users.rb', line 172

def self.create(client, user)
  res = client.request(:post, 'users.json', user: user)
  return "Creation failed: #{res['details']}" if res['error']

  res
end

.create_many(client, users) ⇒ ZendeskSupportAPI::Client.handle_job

Creates many users

Parameters:

  • The client instance to use

  • The users to create

Returns:

  • Either the user details or the error



185
186
187
188
# File 'lib/zendesk_support_api/users.rb', line 185

def self.create_many(client, users)
  res = client.request(:post, 'users/create_many.json', users: users)
  client.handle_job(res)
end

.create_or_update(client, user) ⇒ Hash|String

Creates or updates a user

Examples:

ZendeskSupportAPI::User.create_or_update(client, user)
#=> {user}

Parameters:

  • The client instance to use

  • The user to create/update

Returns:

  • Either the user details or the error



200
201
202
203
204
205
# File 'lib/zendesk_support_api/users.rb', line 200

def self.create_or_update(client, user)
  res = client.request(:post, 'users/create_or_update.json', user: user)
  return "Create/Update failed: #{res['description']}" if res['error']

  res
end

.create_or_update_many(client, users) ⇒ ZendeskSupportAPI::Client.handle_job

Creates or updates many users

Parameters:

  • The client instance to use

  • The users to create/update

Returns:

  • Either the user details or the error



213
214
215
216
217
218
# File 'lib/zendesk_support_api/users.rb', line 213

def self.create_or_update_many(client, users)
  res = client.request(:post,
                       'users/create_or_update_many.json',
                       users: users)
  client.handle_job(res)
end

.delete(client, uid) ⇒ String

Deletes a user

Examples:

ZendeskSupportAPI::Users.delete(client, 123)
#=> User 123 has been deleted
ZendeskSupportAPI::Users.delete(client, 123)
#=> "Deletion of 123 failed: RecordNotFound"

Parameters:

  • The client instance to use

  • The User ID to delete

Returns:



261
262
263
264
265
266
# File 'lib/zendesk_support_api/users.rb', line 261

def self.delete(client, uid)
  res = client.request(:delete, "users/#{uid}.json")
  return "Deletion of #{uid} failed: #{res['error']}" if res['error']

  "User #{uid} has been deleted"
end

.delete_many(client, ids) ⇒ ZendeskSupportAPI::Client.handle_job

Deletes many users

Parameters:

  • The client instance to use

  • The array of User IDs to delete

Returns:

  • Either the user details or the error



274
275
276
277
278
# File 'lib/zendesk_support_api/users.rb', line 274

def self.delete_many(client, ids)
  ids = ids.join(',')
  res = client.request(:delete, "users/destroy_many.json?ids=#{ids}")
  client.handle_job(res)
end

.includedString

Function to return a string that side-loads organizations

Returns:



10
11
12
# File 'lib/zendesk_support_api/users.rb', line 10

def self.included
  'include=organizations'
end

.list(client) ⇒ Hash

Lists out users (first 100)

Examples:

ZendeskSupportAPI::Users.list(client)
#=> {users:[{user1},{user2}...{user100}]}

Parameters:

  • The client instance to use

Returns:



47
48
49
50
51
# File 'lib/zendesk_support_api/users.rb', line 47

def self.list(client)
  res = client.request(:get, "users.json?#{included}")
  res['users'].map { |u| user_object(u, res['organizations']) }
  res['users']
end

.next_page(res) ⇒ nil|String

Returns the string of the next_page for pagination

Examples:

ZendeskSupportAPI::Users.next_page(response) #=> nil
ZendeskSupportAPI::Users.next_page(response)
#=> "users.json?include=organizations&page=56

Parameters:

  • The Hash containing the response from a request

Returns:



34
35
36
# File 'lib/zendesk_support_api/users.rb', line 34

def self.next_page(res)
  (res['next_page'].nil? ? nil : res['next_page'].split('/').last)
end

.show(client, id) ⇒ Hash

Grabs a specific user

Examples:

ZendeskSupportAPI::Users.show(client, 123)
#=> {
#=>   "id"=>123,
#=>   "url"=>"https://zendesk.com/api/users/123.json",
#=>   "name"=>"Test User",
#=>   "email"=>"[email protected]",
#=>   "created_at"=>"2020-04-28T13:45:54Z",
#=>   "updated_at"=>"2020-04-28T13:45:54Z",
#=>   "time_zone"=>"Eastern Time (US & Canada)",
#=>   "iana_time_zone"=>"America/New_York",
#=>   "phone"=>nil,
#=>   "shared_phone_number"=>nil,
#=>   "photo"=>nil,
#=>   "locale_id"=>1,
#=>   "locale"=>"en-US",
#=>   "organization_id"=>nil,
#=>   "role"=>"end-user",
#=>   "verified"=>false,
#=>   "external_id"=>nil,
#=>   "tags"=>[],
#=>   "alias"=>nil,
#=>   "active"=>true,
#=>   "shared"=>false,
#=>   "shared_agent"=>false,
#=>   "last_login_at"=>nil,
#=>   "two_factor_auth_enabled"=>false,
#=>   "signature"=>nil,
#=>   "details"=>nil,
#=>   "notes"=>nil,
#=>   "role_type"=>nil,
#=>   "custom_role_id"=>nil,
#=>   "moderator"=>false,
#=>   "ticket_restriction"=>"requested",
#=>   "only_private_comments"=>false,
#=>   "restricted_agent"=>true,
#=>   "suspended"=>false,
#=>   "chat_only"=>false,
#=>   "default_group_id"=>nil,
#=>   "report_csv"=>false,
#=>   "organization"=>[]}

Parameters:

  • The client instance to use

  • The User’s ID to use

Returns:



124
125
126
127
# File 'lib/zendesk_support_api/users.rb', line 124

def self.show(client, id)
  res = client.request(:get, "users/#{id}.json?#{included}")
  user_object(res['user'], res['organizations'])
end

.show_many(client, ids) ⇒ Array

Shows many users

Examples:

ZendeskSupportAPI::Users.show_many(client, [123, 456, 789])
#=> [{user123},{user456},{user789}]

Parameters:

  • The client instance to use

  • An array of User IDs to use

Returns:



139
140
141
142
143
144
# File 'lib/zendesk_support_api/users.rb', line 139

def self.show_many(client, ids)
  ids = ids.join(',')
  res = client.request(:get, "users/show_many.json?#{included}&ids=#{ids}")
  res['users'].map { |u| user_object(u, res['organizations']) }
  res['users']
end

.suspend(client, id) ⇒ String

Suspends a user

Examples:

ZendeskSupportAPI::Users.suspend(client, 123)
#=> User 123 is suspended

Parameters:

  • The client instance to use

  • The User ID to suspend

Returns:

  • Either a success message or an error



290
291
292
293
294
295
# File 'lib/zendesk_support_api/users.rb', line 290

def self.suspend(client, id)
  res = client.request(:put, "users/#{id}.json", user: { suspended: true })
  return "Suspension of #{id} failed: #{res['error']}" if res['error']

  "User #{id} suspended" if res['user']['suspended']
end

.update(client, uid, user) ⇒ Hash|String

Updates a user

Examples:

ZendeskSupportAPI::User.update(client, 123, user)
#=> {user}

Parameters:

  • The client instance to use

  • The User’s ID

  • The user details to update

Returns:

  • Either the user details or the error



231
232
233
234
235
236
# File 'lib/zendesk_support_api/users.rb', line 231

def self.update(client, uid, user)
  res = client.request(:put, "users/#{uid}.json", user: user)
  return "Update of #{uid} failed: #{res['error']}" if res['error']

  res
end

.update_many(client, users) ⇒ ZendeskSupportAPI::Client.handle_job

Updates many users

Parameters:

  • The client instance to use

  • The users to update

Returns:

  • Either the user details or the error



244
245
246
247
# File 'lib/zendesk_support_api/users.rb', line 244

def self.update_many(client, users)
  res = client.request(:put, 'users/update_many.json', users: users)
  client.handle_job(res)
end

.user_map(users, orgs) ⇒ Hash

Maps users into user_objects

Parameters:

  • The Array of users to map

  • The Array of orgs to use in mapping

Returns:



20
21
22
# File 'lib/zendesk_support_api/users.rb', line 20

def self.user_map(users, orgs)
  users.map { |u| user_object(u, orgs) }
end

.user_object(user, orgs) ⇒ Hash

Creates a user hash (for mapping the org into the user Hash)

Parameters:

  • The user details to use

  • The Array of orgs to use

Returns:



152
153
154
155
156
# File 'lib/zendesk_support_api/users.rb', line 152

def self.user_object(user, orgs)
  oid = 'organization_id'
  user['organization'] = orgs.select { |o| o['id'] == user[oid] }
  user
end