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 (total: 215336)... / ...done
#=> {users:[{user1},{user2}...{user201520}]}

Parameters:

Returns:

  • (Hash)


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:

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

Parameters:

Returns:

  • (Hash|String)

    Either the user details or the error



158
159
160
161
162
163
# File 'lib/zendesk_support_api/users.rb', line 158

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:

Returns:



171
172
173
174
# File 'lib/zendesk_support_api/users.rb', line 171

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": {
#=>     "id":   9873843,
#=>     "name": "Roger Wilco",
#=>     "email": "[email protected]"
#=>     ...
#=>   }
#=> }

Parameters:

Returns:

  • (Hash|String)

    Either the user details or the error



193
194
195
196
197
198
# File 'lib/zendesk_support_api/users.rb', line 193

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:

Returns:



206
207
208
209
210
211
# File 'lib/zendesk_support_api/users.rb', line 206

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:

Returns:

  • (String)


254
255
256
257
258
259
# File 'lib/zendesk_support_api/users.rb', line 254

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:

Returns:



267
268
269
270
271
# File 'lib/zendesk_support_api/users.rb', line 267

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

.groups(client, uid) ⇒ Array

Shows a users groups

Examples:

ZendeskSupportAPI::Users.groups(client, 1234)
#=> [
#=>   {
#=>     "name":       "DJs",
#=>     "created_at": "2009-05-13T00:07:08Z",
#=>     "updated_at": "2011-07-22T00:11:12Z",
#=>     "id":         211
#=>   },
#=>   {
#=>     "name":       "MCs",
#=>     "created_at": "2009-08-26T00:07:08Z",
#=>     "updated_at": "2010-05-13T00:07:08Z",
#=>     "id":         122
#=>   }
#=> ]

Parameters:

Returns:

  • (Array)


313
314
315
# File 'lib/zendesk_support_api/users.rb', line 313

def self.groups(client, uid)
  client.request(:get, "users/#{uid}/groups.json")['groups']
end

.includedString

Function to return a string that side-loads organizations

Returns:

  • (String)


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:

Returns:

  • (Hash)


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:

  • res (Hash)

    The Hash containing the response from a request

Returns:

  • (nil|String)


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",
#=>   ...
#=> }

Parameters:

Returns:

  • (Hash)


91
92
93
94
# File 'lib/zendesk_support_api/users.rb', line 91

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])
#=> [
#=>   {
#=>     "id": 123,
#=>     "name": "Johnny Appleseed",
#=>     ...
#=>   },
#=>   {
#=>     "id": 456,
#=>     "name": "Rupert Root",
#=>     ...
#=>   }
#=> ]

Parameters:

Returns:

  • (Array)


117
118
119
120
121
122
# File 'lib/zendesk_support_api/users.rb', line 117

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:

Returns:

  • (String)

    Either a success message or an error



283
284
285
286
287
288
# File 'lib/zendesk_support_api/users.rb', line 283

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:

  • client (ZendeskSupportAPI::Client)

    The client instance to use

  • uid (Integer)

    The User’s ID

  • user (Hash)

    The user details to update

Returns:

  • (Hash|String)

    Either the user details or the error



224
225
226
227
228
229
# File 'lib/zendesk_support_api/users.rb', line 224

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:

Returns:



237
238
239
240
# File 'lib/zendesk_support_api/users.rb', line 237

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:

  • users (Array)

    The Array of users to map

  • orgs (Array)

    The Array of orgs to use in mapping

Returns:

  • (Hash)


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:

  • user (Hash)

    The user details to use

  • orgs (Array)

    The Array of orgs to use

Returns:

  • (Hash)


130
131
132
133
134
# File 'lib/zendesk_support_api/users.rb', line 130

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