Class: Readiness::Zendesk::Tickets

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

Overview

Defines the class Tickets 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 = {}) ⇒ Tickets

Creates a new Readiness::Zendesk::Tickets instance

Examples:

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

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.0



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/support_readiness/zendesk/tickets.rb', line 24

def initialize(object = {})
  @assignee_id = object['assignee_id']
  @additional_tags = object['additional_tags']
  @brand_id = object['brand_id']
  @collaborator_ids = object['collaborator_ids']
  @comment = object['comment']
  @created_at = object['created_at']
  @custom_fields = object['custom_fields']
  @custom_status_id = object['custom_status_id']
  @description = object['description']
  @due_at = object['due_at']
  @email_cc_ids = object['email_cc_ids']
  @email_ccs = object['email_ccs']
  @fields = object['fields']
  @follower_ids = object['follower_ids']
  @followup_ids = object['followup_ids']
  @group_id = object['group_id']
  @has_incidents = object['has_incidents']
  @id = object['id']
  @is_public = object['is_public']
  @organization_id = object['organization_id']
  @priority = object['priority']
  @problem_id = object['problem_id']
  @remove_tags = object['remove_tags']
  @requester = object['requester']
  @requester_id = object['request_id']
  @satisfaction_rating = object['satisfaction_rating']
  @status = object['status']
  @subject = object['subject']
  @submitter_id = object['submitter_id']
  @tags = object['tags']
  @ticket_form_id = object['ticket_form_id']
  @type = object['type']
  @updated_at = object['updated_at']
  @via = object['via']
  @via_followup_source_id = object['via_followup_source_id']
end

Instance Attribute Details

#additional_tagsObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def additional_tags
  @additional_tags
end

#assignee_idObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def assignee_id
  @assignee_id
end

#brand_idObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def brand_id
  @brand_id
end

#collaborator_idsObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def collaborator_ids
  @collaborator_ids
end

#commentObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def comment
  @comment
end

#created_atObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def created_at
  @created_at
end

#custom_fieldsObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def custom_fields
  @custom_fields
end

#custom_status_idObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def custom_status_id
  @custom_status_id
end

#descriptionObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def description
  @description
end

#due_atObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def due_at
  @due_at
end

#email_cc_idsObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def email_cc_ids
  @email_cc_ids
end

#email_ccsObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def email_ccs
  @email_ccs
end

#fieldsObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def fields
  @fields
end

#follower_idsObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def follower_ids
  @follower_ids
end

#followup_idsObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def followup_ids
  @followup_ids
end

#group_idObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def group_id
  @group_id
end

#has_incidentsObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def has_incidents
  @has_incidents
end

#idObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def id
  @id
end

#is_publicObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def is_public
  @is_public
end

#organization_idObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def organization_id
  @organization_id
end

#priorityObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def priority
  @priority
end

#problem_idObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def problem_id
  @problem_id
end

#remove_tagsObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def remove_tags
  @remove_tags
end

#requesterObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def requester
  @requester
end

#requester_idObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def requester_id
  @requester_id
end

#satisfaction_ratingObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def satisfaction_rating
  @satisfaction_rating
end

#statusObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def status
  @status
end

#subjectObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def subject
  @subject
end

#submitter_idObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def submitter_id
  @submitter_id
end

#tagsObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def tags
  @tags
end

#ticket_form_idObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def ticket_form_id
  @ticket_form_id
end

#typeObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def type
  @type
end

#updated_atObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def updated_at
  @updated_at
end

#viaObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def via
  @via
end

#via_followup_source_idObject

Since:

  • 1.0.0



13
14
15
# File 'lib/support_readiness/zendesk/tickets.rb', line 13

def via_followup_source_id
  @via_followup_source_id
end

Class Method Details

.ccs(client, ticket) ⇒ Array

Lists the CCs on a ticket. If your system is not using followers, you need to use collaborators

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)
ticket = Readiness::Zendesk::Tickets.find!(client, 123456)
users = Readiness::Zendesk::Tickets.ccs(client, ticket)
pp users.last.name
# => "Jane End User"

Parameters:

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



134
135
136
137
138
# File 'lib/support_readiness/zendesk/tickets.rb', line 134

def self.ccs(client, ticket)
  response = client.connection.get("tickets/#{ticket.id}/email_ccs")
  handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
  Oj.load(response.body)['users']
end

.collaborators(client, ticket) ⇒ Array

Lists the collaborators on a ticket

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)
ticket = Readiness::Zendesk::Tickets.find!(client, 123456)
users = Readiness::Zendesk::Tickets.collaborators(client, ticket)
pp users.first.name
# => "Johnny Agent"

Parameters:

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



82
83
84
85
86
# File 'lib/support_readiness/zendesk/tickets.rb', line 82

def self.collaborators(client, ticket)
  response = client.connection.get("tickets/#{ticket.id}/collaborators")
  handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
  Oj.load(response.body)['users']
end

.comments(client, ticket, sort = 'created_at') ⇒ Array

Lists the comments on a ticket

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)
ticket = Readiness::Zendesk::Tickets.find!(client, 123456)
comments = Readiness::Zendesk::Tickets.comments(client, ticket)
pp creates.select { |c| c['public'] == false }.count
# => 12

Parameters:

  • client (Object)

    An instance of Client

  • sort (String) (defaults to: 'created_at')

    What sorting method to use

    • created_at => Sort by created_at ASC

    • -created_at => Sort by created_at DESC

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/support_readiness/zendesk/tickets.rb', line 253

def self.comments(client, ticket, sort = 'created_at')
  array = []
  opts = "page[size]=100&sort=#{sort}"
  loop do
    response = client.connection.get("tickets/#{ticket.id}/comments?#{opts}")
    handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
    body = Oj.load(response.body)
    array += body['comments']
    break unless body['meta']['has_more']

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

.create!(client, ticket) ⇒ Object

Creates a ticket. 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)
ticket = Readiness::Zendesk::Tickets.new
ticket.comment = { body: 'The smoke is very colorful'}
ticket.priority = 'urgent'
ticket.subject = 'My printer is on fire!'
create = Readiness::Zendesk::Tickets.create!(client, ticket)
pp create.id
# => 35436

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



291
292
293
294
295
# File 'lib/support_readiness/zendesk/tickets.rb', line 291

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

.create_many!(client, tickets) ⇒ object

Creates multiple tickets 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)
ticket1 = Readiness::Zendesk::Tickets.new
ticket1.comment = { body: 'The smoke is very colorful'}
ticket1.priority = 'urgent'
ticket1.subject = 'My printer is on fire!'
ticket2 = Readiness::Zendesk::Tickets.new
ticket2.comment = { body: 'This is a comment'}
ticket2.priority = 'normal'
ticket2.subject = 'Help'
tickets = [ticket1, ticket2]
creates = Readiness::Zendesk::Tickets.create_many!(client, tickets)
pp creates.id
# => "82de0b044094f0c67893ac9fe64f1a99"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



224
225
226
227
228
229
# File 'lib/support_readiness/zendesk/tickets.rb', line 224

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

.create_satisfaction_score!(client, ticket, score, comment) ⇒ Hash

Add a satisfaction rating to a ticket (must be done using end-user as the config.username). 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)
ticket = Readiness::Zendesk::Tickets.find!(client, 141)
user = Readiness::Zendesk::Users.find!(client, 123456)
oauth_client = Readiness::Zendesk::OAuthClients.find_by_name(client, 'Test App')
new_token = Readiness::Zendesk::OAuthTokens.new
new_token.client_id = oauth_client.id
new_token.scopes = [
  'impersonate',
  'write'
]
oauth_token = Readiness::Zendesk::OAuthTokens.create!(client, new_token)
user_client = Faraday.new(client.url) do |c|
  c.request :retry, {
    max: client.retry_max,
    interval: sb_zendesk_config.retry_interval,
    interval_randomness: client.retry_randomness,
    backoff_factor: client.retry_backoff,
    exceptions: client.retry_exceptions
  }
  c.adapter Faraday.default_adapter
  c.headers['Content-Type'] = 'application/json'
  c.headers['Authorization'] = "Bearer #{oauth_token.full_token}"
  c.headers['X-On-Behalf-Of'] = user.email
end
rating = Readiness::Zendesk::Tickets.create_satisfaction_score!(user_client, ticket, 'good', 'jason is awesome!')
pp rating['score']
# => "good"

Parameters:

  • client (Object)

    An instance of Client

  • ticket (Object)
  • score (String)

    The score to use (‘good’ or ‘bad’ is the normal usage)

  • comment (String)

    The comment to use

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • NEW_VERSION



975
976
977
978
979
980
981
# File 'lib/support_readiness/zendesk/tickets.rb', line 975

def self.create_satisfaction_score!(client, ticket, score, comment)
  data = { satisfaction_rating: { score: score, comment: comment }}.to_json
  response = client.post "tickets/#{ticket.id}/satisfaction_rating", data
  pp response.body
  handle_request_error(1, 'Zendesk', response.status, { action: 'Add satisfaction rating', message: Oj.load(response.body)}) unless response.status == 200
  Oj.load(response.body)['satisfaction_rating']
end

.delete!(client, ticket) ⇒ Boolean

Deletes a ticket. 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)
ticket = Readiness::Zendesk::Tickets.find!(client, 123456)
delete = Readiness::Zendesk::Tickets.delete!(client, ticket)
pp delete
# => true

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



372
373
374
375
376
# File 'lib/support_readiness/zendesk/tickets.rb', line 372

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

.delete_many!(client, tids) ⇒ object

Deletes multiple tickets 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)
delete = Readiness::Zendesk::Tickets.delete_many!(client, [35436, 35437])
pp delete.id
# => "82de0b044094f0c67893ac9fe64f1a99"

Parameters:

  • client (Object)

    An instance of Client

  • tids (Array)

    An array of ticket IDs

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



159
160
161
162
163
# File 'lib/support_readiness/zendesk/tickets.rb', line 159

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

.delete_many_suspended!(client, sids) ⇒ Boolean

Deletes many suspended tickets 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)
deletes = Readiness::Zendesk::Tickets.delete_many_suspended!(client, [123, 456])
pp deletes
# => true

Parameters:

  • client (Object)

    An instance of Client

  • sids (Array)

    An Array of suspended ticket IDs to delete

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



848
849
850
851
852
# File 'lib/support_readiness/zendesk/tickets.rb', line 848

def self.delete_many_suspended!(client, sids)
  response = client.connection.delete("suspended_tickets/destroy_many?ids=#{sids.join(',')}")
  handle_request_error(1, 'Zendesk', response.status, { action: 'Delete many suspended ticket', id: sids }) unless response.status == 200
  true
end

.delete_permanently!(client, tid) ⇒ Object

Deletes a deleted ticket. This 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)
delete = Readiness::Zendesk::Tickets.delete_permanently!(client, 123456)
pp delete.id
# => "82de0b044094f0c67893ac9fe64f1a99"

Parameters:

  • client (Object)

    An instance of Client

  • tid (Integer)

    The deleted ticket’s ID

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



496
497
498
499
500
# File 'lib/support_readiness/zendesk/tickets.rb', line 496

def self.delete_permanently!(client, tid)
  response = client.connection.delete "deleted_tickets/#{tid}"
  handle_request_error(1, 'Zendesk', response.status) unless response.status == 200
  JobStatuses.new(Oj.load(response.body)['job_status'])
end

.delete_suspended!(client, sid) ⇒ Boolean

Deletes a suspended ticket 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)
delete = Readiness::Zendesk::Tickets.delete_suspended!(client, 123)
pp delete
# => true

Parameters:

  • client (Object)

    An instance of Client

  • sid (Integer)

    The ID of the suspended ticket

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



823
824
825
826
827
# File 'lib/support_readiness/zendesk/tickets.rb', line 823

def self.delete_suspended!(client, sid)
  response = client.connection.delete("suspended_tickets/#{sid}")
  handle_request_error(1, 'Zendesk', response.status, { action: 'Delete suspended ticket', id: sid }) unless response.status == 204
  true
end

.find(client, tid) ⇒ Hash

Locates a ticket 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)
ticket = Readiness::Zendesk::Tickets.find(client, 35436)
pp ticket.subject
# => "Help, my printer is on fire!"

Parameters:

  • client (Object)

    An instance of Client

  • tid (Integer)

    The ticket ID to find

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



663
664
665
666
667
668
669
# File 'lib/support_readiness/zendesk/tickets.rb', line 663

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

  Oj.load(response.body)
end

.find!(client, tid) ⇒ Hash

Locates a ticket 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)
ticket = Readiness::Zendesk::Tickets.find!(client, 35436)
pp ticket.subject
# => "Help, my printer is on fire!"

Parameters:

  • client (Object)

    An instance of Client

  • tid (Integer)

    The ticket ID to find

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



690
691
692
693
694
# File 'lib/support_readiness/zendesk/tickets.rb', line 690

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

.find_many(client, tids) ⇒ Array

Locates up to 100 tickets 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)
tickets = Readiness::Zendesk::Tickets.find_many(client, [35436, 35437])
pp ticket.count
# => 2

Parameters:

  • client (Object)

    An instance of Client

  • tids (Array)

    The ticket IDs to find

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



715
716
717
718
719
# File 'lib/support_readiness/zendesk/tickets.rb', line 715

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

.followers(client, ticket) ⇒ Array

Lists the followers on a ticket

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)
ticket = Readiness::Zendesk::Tickets.find!(client, 123456)
users = Readiness::Zendesk::Tickets.followers(client, ticket)
pp users.last.name
# => "Peter Admin"

Parameters:

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



108
109
110
111
112
# File 'lib/support_readiness/zendesk/tickets.rb', line 108

def self.followers(client, ticket)
  response = client.connection.get("tickets/#{ticket.id}/followers")
  handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
  Oj.load(response.body)['users']
end

.incidents(client, ticket) ⇒ Array

Lists the incident tickets connected to a specific ticket.

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)
ticket = Readiness::Zendesk::Tickets.find!(client, 123456)
incidents = Readiness::Zendesk::Tickets.incidents(client, ticket)
pp incidents.map { |i| i.id }
# => [33, 34]

Parameters:

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/support_readiness/zendesk/tickets.rb', line 522

def self.incidents(client, ticket)
  array = []
  opts = "page[size]=100"
  loop do
    response = client.connection.get("tickets/#{ticket.id}/incidents?#{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

.list(client, sort = 'id') ⇒ Array

Lists the first 100 non-archived tickets

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)
tickets = Readiness::Zendesk::Tickets.list(client)
pp tickets.first.id
# => 35436

Parameters:

  • client (Object)

    An instance of Client

  • sort (String) (defaults to: 'id')

    What sorting method to use

    • updated_at => Sort by updated_at ASC

    • id => Sort by id ASC

    • status => Sort by status ASC

    • -updated_at => Sort by updated_at DESC

    • -id => Sort by id DESC

    • -status => Sort by status DESC

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



595
596
597
598
599
# File 'lib/support_readiness/zendesk/tickets.rb', line 595

def self.list(client, sort = 'id')
  response = client.connection.get("tickets?page[size]=100&sort=#{sort}")
  handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
  Oj.load(response.body)['tickets'].map { |t| Tickets.new(t) }
end

.list_deleted(client, sort = 'id') ⇒ Array

Lists the first 100 deleted tickets

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)
tickets = Readiness::Zendesk::Tickets.list_deleted(client)
pp tickets.count
# => 1

Parameters:

  • client (Object)

    An instance of Client

  • sort (String) (defaults to: 'id')

    What sorting method to use

    • updated_at => Sort by updated_at ASC

    • id => Sort by id ASC

    • status => Sort by status ASC

    • -updated_at => Sort by updated_at DESC

    • -id => Sort by id DESC

    • -status => Sort by status DESC

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



403
404
405
406
407
# File 'lib/support_readiness/zendesk/tickets.rb', line 403

def self.list_deleted(client, sort = 'id')
  response = client.connection.get("deleted_tickets?page[size]=100&sort=#{sort}")
  handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
  Oj.load(response.body)['deleted_tickets']
end

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

Lists many tickets. 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)
tickets = Readiness::Zendesk::Tickets.list_many(client, 5)
pp tickets.count
# => 500

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.

  • sort (String) (defaults to: 'id')

    What sorting method to use

    • updated_at => Sort by updated_at ASC

    • id => Sort by id ASC

    • status => Sort by status ASC

    • -updated_at => Sort by updated_at DESC

    • -id => Sort by id DESC

    • -status => Sort by status DESC

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/support_readiness/zendesk/tickets.rb', line 628

def self.list_many(client, limit = 0, sort = 'id')
  array = []
  opts = "page[size]=100&sort=#{sort}"
  loop do
    response = client.connection.get("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 if limit != 0 && array.count >= (limit * 100)
    break unless body['meta']['has_more']

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

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

Lists many deleted tickets. 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)
tickets = Readiness::Zendesk::Tickets.list_many_deleted(client, 3)
pp tickets.count
# => 267

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.

  • sort (String) (defaults to: 'id')

    What sorting method to use

    • updated_at => Sort by updated_at ASC

    • id => Sort by id ASC

    • status => Sort by status ASC

    • -updated_at => Sort by updated_at DESC

    • -id => Sort by id DESC

    • -status => Sort by status DESC

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/support_readiness/zendesk/tickets.rb', line 436

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

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

.list_suspended(client) ⇒ Array

Locates all suspended tickets 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)
tickets = Readiness::Zendesk::Tickets.list_suspended(client)
pp ticket.count
# => 18

Parameters:

  • client (Object)

    An instance of Client

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'lib/support_readiness/zendesk/tickets.rb', line 739

def self.list_suspended(client)
  array = []
  opts = "page[size]=100"
  loop do
    response = client.connection.get("suspended_tickets?#{opts}")
    handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
    body = Oj.load(response.body)
    array += body['suspended_tickets']
    break unless body['meta']['has_more']

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

.mark_as_spam!(client, ticket) ⇒ Boolean

Marks a ticket as spam and suspends the user. 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)
ticket = Readiness::Zendesk::Tickets.find!(client, 123456)
mark = Readiness::Zendesk::Tickets.mark_as_spam!(client, ticket)
pp mark
# => true

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



346
347
348
349
350
# File 'lib/support_readiness/zendesk/tickets.rb', line 346

def self.mark_as_spam!(client, ticket)
  response = client.connection.put "tickets/#{ticket.id}/mark_as_spam"
  handle_request_error(1, 'Zendesk', response.status, { action: 'Mark ticket as spam', id: ticket.id, message: Oj.load(response.body)}) unless response.status == 200
  true
end

.problems(client) ⇒ Array

Lists all problem tickets

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)
problems = Readiness::Zendesk::Tickets.problems(client)
pp problems.map { |p| p.id }
# => [33, 34]

Parameters:

  • client (Object)

    An instance of Client

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/support_readiness/zendesk/tickets.rb', line 555

def self.problems(client)
  array = []
  opts = "page[size]=100"
  loop do
    response = client.connection.get("problems?#{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

.recover_many_suspended!(client, sids) ⇒ Array

Recover many suspended tickets 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)
tickets = Readiness::Zendesk::Tickets.recover_many_suspended!(client, [123, 456, 789])
pp tickets.first.subject
# => "Help, my printer is on fire!"

Parameters:

  • client (Object)

    An instance of Client

  • sids (Array)

    An Array of suspended ticket IDs to recover

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



798
799
800
801
802
# File 'lib/support_readiness/zendesk/tickets.rb', line 798

def self.recover_many_suspended!(client, sids)
  response = client.connection.put("suspended_tickets/recover_many?ids=#{sids.join(',')}")
  handle_request_error(1, 'Zendesk', response.status, { action: 'Recover many suspended ticket', id: sids }) unless response.status == 200
  Oj.load(response.body)['tickets'].map { |t| Tickets.new(t) }
end

.recover_suspended!(client, sid) ⇒ Object

Recovers a suspended ticket 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)
ticket = Readiness::Zendesk::Tickets.recover_suspended!(client, 123)
pp ticket.subject
# => "Help, my printer is on fire!"

Parameters:

  • client (Object)

    An instance of Client

  • sid (Integer)

    The ID of the suspended ticket

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



773
774
775
776
777
# File 'lib/support_readiness/zendesk/tickets.rb', line 773

def self.recover_suspended!(client, sid)
  response = client.connection.put("suspended_tickets/#{sid}/recover")
  handle_request_error(1, 'Zendesk', response.status, { action: 'Recover suspended ticket', id: sid }) unless response.status == 200
  Tickets.new(Oj.load(response.body)['ticket'])
end

.redact_attachments(client, ticket) ⇒ Boolean

Redacts all attachments (inline and external) within a ticket. 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)
ticket = Readiness::Zendesk::Tickets.find!(client, 141)
redaction = Readiness::Zendesk::Tickets.redact_attachments(client, ticket)
pp redaction
# => true

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.12



874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
# File 'lib/support_readiness/zendesk/tickets.rb', line 874

def self.redact_attachments(client, ticket)
  redactions = []
  Tickets.comments(client, ticket).each do |c|
    object = {
      id: c['id'],
      redaction: { ticket_id: ticket.id }
    }
    object[:redaction][:external_attachment_urls] = c['attachments'].map { |a| a['content_url'] } unless c['attachments'].count.zero?
    object[:redaction][:html_body] = c['html_body'].gsub('<img', '<img redact') if c['html_body'] =~ /<img/
    redactions.push(object) unless object[:redaction][:external_attachment_urls].nil? && object[:redaction][:html_body].nil?
  end
  redactions.each do |r|
    response = client.connection.put "comment_redactions/#{r[:id]}", r[:redaction].to_json
    handle_request_error(0, 'Zendesk', response.status, { action: 'Redact attachments', id: "tickets/#{ticket.id}/comments/#{r[:id]}" }) unless response.status == 200
    return false
  end
  true
end

.redact_attachments!(client, ticket) ⇒ Boolean

Redacts all attachments (inline and external) within a ticket. 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)
ticket = Readiness::Zendesk::Tickets.find!(client, 141)
redaction = Readiness::Zendesk::Tickets.redact_attachments!(client, ticket)
pp redaction
# => true

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.12



913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# File 'lib/support_readiness/zendesk/tickets.rb', line 913

def self.redact_attachments!(client, ticket)
  redactions = []
  Tickets.comments(client, ticket).each do |c|
    object = {
      id: c['id'],
      redaction: { ticket_id: ticket.id }
    }
    object[:redaction][:external_attachment_urls] = c['attachments'].map { |a| a['content_url'] } unless c['attachments'].count.zero?
    object[:redaction][:html_body] = c['html_body'].gsub('<img', '<img redact') if c['html_body'] =~ /<img/
    redactions.push(object) unless object[:redaction][:external_attachment_urls].nil? && object[:redaction][:html_body].nil?
  end
  redactions.each do |r|
    response = client.connection.put "comment_redactions/#{r[:id]}", r[:redaction].to_json
    handle_request_error(1, 'Zendesk', response.status, { action: 'Redact attachments', id: "tickets/#{ticket.id}/comments/#{r[:id]}" }) unless response.status == 200
  end
  true
end

.restore!(client, tid) ⇒ Boolean

Restores a deleted ticket. This 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)
ticket = Readiness::Zendesk::Tickets.restore!(client, 123456)
pp ticket
# => true

Parameters:

  • client (Object)

    An instance of Client

  • tid (Integer)

    The deleted ticket’s ID

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



471
472
473
474
475
# File 'lib/support_readiness/zendesk/tickets.rb', line 471

def self.restore!(client, tid)
  response = client.connection.put "deleted_tickets/#{tid}/restore"
  handle_request_error(1, 'Zendesk', response.status) unless response.status == 200
  true
end

.update!(client, ticket) ⇒ Object

Updates a ticket. 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)
ticket = Readiness::Zendesk::Tickets.find!(client, 35436)
ticket.status = 'open'
ticket.custom_status_id = 123
ticket.comment = { body: '"The smoke is very colorful', author_id: 494820284 }
update = Readiness::Zendesk::Tickets.update!(client, ticket)
pp update.status
# => "open"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



320
321
322
323
324
# File 'lib/support_readiness/zendesk/tickets.rb', line 320

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

.update_many!(client, tickets) ⇒ object

Updates multiple tickets 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)
ticket1 = Readiness::Zendesk::Tickets.find!(client, 1)
ticket1.status = 'solved'
ticket2 = Readiness::Zendesk::Tickets.find!(client, 2)
ticket2.status = 'pending'
tickets = [ticket1, ticket2]
updates = Readiness::Zendesk::Tickets.update_many!(client, tickets)
pp updates.id
# => "82de0b044094f0c67893ac9fe64f1a99"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



189
190
191
192
193
194
# File 'lib/support_readiness/zendesk/tickets.rb', line 189

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