Class: Readiness::Repos::DynamicContent

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

Overview

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

Author:

  • Jason Colyer

Since:

  • 1.0.74

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(zendesk_client, location = 'data', verbose = false) ⇒ Hash

Compares the repo dynamic content files to the Zendesk instance dynamic content

Parameters:

  • 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.74



22
23
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/support_readiness/repos/dynamic_content.rb', line 22

def self.compare(zendesk_client, location = 'data', verbose = false)
  @zendesk_client = zendesk_client
  @locales = Zendesk::Locales.list(zendesk_client)
  @from_repo = gather(location)
  @from_zendesk = Zendesk::DynamicContent.list(zendesk_client)
  @diffs = {
    item_creates: [],
    item_updates: [],
    variant_creates: [],
    variant_updates: [],
    variant_deletes: []
  }
  @from_repo.each do |repo|
    zd = @from_zendesk.detect { |z| z.name == repo[:item].name }
    if zd.nil?
      @diffs[:item_creates].push(repo)
    else
      comparable_zd = zd.dup
      comparable_zd.id = nil
      comparable_zd.outdated = nil
      if to_clean_json(comparable_zd) != to_clean_json(repo[:item])
        @diffs[:item_updates].push(item_update_object(repo[:item], zd))
        detailed_diff(repo[:item], comparable_zd) if verbose
      end
      zd_variants = Readiness::Zendesk::DynamicContent.variants(zendesk_client, zd)
      repo[:variants].each do |v|
        zd_variant = zd_variants.detect { |z| z.locale_id == v.locale_id }
        if zd_variant.nil?
          object = {
            item: repo[:item].name,
            variant: v
          }
          @diffs[:variant_creates].push(object)
        else
          comparable_zd_variant = zd_variant.dup
          comparable_zd_variant.id = nil
          comparable_zd_variant.outdated = nil
          if to_clean_json(comparable_zd_variant) != to_clean_json(v)
            @diffs[:variant_updates].push(variant_update_object(repo[:item], v, zd_variant))
            detailed_variant_diff(repo[:item], v, comparable_zd_variant) if verbose
          end
        end
        zd_variants.each do |z|
          unless repo[:variants].map { |v| v.locale_id }.include? z.locale_id
            object = {
              item: repo[:item].name,
              variant: z
            }
            @diffs[:variant_deletes].push(object)
          end
        end
      end
    end
  end
  report_diffs if verbose
  @diffs
end

.convert_names_to_ids(dynamic_content) ⇒ Hash

Converts various user Strings to the proper ID value

Parameters:

  • content (Hash)

    The dynamic content information from the YAML file

Returns:

  • (Hash)

Author:

  • Jason Colyer

Since:

  • 1.0.100



213
214
215
216
217
218
219
220
221
222
# File 'lib/support_readiness/repos/dynamic_content.rb', line 213

def self.convert_names_to_ids(dynamic_content)
  dynamic_content['variants'].each_with_index do |v, i|
    if v['content'] =~ /\[Form\:\ .*\]/
      name = v['content'].split('[Form: ').last.split(']').first
      ticket_form = Readiness::Zendesk::TicketForms.find_by_name(@zendesk_client, name, @ticket_forms)
      dynamic_content['variants'][i]['content'] = v['content'].gsub("[Form: #{ticket_form.name}]", ticket_form.id.to_s)
    end
  end
  dynamic_content
end

.detailed_diff(repo, zendesk) ⇒ Object

Outputs a detailed comparison report

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.74



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/support_readiness/repos/dynamic_content.rb', line 101

def self.detailed_diff(repo, zendesk)
  puts "Detailed diff of #{repo.name}"
  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

.detailed_variant_diff(item, repo, zendesk) ⇒ Object

Outputs a detailed comparison report

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.74



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/support_readiness/repos/dynamic_content.rb', line 123

def self.detailed_variant_diff(item, repo, zendesk)
  puts "Detailed diff of variant for '#{item.name}' with locale ID #{repo.locale_id}"
  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

.gather(location) ⇒ Array

Parses repo user field 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'
client = Readiness::Zendesk::Client.new(config)
repo = Readiness::Repos::DynamicContent.gather('data')
pp repo.count
# => 2

Parameters:

  • location (String)

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

Returns:

  • (Array)

Author:

  • Jason Colyer

Since:

  • 1.0.74



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/support_readiness/repos/dynamic_content.rb', line 186

def self.gather(location)
  @ticket_fields = Readiness::Zendesk::TicketFields.list(@zendesk_client)
  @ticket_forms = Readiness::Zendesk::TicketForms.list(@zendesk_client)
  @errors = []
  @location = location
  array = []
  Dir["#{@location}/*.yaml"].each do |f|
    object = YAML.safe_load_file(f)
    object = convert_names_to_ids(object)
    data = {
      item: Zendesk::DynamicContent.new(object),
      variants: object['variants'].map { |v| Zendesk::DynamicContentVariants.new(v) }
    }
    array.push(data)
  end
  repo_check(array)
  report_errors unless @errors.count.zero?
  array
end

.item_update_object(repo, zendesk) ⇒ Object

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

Parameters:

Returns:

  • (Object)

Author:

  • Jason Colyer

Since:

  • 1.0.74



145
146
147
148
149
# File 'lib/support_readiness/repos/dynamic_content.rb', line 145

def self.item_update_object(repo, zendesk)
  object = repo
  object.id = zendesk.id
  object
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.74



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/support_readiness/repos/dynamic_content.rb', line 244

def self.repo_check(objects)
  duplicate_names = objects.map { |o| o[:item] }.group_by { |o| o.name }.select { |k, v| v.size > 1 }.map(&:first)
  unless duplicate_names.count.zero?
    duplicate_names.each do |d|
      @errors.push("The item '#{d}' is used in multiple files")
    end
  end
  objects.select { |o| o[:variants].count.zero? }.each do |o|
    @errors.push("The item '#{o[:item].name}' has no variants")
  end
  objects.each do |o|
    @errors.push("Bad default locale id for the item '#{o[:item].name}'") unless @locales.map { |l| l.id }.include? o[:item].default_locale_id
    @errors.push("The item '#{o[:item].name}' has no default variant") unless o[:variants].detect { |v| v.default }
    o[:variants].each_with_index do |v, i|
      @errors.push("Variant #{i + 1} on item '#{o[:item].name}' has a bad locale") unless @locales.map { |l| l.id }.include? v.locale_id
    end
  end
end

.report_diffsObject

Outputs a comparison report

Author:

  • Jason Colyer

Since:

  • 1.0.74



85
86
87
88
89
90
91
92
# File 'lib/support_readiness/repos/dynamic_content.rb', line 85

def self.report_diffs
  puts 'Compare report:'
  puts "- Item Creates:    #{@diffs[:item_creates].count}"
  puts "- Item Updates:    #{@diffs[:item_updates].count}"
  puts "- Variant Creates: #{@diffs[:variant_creates].count}"
  puts "- Variant Updates: #{@diffs[:variant_updates].count}"
  puts "- Variant Deletes: #{@diffs[:variant_deletes].count}"
end

.report_errorsObject

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

Author:

  • Jason Colyer

Since:

  • 1.0.74



229
230
231
232
233
234
235
236
# File 'lib/support_readiness/repos/dynamic_content.rb', line 229

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

.variant_update_object(item, repo, zendesk) ⇒ Hash

Creates a Hash to use for updates

Parameters:

Returns:

  • (Hash)

Author:

  • Jason Colyer

Since:

  • 1.0.74



160
161
162
163
164
165
166
167
# File 'lib/support_readiness/repos/dynamic_content.rb', line 160

def self.variant_update_object(item, repo, zendesk)
  object = {
    item: item.name,
    variant: repo
  }
  object[:variant].id = zendesk.id
  object
end