Class: ZendeskSupportAPI::Users
- Inherits:
-
Object
- Object
- ZendeskSupportAPI::Users
- Defined in:
- lib/zendesk_support_api/users.rb
Overview
Users class - developer.zendesk.com/rest_api/docs/support/users
Class Method Summary collapse
-
.all(client) ⇒ Hash
Lists out all users (paginates over every page).
-
.create(client, user) ⇒ Hash|String
Creates a user.
-
.create_many(client, users) ⇒ ZendeskSupportAPI::Client.handle_job
Creates many users.
-
.create_or_update(client, user) ⇒ Hash|String
Creates or updates a user.
-
.create_or_update_many(client, users) ⇒ ZendeskSupportAPI::Client.handle_job
Creates or updates many users.
-
.delete(client, uid) ⇒ String
Deletes a user.
-
.delete_many(client, ids) ⇒ ZendeskSupportAPI::Client.handle_job
Deletes many users.
-
.groups(client, uid) ⇒ Array
Shows a users groups.
-
.included ⇒ String
Function to return a string that side-loads organizations.
-
.list(client) ⇒ Hash
Lists out users (first 100).
-
.next_page(res) ⇒ nil|String
Returns the string of the next_page for pagination.
-
.show(client, id) ⇒ Hash
Grabs a specific user.
-
.show_many(client, ids) ⇒ Array
Shows many users.
-
.suspend(client, id) ⇒ String
Suspends a user.
-
.update(client, uid, user) ⇒ Hash|String
Updates a user.
-
.update_many(client, users) ⇒ ZendeskSupportAPI::Client.handle_job
Updates many users.
-
.user_map(users, orgs) ⇒ Hash
Maps users into user_objects.
-
.user_object(user, orgs) ⇒ Hash
Creates a user hash (for mapping the org into the user Hash).
Class Method Details
.all(client) ⇒ Hash
Lists out all users (paginates over every page)
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
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
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
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
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
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
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
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 |
.included ⇒ String
Function to return a string that side-loads organizations
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)
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
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
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
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
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
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
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
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)
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 |