Class: Readiness::Zendesk::TicketFields

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

Overview

Defines the class TicketFields 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(client, object = {}) ⇒ TicketFields

Creates a new Readiness::Zendesk::TicketFields instance

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)
Readiness::Zendesk::TicketFields.new(client)

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.0



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/support_readiness/zendesk/ticket_fields.rb', line 30

def initialize(client, object = {})
  @active = object['active']
  @agent_description = object['agent_description']
  @collapsed_for_agents = object['collapsed_for_agents']
  @custom_field_options = ticket_field_options(client, object)
  @description = object['description']
  @editable_in_portal = object['editable_in_portal']
  @id = object['id']
  @key = object['key']
  @position = object['position']
  @raw_title_in_portal = object['raw_title_in_portal']
  @regexp_for_validation = object['regexp_for_validation']
  @removable = object['removable']
  @required = object['required']
  @system_field_options = object['system_field_options']
  @tag = object['tag']
  @title = object['title']
  @title_in_portal = object['title_in_portal']
  @type = object['type']
  @visible_in_portal = object['visible_in_portal']
end

Instance Attribute Details

#activeObject

Since:

  • 1.0.0



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

def active
  @active
end

#agent_descriptionObject

Since:

  • 1.0.0



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

def agent_description
  @agent_description
end

#collapsed_for_agentsObject

Since:

  • 1.0.0



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

def collapsed_for_agents
  @collapsed_for_agents
end

#custom_field_optionsObject

Since:

  • 1.0.0



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

def custom_field_options
  @custom_field_options
end

#descriptionObject

Since:

  • 1.0.0



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

def description
  @description
end

#editable_in_portalObject

Since:

  • 1.0.0



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

def editable_in_portal
  @editable_in_portal
end

#idObject

Since:

  • 1.0.0



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

def id
  @id
end

#keyObject

Since:

  • 1.0.0



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

def key
  @key
end

#positionObject

Since:

  • 1.0.0



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

def position
  @position
end

#raw_title_in_portalObject

Since:

  • 1.0.0



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

def raw_title_in_portal
  @raw_title_in_portal
end

#regexp_for_validationObject

Since:

  • 1.0.0



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

def regexp_for_validation
  @regexp_for_validation
end

#removableObject

Since:

  • 1.0.0



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

def removable
  @removable
end

#requiredObject

Since:

  • 1.0.0



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

def required
  @required
end

#system_field_optionsObject

Since:

  • 1.0.0



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

def system_field_options
  @system_field_options
end

#tagObject

Since:

  • 1.0.0



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

def tag
  @tag
end

#titleObject

Since:

  • 1.0.0



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

def title
  @title
end

#title_in_portalObject

Since:

  • 1.0.0



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

def title_in_portal
  @title_in_portal
end

#typeObject

Since:

  • 1.0.0



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

def type
  @type
end

#visible_in_portalObject

Since:

  • 1.0.0



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

def visible_in_portal
  @visible_in_portal
end

Class Method Details

.create!(client, field) ⇒ Object

Creates a ticket field. Will exit if unsuccessful. Note that this does not not create ticket field options. Use Readiness::Zendesk::TicketFieldOptions for that.

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)
field = Readiness::Zendesk::TicketFields.new(client)
field.type = 'text'
field.title = 'Age'
create = Readiness::Zendesk::TicketFields.create!(client, field)
pp create.id
# => 89

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



231
232
233
234
235
236
# File 'lib/support_readiness/zendesk/ticket_fields.rb', line 231

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

.delete!(client, field) ⇒ Boolean

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

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



290
291
292
293
294
# File 'lib/support_readiness/zendesk/ticket_fields.rb', line 290

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

.find(client, fid) ⇒ Hash

Locates a ticket field 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)
field = Readiness::Zendesk::TicketFields.find(client, 89)
pp field.title
# => "Age"

Parameters:

  • client (Object)

    An instance of Client

  • fid (Integer)

    The ticket field ID to find

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



123
124
125
126
127
128
129
# File 'lib/support_readiness/zendesk/ticket_fields.rb', line 123

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

  Oj.load(response.body)
end

.find!(client, fid) ⇒ Hash

Locates a ticket field 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)
field = Readiness::Zendesk::TicketFields.find!(client, 89)
pp field.title
# => "Age"

Parameters:

  • client (Object)

    An instance of Client

  • fid (Integer)

    The ticket field ID to find

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



150
151
152
153
154
# File 'lib/support_readiness/zendesk/ticket_fields.rb', line 150

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

.find_by_name(client, name, cache = nil) ⇒ Object

Locates a ticket field within Zendesk by name. Can utilize a cacheh for quicker results

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)
field = Readiness::Zendesk::TicketFields.find_by_name(client, 'Age')
pp field.id
# => 89

Parameters:

  • client (Object)

    An instance of Client

  • name (String)

    The ticket field name to look for

  • cache (Array) (defaults to: nil)

    The results of Webhooks#list

Returns:

Author:

  • Jason Colyer

Since:

  • 1.0.12



175
176
177
178
179
180
181
182
# File 'lib/support_readiness/zendesk/ticket_fields.rb', line 175

def self.find_by_name(client, name, cache = nil)
  fields = if cache.nil?
             TicketFields.list(client)
           else
             cache
           end
  fields.detect { |f| f.title == name }
end

.find_by_name!(client, name) ⇒ Object

Locates a ticket field within Zendesk by name. 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)
field = Readiness::Zendesk::TicketFields.find_by_name!(client, 'Age')
pp field.id
# => 89

Parameters:

  • client (Object)

    An instance of Client

  • name (String)

    The ticket field name to look for

Returns:

Author:

  • Jason Colyer

Since:

  • 1.0.12



202
203
204
205
206
207
# File 'lib/support_readiness/zendesk/ticket_fields.rb', line 202

def self.find_by_name!(client, name)
  fields = TicketFields.list(client)
  field = fields.detect { |f| f.title == name }
  handle_request_error(1, 'Zendesk', 404, { action: 'Find ticket field', id: name }) if field.nil?
  field
end

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

Lists all ticket fields.

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)
fields = Readiness::Zendesk::TicketFields.list(client, 2)
pp fields.count
# => 200

Parameters:

  • client (Object)

    An instance of Client

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/support_readiness/zendesk/ticket_fields.rb', line 88

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

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

.update!(client, field) ⇒ Object

Updates a ticket field. Will exit if unsuccessful. Note that this does not not update ticket field options. Use Readiness::Zendesk::TicketFieldOptions for that.

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)
field = Readiness::Zendesk::TicketFields.find!(client, 21938362)
field.title = 'Pies'
field.custom_field_options = [
  { name: 'Apple Pie', value: 'apple' },
  { name: 'Pecan Pie', value: 'pecan' }
]
update = Readiness::Zendesk::TicketFields.update!(client, field)
pp update.title
# => "Pies"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



263
264
265
266
267
268
# File 'lib/support_readiness/zendesk/ticket_fields.rb', line 263

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

Instance Method Details

#ticket_field_options(client, object) ⇒ Array

Gets the ticket field options for a ticket field

Parameters:

Returns:

  • (Array)

Author:

  • Jason Colyer

Since:

  • 1.0.12



60
61
62
63
64
65
66
67
68
# File 'lib/support_readiness/zendesk/ticket_fields.rb', line 60

def ticket_field_options(client, object)
  return nil unless %w[tagger multiselect].include? object['type']

  # Handles when the position value is nil
  object['custom_field_options'].each do |c|
    c['position'] = object['custom_field_options'].index(c)
  end
  object['custom_field_options'].map { |c| TicketFieldOptions.new(c) }
end