Class: Readiness::Zendesk::Views

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

Overview

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

Creates a new Readiness::Zendesk::Views instance

Examples:

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

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.0



24
25
26
27
28
29
30
31
32
33
# File 'lib/support_readiness/zendesk/views.rb', line 24

def initialize(object = {})
  @active = object['active']
  @conditions = object['conditions']
  @description = object['description']
  @execution = object['execution']
  @id = object['id']
  @position = object['position']
  @restriction = object['restriction']
  @title = object['title']
end

Instance Attribute Details

#activeObject

Since:

  • 1.0.0



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

def active
  @active
end

#conditionsObject

Since:

  • 1.0.0



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

def conditions
  @conditions
end

#descriptionObject

Since:

  • 1.0.0



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

def description
  @description
end

#executionObject

Since:

  • 1.0.0



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

def execution
  @execution
end

#idObject

Since:

  • 1.0.0



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

def id
  @id
end

#positionObject

Since:

  • 1.0.0



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

def position
  @position
end

#restrictionObject

Since:

  • 1.0.0



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

def restriction
  @restriction
end

#titleObject

Since:

  • 1.0.0



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

def title
  @title
end

Class Method Details

.count_tickets(client, view) ⇒ Hash

Display count information for a view

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)
view = Readiness::Zendesk::Views.find!(client, 9873858)
count = Readiness::Zendesk::Views.count_tickets(client, view)
pp value['value']
# => 719

Parameters:

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



441
442
443
444
445
# File 'lib/support_readiness/zendesk/views.rb', line 441

def self.count_tickets(client, view)
  response = client.connection.get "views/#{view.id}/count"
  handle_request_error(1, 'Zendesk', response.status, { action: 'Count tickets in a view', message: Oj.load(response.body)}) unless response.status == 200
  Oj.load(response.body)['view_count']
end

.count_tickets_in_many(client, vids) ⇒ Array

Display count information for multiple views

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::Views.count_tickets_in_many(client, [25, 78])
pp value.map { |v| v['value'] }
# => [719, nil]

Parameters:

  • client (Object)

    An instance of Client

  • vids (Array)

    The view IDs to get count information for

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



466
467
468
469
470
# File 'lib/support_readiness/zendesk/views.rb', line 466

def self.count_tickets_in_many(client, vids)
  response = client.connection.get "views/count_many?ids=#{vids.join(',')}"
  handle_request_error(1, 'Zendesk', response.status, { action: 'Count tickets in views', message: Oj.load(response.body)}) unless response.status == 200
  Oj.load(response.body)['view_counts']
end

.create!(client, view) ⇒ Object

Creates a view. 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)
view = Readiness::Zendesk::Views.new
view.title = "Kelly's tickets"
view.description = 'Tickets that are assigned to Kelly'
view.active = true
view.position = 3
view.conditions = {
  "all" => [
    { "field" => 'status', "operator" => 'less_than', "value" => 'solved' }
  ],
  "any" => []
}
view.execution = {
  "columns" => [
    { "id" => 'status', "title" => 'Status', "filterable" => true, "sortable" => true},
    { "id" => 'requester', "title" => 'Requester', "filterable" => true, "sortable" => true},
    { "id" => 'assignee', "title" => 'Assignee', "filterable" => true, "sortable" => true},
  ],
  "group_by" => 'assignee',
  "group_order" => 'desc',
  "sort_by" => 'status',
  "sort_order" => 'desc',
  "group" => {
    "id" => 'assignee',
    "title" => 'Assignee',
    "filterable" => true,
    "sortable" => true,
    "order" => 'desc'
  },
  "sort" => {
    "id" => 'status',
    "title" => 'Status',
    "filterable" => true,
    "sortable" => true,
    "order" => 'desc'
  }
}
create = Readiness::Zendesk::Views.create!(client, view)
pp create.id
# => 9873843

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



248
249
250
251
252
# File 'lib/support_readiness/zendesk/views.rb', line 248

def self.create!(client, view)
  response = client.connection.post 'views', { view: create_or_update_object(view) }.to_json
  handle_request_error(1, 'Zendesk', response.status, { action: 'Create view', message: Oj.load(response.body)}) unless response.status == 201
  Views.new(Oj.load(response.body)['view'])
end

.create_or_update_object(view) ⇒ Hash

Returns a Hash used in creating/updating views, as the formatting does not match the Zendesk list functions

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)
view = Readiness::Zendesk::Views.find!(client, 9873858)
object = Readiness::Zendesk::Views.create_or_update_object(view)
pp object[:id]
# => 9873858

Parameters:

Returns:

  • (Hash)

Author:

  • Jason Colyer

Since:

  • 1.0.0



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/support_readiness/zendesk/views.rb', line 490

def self.create_or_update_object(view)
  {
    id: view.id,
    title: view.title,
    description: view.description,
    active: view.active,
    position: view.position,
    restriction: view.restriction,
    all: view.conditions['all'],
    any: view.conditions['any'],
    output: {
      columns: view.execution['columns'].map { |c| c['id'] },
      group_by: view.execution['group_by'],
      group_order: view.execution['group_order'],
      sort_by: view.execution['sort_by'],
      sort_order: view.execution['sort_order']
    }
  }.compact
end

.delete!(client, view) ⇒ Boolean

Deletes a view. 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)
view = Readiness::Zendesk::Views.find!(client, 9873858)
delete = Readiness::Zendesk::Views.delete!(client, views)
pp delete
# => true

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



332
333
334
335
336
# File 'lib/support_readiness/zendesk/views.rb', line 332

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

.delete_many!(client, vids) ⇒ Boolean

Deletes multiple views

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::Views.find!(client, [9873858, 9873843])
pp deletes
# => true

Parameters:

  • client (Object)

    An instance of Client

  • vids (Array)

    An array of view IDs

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



357
358
359
360
361
# File 'lib/support_readiness/zendesk/views.rb', line 357

def self.delete_many!(client, vids)
  response = client.connection.delete("views/destroy_many?ids=#{vids.join(',')}")
  handle_request_error(1, 'Zendesk', response.status, { action: 'Delete many views', message: Oj.load(response.body)}) unless response.status == 204
  true
end

.find(client, vid) ⇒ Hash

Locates a view 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)
view = Readiness::Zendesk::Views.find(client, 25)
pp view.title
# => "Tickets updated less than 12 Hours"

Parameters:

  • client (Object)

    An instance of Client

  • vid (Integer)

    The view ID to find

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



87
88
89
90
91
92
93
# File 'lib/support_readiness/zendesk/views.rb', line 87

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

  Oj.load(response.body)
end

.find!(client, vid) ⇒ Object

Locates a view 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)
view = Readiness::Zendesk::Views.find!(client, 25)
pp view.title
# => "Tickets updated less than 12 Hours"

Parameters:

  • client (Object)

    An instance of Client

  • vid (Integer)

    The view ID to find

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



114
115
116
117
118
# File 'lib/support_readiness/zendesk/views.rb', line 114

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

.find_by_name(client, name) ⇒ Object

Locates a view within Zendesk by name. 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::Views.find_by_name(client, 'Tickets updated less than 12 Hours')
pp field.id
# => 25

Parameters:

  • client (Object)

    An instance of Client

  • name (String)

    The view name to look for

Returns:

Author:

  • Jason Colyer

Since:

  • 1.0.12



163
164
165
166
# File 'lib/support_readiness/zendesk/views.rb', line 163

def self.find_by_name(client, name)
  views = Views.list(client)
  views.detect { |v| v.title == name }
end

.find_by_name!(client, name) ⇒ Object

Locates a view 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::Views.find_by_name!(client, 'Tickets updated less than 12 Hours')
pp field.id
# => 25

Parameters:

  • client (Object)

    An instance of Client

  • name (String)

    The view name to look for

Returns:

Author:

  • Jason Colyer

Since:

  • 1.0.12



186
187
188
189
190
191
# File 'lib/support_readiness/zendesk/views.rb', line 186

def self.find_by_name!(client, name)
  views = Views.list(client)
  view = views.detect { |v| v.title == name }
  handle_request_error(1, 'Zendesk', 404, { action: 'Find view', id: name }) if view.nil?
  view
end

.find_many(client, vids) ⇒ Array

Locates up to 100 views 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)
views = Readiness::Zendesk::Views.find_many(client, [25, 26])
pp views.map { |v| v.title }
# => ["Tickets updated less than 12 Hours", "Tickets updated more than 12 Hours"]

Parameters:

  • client (Object)

    An instance of Client

  • vids (Array)

    The view IDs to find

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



139
140
141
142
143
# File 'lib/support_readiness/zendesk/views.rb', line 139

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

.list(client) ⇒ Array

Lists views

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)
views = Readiness::Zendesk::Views.list(client)
pp views.first.title
# => "Tickets updated less than 12 Hours"

Parameters:

  • client (Object)

    An instance of Client

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/support_readiness/zendesk/views.rb', line 53

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

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

.preview!(client, view) ⇒ Hash

Previews what a view would look like

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)
view = Readiness::Zendesk::Views.find!(client, 9873858)
preview = Readiness::Zendesk::Views.preview!(client, view)
pp preview['rows']['tickets'].first['id']
# => 12851

Parameters:

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



383
384
385
386
387
388
389
390
391
392
393
# File 'lib/support_readiness/zendesk/views.rb', line 383

def self.preview!(client, view)
  converted = create_or_update_object(view)
  data = {
    all: converted[:all],
    any: converted[:any],
    output: converted[:output]
  }.compact
  response = client.connection.post 'views/preview', { view: data }.to_json
  handle_request_error(1, 'Zendesk', response.status, { action: 'Preview a view', message: Oj.load(response.body)}) unless response.status == 200
  Oj.load(response.body)
end

.tickets(client, view) ⇒ Array

Get the tickets in a view

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)
view = Readiness::Zendesk::Views.find!(client, 9873858)
tickets = Readiness::Zendesk::Views.tickets(client, view)
pp tickets.first.id
# => 12851

Parameters:

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



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

def self.tickets(client, view)
  response = client.connection.get "views/#{view.id}/tickets"
  handle_request_error(1, 'Zendesk', response.status, { action: 'Get tickets from a view', message: Oj.load(response.body)}) unless response.status == 200
  Oj.load(response.body)['tickets']
end

.update!(client, view) ⇒ Object

Updates a view. 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)
view = Readiness::Zendesk::Views.find!(client, 9873843)
view.title = 'Canada tickets'
update = Readiness::Zendesk::Views.update!(client, view)
pp create.title
# => "Canada tickets"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



275
276
277
278
279
# File 'lib/support_readiness/zendesk/views.rb', line 275

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

.update_many!(client, views) ⇒ object

Updates multiple views 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)
view1 = Readiness::Zendesk::Views.find!(client, 9873843)
view1.title = 'Canada tickets'
view2 = Readiness::Zendesk::Views.find!(client, 9873858)
view2.title = 'Mexico tickets'
views = [view1, view2]
updates = Readiness::Zendesk::Views.update_many!(client, views)
pp updates.map { |v| v.title }
# => ["Canada tickets", "Mexico tickets"]

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



305
306
307
308
309
310
# File 'lib/support_readiness/zendesk/views.rb', line 305

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