Class: Readiness::Zendesk::Organizations

Inherits:
Client
  • Object
show all
Defined in:
lib/support_readiness/zendesk/organizations.rb

Overview

TODO:

Merge org

TODO:

Show merge

TODO:

List merges

Defines the class Organizations within the module Readiness::Zendesk.

Author:

  • Jason Colyer

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

auth_error, bad_request_error, convert_actions, convert_conditions, convert_standard_names_to_ids, convert_ticket_form_agent_conditions, convert_ticket_form_brands, convert_ticket_form_end_user_conditions, convert_ticket_form_names_to_ids, convert_view_names_to_ids, convert_view_restrictions, covert_ticket_form_field_ids, create_package!, erb_renderer, handle_request_error, not_found_error, not_processible_error, put_into_archive, recursively_deflate_directory, timestamp_filename, to_clean_json, to_clean_json_with_key, to_hash, to_nearly_clean_json, to_nearly_clean_json_with_key, to_param_string, write_entries

Constructor Details

#initialize(object = {}) ⇒ Organizations

Creates a new Readiness::Zendesk::Organizations instance

Examples:

require 'support_readiness'
Readiness::Zendesk::Organizations.new

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.0



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/support_readiness/zendesk/organizations.rb', line 27

def initialize(object = {})
  @details = object['details']
  @domain_names = object['domain_names']
  @external_id = object['external_id']
  @group_id = object['group_id']
  @id = object['id']
  @name = object['name']
  @notes = object['notes']
  @organization_fields = object['organization_fields']
  @shared_comments = object['shared_comments']
  @shared_tickets = object['shared_tickets']
  @tags = object['tags']
end

Instance Attribute Details

#detailsObject

Since:

  • 1.0.0



16
17
18
# File 'lib/support_readiness/zendesk/organizations.rb', line 16

def details
  @details
end

#domain_namesObject

Since:

  • 1.0.0



16
17
18
# File 'lib/support_readiness/zendesk/organizations.rb', line 16

def domain_names
  @domain_names
end

#external_idObject

Since:

  • 1.0.0



16
17
18
# File 'lib/support_readiness/zendesk/organizations.rb', line 16

def external_id
  @external_id
end

#group_idObject

Since:

  • 1.0.0



16
17
18
# File 'lib/support_readiness/zendesk/organizations.rb', line 16

def group_id
  @group_id
end

#idObject

Since:

  • 1.0.0



16
17
18
# File 'lib/support_readiness/zendesk/organizations.rb', line 16

def id
  @id
end

#nameObject

Since:

  • 1.0.0



16
17
18
# File 'lib/support_readiness/zendesk/organizations.rb', line 16

def name
  @name
end

#notesObject

Since:

  • 1.0.0



16
17
18
# File 'lib/support_readiness/zendesk/organizations.rb', line 16

def notes
  @notes
end

#organization_fieldsObject

Since:

  • 1.0.0



16
17
18
# File 'lib/support_readiness/zendesk/organizations.rb', line 16

def organization_fields
  @organization_fields
end

#shared_commentsObject

Since:

  • 1.0.0



16
17
18
# File 'lib/support_readiness/zendesk/organizations.rb', line 16

def shared_comments
  @shared_comments
end

#shared_ticketsObject

Since:

  • 1.0.0



16
17
18
# File 'lib/support_readiness/zendesk/organizations.rb', line 16

def shared_tickets
  @shared_tickets
end

#tagsObject

Since:

  • 1.0.0



16
17
18
# File 'lib/support_readiness/zendesk/organizations.rb', line 16

def tags
  @tags
end

Class Method Details

.count(client) ⇒ Hash

Show information on the approximate count of organizations.

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
count = Readiness::Zendesk::Organizations.count(client)
pp count.value
# => 54590

Parameters:

  • client (Object)

    An instance of Client

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



119
120
121
122
123
# File 'lib/support_readiness/zendesk/organizations.rb', line 119

def self.count(client)
  response = client.connection.get('organizations/count')
  handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
  Oj.load(response.body)['count']
end

.create!(client, organization) ⇒ Object

Creates an organization. Will exit if unsuccessful

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
org = Readiness::Zendesk::Organizations.new
org.name = 'My Organization'
create = Readiness::Zendesk::Organizations.create!(client, org)
pp create.id
# => 23409462

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



223
224
225
226
227
# File 'lib/support_readiness/zendesk/organizations.rb', line 223

def self.create!(client, organization)
  response = client.connection.post 'organizations', to_clean_json_with_key(organization, 'organization')
  handle_request_error(1, 'Zendesk', response.status, { action: 'Create organization', message: Oj.load(response.body)}) unless response.status == 201
  Organizations.new(Oj.load(response.body)['organization'])
end

.create_many!(client, organizations) ⇒ object

Creates multiple organizations via a batch job

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
org1 = Readiness::Zendesk::Organizations.new
org1.name = 'Org 1'
org2 = Readiness::Zendesk::Organizations.new
org2.name = 'Org 2'
orgs = [org1, org2]
creates = Readiness::Zendesk::Organizations.create_many!(client, orgs)
pp creates.id
# => "8b726e606741012ffc2d782bcb7848fe"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



415
416
417
418
419
420
# File 'lib/support_readiness/zendesk/organizations.rb', line 415

def self.create_many!(client, organizations)
  data = { organizations: organizations.map { |o| to_hash(o).compact } }.to_json
  response = client.connection.post('organizations/create_many', data)
  handle_request_error(1, 'Zendesk', response.status, { action: 'Create many organizations', message: Oj.load(response.body)}) unless response.status == 200
  JobStatuses.new(Oj.load(response.body)['job_status'])
end

.create_or_update!(client, organization) ⇒ Object

Creates or updates an organization. Will exit if unsuccessful

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
org = Readiness::Zendesk::Organizations.find!(client, 123)
org.name = 'My Organization'
create = Readiness::Zendesk::Organizations.create_or_update!(client, org)
pp create.name
# => 'My Organization'
require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
org = Readiness::Zendesk::Organizations.new
org.name = 'My Organization'
create = Readiness::Zendesk::Organizations.create_or_update!(client, org)
pp create.id
# => 23409462

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



262
263
264
265
266
# File 'lib/support_readiness/zendesk/organizations.rb', line 262

def self.create_or_update!(client, organization)
  response = client.connection.post 'organizations/create_or_update', to_clean_json_with_key(organization, 'organization')
  handle_request_error(1, 'Zendesk', response.status, { action: 'Create or update organization', id: organization.name, message: Oj.load(response.body)}) unless [200, 201].include?(response.status)
  Organizations.new(Oj.load(response.body)['organization'])
end

.delete!(client, organization) ⇒ Boolean

Deletes an organization. Will exit if unsuccessful

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
org = Readiness::Zendesk::Organizations.find!(client, 16)
delete = Readiness::Zendesk::Organizations.delete!(client, org)
pp delete
# => true

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



315
316
317
318
319
# File 'lib/support_readiness/zendesk/organizations.rb', line 315

def self.delete!(client, organization)
  response = client.connection.delete "organizations/#{organization.id}"
  handle_request_error(1, 'Zendesk', response.status, { action: 'Delete an organization', id: organization.id, message: Oj.load(response.body)}) unless response.status == 204
  true
end

.delete_many!(client, oids) ⇒ object

Deletes multiple organizations via a batch job

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
deletes = Readiness::Zendesk::Organizations.delete_many!(client, [123, 456])
pp deletes.id
# => "8b726e606741012ffc2d782bcb7848fe"

Parameters:

  • client (Object)

    An instance of Client

  • oids (Array)

    An array of organization IDs to delete

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



472
473
474
475
476
# File 'lib/support_readiness/zendesk/organizations.rb', line 472

def self.delete_many!(client, oids)
  response = client.connection.delete "organizations/destroy_many?ids=#{oids.join(',')}"
  handle_request_error(1, 'Zendesk', response.status, { action: 'Delete many organizations', message: Oj.load(response.body)}) unless response.status == 200
  JobStatuses.new(Oj.load(response.body)['job_status'])
end

.find(client, oid) ⇒ Hash

Locates an organization within Zendesk. This will not exit on error (except Authentication errors)

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
org = Readiness::Zendesk::Organizations.find(client, 4112492)
pp org.name
# => "Groablet Enterprises"

Parameters:

  • client (Object)

    An instance of Client

  • oid (Integer)

    The organization ID to find

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



144
145
146
147
148
149
150
# File 'lib/support_readiness/zendesk/organizations.rb', line 144

def self.find(client, oid)
  response = client.connection.get("organizations/#{oid}")
  handle_request_error(0, 'Zendesk', response.status,  { action: 'get', id: oid }) unless response.status == 200
  return Organizations.new(Oj.load(response.body)['organization']) if response.status == 200

  Oj.load(response.body)
end

.find!(client, oid) ⇒ Object

Locates an organization within Zendesk. This will exit on error

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
org = Readiness::Zendesk::Organizations.find!(client, 4112492)
pp org.name
# => "Groablet Enterprises"

Parameters:

  • client (Object)

    An instance of Client

  • oid (Integer)

    The organization ID to find

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



171
172
173
174
175
# File 'lib/support_readiness/zendesk/organizations.rb', line 171

def self.find!(client, oid)
  response = client.connection.get("organizations/#{oid}")
  handle_request_error(1, 'Zendesk', response.status, { action: 'Find organization', id: oid }) unless response.status == 200
  Organizations.new(Oj.load(response.body)['organization'])
end

.find_many(client, oids) ⇒ Array

Locates up to 100 organizations within Zendesk.

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
orgs = Readiness::Zendesk::Organizations.find_many(client, [35436, 20057623])
pp orgs.map { |o| o.name }
# => ["Important Customers", "Imperial College"]

Parameters:

  • client (Object)

    An instance of Client

  • oids (Array)

    The organization IDs to find

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



196
197
198
199
200
# File 'lib/support_readiness/zendesk/organizations.rb', line 196

def self.find_many(client, oids)
  response = client.connection.get("organizations/show_many?ids=#{oids.join(',')}")
  handle_request_error(0, 'Zendesk', response.status,  { action: 'get', id: oids }) unless response.status == 200
  Oj.load(response.body)['organizations'].map { |o| Organizations.new(o) }
end

.list(client) ⇒ Array

Lists the first 100 organizations

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
orgs = Readiness::Zendesk::Organizations.list(client)
pp orgs.first.id
# => 4112492

Parameters:

  • client (Object)

    An instance of Client

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



59
60
61
62
63
# File 'lib/support_readiness/zendesk/organizations.rb', line 59

def self.list(client)
  response = client.connection.get("organizations?page[size]=100")
  handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
  Oj.load(response.body)['organizations'].map { |o| Organizations.new(o) }
end

.list_many(client, limit = 0, sort = 'id') ⇒ Array

Lists many organizations. Does not stop until it ends or the page limit is hit. This method can take a long time to run depending on the parameters used.

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
orgs = Readiness::Zendesk::Organizations.list_many(client, 4)
pp orgs.count
# => 400

Parameters:

  • client (Object)

    An instance of Client

  • limit (Integer) (defaults to: 0)

    The number of pages to stop at. Using 0 means to list all.

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/support_readiness/zendesk/organizations.rb', line 85

def self.list_many(client, limit = 0, sort = 'id')
  array = []
  opts = "page[size]=100"
  loop do
    response = client.connection.get("organizations?#{opts}")
    handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
    body = Oj.load(response.body)
    array += body['organizations'].map { |o| Organizations.new(o) }
    break if limit != 0 && array.count >= (limit * 100)
    break unless body['meta']['has_more']

    opts = body['links'] ['next'].split('?').last
  end
  array
end

.memberships(client, organization) ⇒ Array

Lists members of an organization

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
org = Readiness::Zendesk::Organizations.find!(client, 123)
members = Readiness::Zendesk::Organizations.memberships(client, org)
pp members.first.user_id
# => 29

Parameters:

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/support_readiness/zendesk/organizations.rb', line 498

def self.memberships(client, organization)
  array = []
  opts = "page[size]=100"
  loop do
    response = client.connection.get("organizations/#{organization.id}/organization_memberships?#{opts}")
    handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
    body = Oj.load(response.body)
    array += body['organization_memberships'].map { |o| OrganizationMemberships.new(o) }
    break unless body['meta']['has_more']

    opts = body['links'] ['next'].split('?').last
  end
  array
end

.tickets(client, organization) ⇒ Array

Lists tickets for an organization.

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
org = Readiness::Zendesk::Organizations.find!(client, 16)
tickets = Readiness::Zendesk::Organizations.tickets(client, org)
pp tickets.first.status
# => "pending"

Parameters:

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/support_readiness/zendesk/organizations.rb', line 341

def self.tickets(client, organization)
  array = []
  opts = "page[size]=100"
  loop do
    response = client.connection.get("organizations/#{organization.id}/tickets?#{opts}")
    handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
    body = Oj.load(response.body)
    array += body['tickets'].map { |t| Tickets.new(t) }
    break unless body['meta']['has_more']

    opts = body['links'] ['next'].split('?').last
  end
  array
end

.update!(client, organization) ⇒ Object

Updates an organization. Will exit if unsuccessful

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
org = Readiness::Zendesk::Organizations.find!(client, 4112492)
org.notes = 'Something interesting'
update = Readiness::Zendesk::Organizations.update!(client, org)
pp update.notes
# => "Something interesting"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



289
290
291
292
293
# File 'lib/support_readiness/zendesk/organizations.rb', line 289

def self.update!(client, organization)
  response = client.connection.put "organizations/#{organization.id}", to_clean_json_with_key(organization, 'organization')
  handle_request_error(1, 'Zendesk', response.status, { action: 'Update organization', id: organization.id, message: Oj.load(response.body)}) unless response.status == 200
  Organizations.new(Oj.load(response.body)['organization'])
end

.update_many!(client, organizations) ⇒ object

Updates multiple organizations via a batch job

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
org1 = Readiness::Zendesk::Organizations.find!(client, 123)
org1.notes = 'Escalated customer, please use due diligence'
org2 = Readiness::Zendesk::Organizations.find!(client, 456)
org2.notes = 'Escalated customer, please use due diligence'
orgs = [org1, org2]
updates = Readiness::Zendesk::Organizations.update_many!(client, orgs)
pp updates.id
# => "8b726e606741012ffc2d782bcb7848fe"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



446
447
448
449
450
451
# File 'lib/support_readiness/zendesk/organizations.rb', line 446

def self.update_many!(client, organizations)
  data = { organizations: organizations.map { |o| to_hash(o).compact } }.to_json
  response = client.connection.put('organizations/update_many', data)
  handle_request_error(1, 'Zendesk', response.status, { action: 'Update many organizations', message: Oj.load(response.body)}) unless response.status == 200
  JobStatuses.new(Oj.load(response.body)['job_status'])
end

.users(client, organization) ⇒ Array

Lists users for an organization.

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
org = Readiness::Zendesk::Organizations.find!(client, 16)
users = Readiness::Zendesk::Organizations.users(client, org)
pp users.count
# => 28

Parameters:

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/support_readiness/zendesk/organizations.rb', line 376

def self.users(client, organization)
  array = []
  opts = "page[size]=100"
  loop do
    response = client.connection.get("organizations/#{organization.id}/users?#{opts}")
    handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
    body = Oj.load(response.body)
    array += body['users'].map { |u| Users.new(u) }
    break unless body['meta']['has_more']

    opts = body['links'] ['next'].split('?').last
  end
  array
end