Class: Readiness::Repos::Views

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

Overview

Defines the class Views within the module Readiness::Repos.

Author:

  • Jason Colyer

Since:

  • 1.0.12

Class 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

Class Method Details

.compare(config, zendesk_client, location = 'data', verbose = false) ⇒ Hash

Compares the repo view files to the Zendesk instance 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)
diffs = Readiness::Repos::Views.compare(config, client, 'views/data', false)
pp diffs[:updates.count]
# => 5
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)
diffs = Readiness::Repos::Views.compare(config, client, 'views/data', true)
# => Detailed diff of Status::Close::Close solved tickets after 7 days
# => - @default Repo:
# => false
# =>   @default Zendesk:
# => true
# => Compare report:
# => - Creates: 0
# => - Updates: 1
pp diffs[:updates.count]
# => 1

Parameters:

  • config (Object)

    An instance of Zendesk::Configuration

  • zendesk_client (Object)

    An instance of Zendesk::Client

  • location (String) (defaults to: 'data')

    The location (relative or absolute) of the repo’s data folder

  • verbose (Boolean) (defaults to: false)

    Whether you want a detailed report or not

Returns:

  • (Hash)

Author:

  • Jason Colyer

Since:

  • 1.0.12



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/support_readiness/repos/views.rb', line 51

def self.compare(config, zendesk_client, location = 'data', verbose = false)
  @zendesk_client = zendesk_client
  diffs = {
    updates: [],
    creates: []
  }
  from_repo = gather(config, location)
  from_zendesk = Zendesk::Views.list(zendesk_client)
  from_repo.each do |repo|
    zd = from_zendesk.detect { |z| z.title == repo.title }
    if zd.nil?
      diffs[:creates].push(repo)
    else
      comparable = zd.dup
      comparable.id = nil
      comparable.conditions = zd.conditions.sort.to_h
      diffs[:updates].push(update_object(repo, zd)) if to_clean_json(repo) != to_clean_json(comparable)
      detailed_diff(repo, comparable) if verbose && to_clean_json(repo) != to_clean_json(comparable)
    end
  end
  report_diffs(diffs) if verbose
  diffs
end

.convert_columns(config, object, conversion) ⇒ Object

Performs conversions for a repo file using managed content

Author:

  • Jason Colyer

Since:

  • 1.0.12



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/support_readiness/repos/views.rb', line 237

def self.convert_columns(config, object, conversion)
  array = []
  object['fields'].each do |f|
    c = conversion.detect { |c| c['name'] == f }
    o = {
      "id" => c['id'],
      "title" => c['name'],
      "type" => nil,
      "url" => nil,
      "filterable" => filterable?(c),
      "sortable" => sortable?(c),
    }
    o['type'] = c['type'] if c['custom']
    o['url'] = "#{config.url}/ticket_fields/#{c['id']}.json" if c['custom']
    array.push(o.compact)
  end
  array
end

.convert_custom_fields(config, object, conversion) ⇒ Object

Performs conversions for a repo file using managed content

Author:

  • Jason Colyer

Since:

  • 1.0.12



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/support_readiness/repos/views.rb', line 282

def self.convert_custom_fields(config, object, conversion)
  array = []
  object['fields'].each do |f|
    c = conversion.detect { |c| c['name'] == f }
    next unless c['custom']
    o = {
      "id" => c['id'],
      "title" => c['name'],
      "type" => c['type'],
      "url" => "#{config.url}/ticket_fields/#{c['id']}.json",
      "filterable" => filterable?(c),
      "sortable" => sortable?(c)
    }
    array.push(o.compact)
  end
  array.sort_by { |a| a['id'] }
end

.convert_fields(object, conversion) ⇒ Object

Performs conversions for a repo file using managed content

Author:

  • Jason Colyer

Since:

  • 1.0.12



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/support_readiness/repos/views.rb', line 261

def self.convert_fields(object, conversion)
  array = []
  object['fields'].each do |f|
    c = conversion.detect { |c| c['name'] == f }
    next if c['custom']
    o = {
      "id" => c['id'],
      "title" => c['name'],
      "filterable" => filterable?(c),
      "sortable" => sortable?(c),
    }
    array.push(o)
  end
  array
end

.convert_group(config, object, conversion) ⇒ Object

Performs conversions for a repo file using managed content

Author:

  • Jason Colyer

Since:

  • 1.0.12



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/support_readiness/repos/views.rb', line 328

def self.convert_group(config, object, conversion)
  return nil if object['group_by'].nil?

  c = conversion.detect { |c| c['name'] == object['group_by'] }
  group = {
    "id" => c['id'],
    "title" => c['name'],
    "type" => nil,
    "url" => nil,
    "filterable" => filterable?(c),
    "sortable" => sortable?(c),
    "order" => object['group_order']
  }
  group['type'] = c['type'] if c['custom']
  group['url'] = "#{config.url}/ticket_fields/#{c['id']}.json" if c['custom']
  group.compact
end

.convert_managed_content(config, object) ⇒ Object

Performs conversions for a repo file if using managed content

Parameters:

  • config (Object)

    An instance of Zendesk::Configuration

  • object (Hash)

    The Hash derived from parsing a YAML file

Author:

  • Jason Colyer

Since:

  • 1.0.12



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/support_readiness/repos/views.rb', line 213

def self.convert_managed_content(config, object)
  path = "#{@location}/managed_content/#{object['active'] ? '' : 'in'}active/#{object['title'].gsub('/', '_')}.yaml"
  handle_request_error(1, 'Repos', 404, { action: 'Get managed content', id: object['title'] }) unless File.exist?(path)
  conversion = YAML.safe_load_file("#{@location}/managed_content/fields.yaml")
  data = YAML.safe_load_file(path)
  object['execution'] = {
    "group_by" => (data['group_by'].nil? ? nil : conversion.detect { |c| c['name'] == data['group_by'] }['id'].to_s),
    "group_order" => data['group_order'],
    "sort_by" => (data['sort_by'].nil? ? nil : conversion.detect { |c| c['name'] == data['sort_by'] }['id'].to_s),
    "sort_order" => data['sort_order'],
    "group" => convert_group(config, data, conversion),
    "sort" => convert_sort(config, data, conversion),
    "columns" => convert_columns(config, data, conversion),
    "fields" => convert_fields(data, conversion),
    "custom_fields" => convert_custom_fields(config, data, conversion),
  }
  object
end

.convert_sort(config, object, conversion) ⇒ Object

Performs conversions for a repo file using managed content

Author:

  • Jason Colyer

Since:

  • 1.0.12



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/support_readiness/repos/views.rb', line 305

def self.convert_sort(config, object, conversion)
  return nil if object['sort_by'].nil?

  c = conversion.detect { |c| c['name'] == object['sort_by'] }
  sort = {
    "id" => c['id'],
    "title" => c['name'],
    "type" => nil,
    "url" => nil,
    "filterable" => filterable?(c),
    "sortable" => sortable?(c),
    "order" => object['sort_order']
  }
  sort['type'] = c['type'] if c['custom']
  sort['url'] = "#{config.url}/ticket_fields/#{c['id']}.json" if c['custom']
  sort.compact
end

.detailed_diff(repo, zendesk) ⇒ Object

Outputs a comparison report

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.12



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/support_readiness/repos/views.rb', line 94

def self.detailed_diff(repo, zendesk)
  puts "Detailed diff of #{repo.title}"
  repo.instance_variables.each do |v|
    next if v == :@id

    if repo.instance_variable_get(v) != zendesk.instance_variable_get(v)
      puts "- #{v} Repo:"
      pp repo.instance_variable_get(v)
      puts "  #{v} Zendesk:"
      pp zendesk.instance_variable_get(v)
    end
  end
end

.filterable?(data) ⇒ Boolean

Performs conversions for a repo file using managed content

Returns:

  • (Boolean)

Author:

  • Jason Colyer

Since:

  • 1.0.12



360
361
362
363
364
365
# File 'lib/support_readiness/repos/views.rb', line 360

def self.filterable?(data)
  return false if data['name'] == 'Requested'
  return false if data['type'] == 'text'

  true
end

.gather(config, location = 'data') ⇒ Array

Parses repo view files

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'
repo = Readiness::Repos::Views.gather(config, 'views/data')
pp repo.count
# => 35

Parameters:

  • config (Object)

    An instance of Zendesk::Configuration

  • location (String) (defaults to: 'data')

    The location (relative or absolute) of the repo’s data folder

Returns:

  • (Array)

Author:

  • Jason Colyer

Since:

  • 1.0.12



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/support_readiness/repos/views.rb', line 139

def self.gather(config, location = 'data')
  @brands = Readiness::Zendesk::Brands.list(@zendesk_client)
  @groups = Readiness::Zendesk::Groups.list(@zendesk_client)
  @schedules = Readiness::Zendesk::Schedules.list(@zendesk_client)
  @satisfaction_reasons = Readiness::Zendesk::SatisfactionReasons.list(@zendesk_client)
  @ticket_fields = Readiness::Zendesk::TicketFields.list(@zendesk_client)
  @ticket_forms = Readiness::Zendesk::TicketForms.list(@zendesk_client)
  @errors = []
  @location = location
  array = []
  Dir["#{@location}/{active,inactive}/*.yaml"].each do |f|
    object = YAML.safe_load_file(f)
    object = convert_managed_content(config, object)
    object = convert_view_names_to_ids(object)
    validity_check(f, object)
    object['id'] = nil
    array.push(Zendesk::Views.new(object))
  end
  repo_check(array)
  report_errors unless @errors.count.zero?
  array
end

.repo_check(objects) ⇒ Object

Performs basic checks on the repo

Parameters:

  • objects (Array)

    The Array of Hashes derived from parsing the repo files

Author:

  • Jason Colyer

Since:

  • 1.0.12



197
198
199
200
201
202
203
204
# File 'lib/support_readiness/repos/views.rb', line 197

def self.repo_check(objects)
  duplicate_names = objects.group_by { |o| o.title }.select { |k, v| v.size > 1 }.map(&:first)
  unless duplicate_names.count.zero?
    duplicate_names.each do |d|
      @errors.push("The title '#{d}' is used in multiple files")
    end
  end
end

.report_diffs(diffs) ⇒ Object

Outputs a comparison report

Parameters:

  • diffs (Hash)

    The returned value of compare

Author:

  • Jason Colyer

Since:

  • 1.0.12



81
82
83
84
85
# File 'lib/support_readiness/repos/views.rb', line 81

def self.report_diffs(diffs)
  puts 'Compare report:'
  puts "- Creates: #{diffs[:creates].count}"
  puts "- Updates: #{diffs[:updates].count}"
end

.report_errorsObject

Outputs an error report and exits with a status code of 1

Author:

  • Jason Colyer

Since:

  • 1.0.12



167
168
169
170
171
172
173
174
# File 'lib/support_readiness/repos/views.rb', line 167

def self.report_errors
  puts 'The following errors were found in the repo files:'
  @errors.each do |e|
    puts "- #{e}"
  end
  puts 'Rectify the errors and retry. We cannot proceed with those errors occurring.'
  exit 1
end

.sortable?(data) ⇒ Boolean

Performs conversions for a repo file using managed content

Returns:

  • (Boolean)

Author:

  • Jason Colyer

Since:

  • 1.0.12



351
352
353
# File 'lib/support_readiness/repos/views.rb', line 351

def self.sortable?(data)
  true
end

.update_object(repo, zendesk) ⇒ Object

Creates an instance of Zendesk::Views to use for updates

Parameters:

Returns:

  • (Object)

Author:

  • Jason Colyer

Since:

  • 1.0.12



116
117
118
119
120
# File 'lib/support_readiness/repos/views.rb', line 116

def self.update_object(repo, zendesk)
  object = repo
  object.id = zendesk.id
  object
end

.validity_check(file, object) ⇒ Object

Performs basic checks on a repo file

Parameters:

  • file (String)

    The path to the repo file

  • object (Hash)

    The Hash derived from parsing a YAML file

Author:

  • Jason Colyer

Since:

  • 1.0.12



183
184
185
186
187
188
189
# File 'lib/support_readiness/repos/views.rb', line 183

def self.validity_check(file, object)
  folder = file.split("#{@location}/").last.split('/').first
  @errors.push("Missing position: #{file}") if object['position'].nil?
  @errors.push("Missing title: #{file}") if object['title'].nil?
  @errors.push("Inactive view in active folder: #{file}") if folder == 'active' && !object['active']
  @errors.push("Active view in inactive folder: #{file}") if folder == 'inactive' && object['active']
end