Class: Readiness::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/support_readiness/client.rb

Overview

Defines the class Client within the module Readiness.

Author:

  • Jason Colyer

Since:

  • 1.0.0

Direct Known Subclasses

Readiness::Calendly::OrganizationMemberships, Readiness::Calendly::Organizations, Readiness::Calendly::Users, GitLab::Epics, GitLab::Groups, GitLab::Issues, GitLab::Jobs, GitLab::Markdown, GitLab::MergeRequests, GitLab::Namespaces, GitLab::Pipelines, GitLab::ProjectWebhooks, GitLab::Projects, GitLab::Repositories, GitLab::Users, GoogleCalendar::Events, GoogleSheets::Spreadsheets, Mailgun::Bounces, Mailgun::Emails, Pagerduty::EscalationPolicies, Pagerduty::Schedules, Pagerduty::Services, Pagerduty::Users, Repos::Articles, Repos::Automations, Repos::DynamicContent, Repos::Groups, Repos::Macros, Repos::OrganizationFields, Repos::SLAs, Repos::Themes, Repos::TicketFields, Repos::TicketForms, Repos::Triggers, Repos::UserFields, Repos::Views, Repos::ZendeskSalesforceSync, Salesforce::Accounts, Salesforce::Cases, Salesforce::Contacts, Salesforce::Queries, Salesforce::SubscriptionDefinitions, Slack::Messages, SupportSuperFormProcessor::CreateArticle, SupportSuperFormProcessor::CreateMacro, SupportSuperFormProcessor::DisableUSGov, SupportSuperFormProcessor::EditMacro, SupportSuperFormProcessor::EnableUSGov, SupportSuperFormProcessor::GlobalIRDotcomSubscriptionsBillingEntityChange, SupportSuperFormProcessor::GlobalIRDotcomSubscriptionsExtension, SupportSuperFormProcessor::GlobalIRDotcomSubscriptionsInvestigate, SupportSuperFormProcessor::GlobalIRDotcomSubscriptionsLink, SupportSuperFormProcessor::GlobalIRDotcomSubscriptionsNFR, SupportSuperFormProcessor::GlobalIRDotcomSubscriptionsResetSeatQSR, SupportSuperFormProcessor::GlobalIRDotcomTrialsEdit, SupportSuperFormProcessor::GlobalIRDotcomTrialsExtension, SupportSuperFormProcessor::GlobalIRDotcomTrialsOverPlan, SupportSuperFormProcessor::GlobalIRHackeroneLicense, SupportSuperFormProcessor::GlobalIROEMLicense, SupportSuperFormProcessor::GlobalIROrderManagement, SupportSuperFormProcessor::GlobalIROther, SupportSuperFormProcessor::GlobalIRProvisionFailure, SupportSuperFormProcessor::GlobalIRSMSubscriptionsDidNotReceive, SupportSuperFormProcessor::GlobalIRSMSubscriptionsMultiyear, SupportSuperFormProcessor::GlobalIRSMSubscriptionsNFR, SupportSuperFormProcessor::GlobalIRSMSubscriptionsResend, SupportSuperFormProcessor::GlobalIRSMSubscriptionsSCL, SupportSuperFormProcessor::GlobalIRSMSubscriptionsTemp, SupportSuperFormProcessor::GlobalIRSMTrialsEdit, SupportSuperFormProcessor::GlobalIRSMTrialsExtension, SupportSuperFormProcessor::GlobalIRSMTrialsNew, SupportSuperFormProcessor::GlobalIRWiderCommunityLicense, SupportSuperFormProcessor::GlobalSetCollaboriationID, SupportSuperFormProcessor::GlobalSetOrgASE, SupportSuperFormProcessor::GlobalToggleEscalation, SupportSuperFormProcessor::GratisRequest, SupportSuperFormProcessor::GratisSupportExtension, SupportSuperFormProcessor::GratisSupportFormerCustomer, SupportSuperFormProcessor::GratisSupportMigration, SupportSuperFormProcessor::GratisSupportOther, SupportSuperFormProcessor::GratisSupportProspect, SupportSuperFormProcessor::GratisSupportUpgrade, SupportSuperFormProcessor::NamespaceAvailability, SupportSuperFormProcessor::PagerdutyShadowModification, SupportSuperFormProcessor::SARequestForSupport, SupportSuperFormProcessor::Shared, SupportSuperFormProcessor::TeamMemberLicense, SupportSuperFormProcessor::TwoFAExemption, SupportSuperFormProcessor::USGovIRORderManagement, SupportSuperFormProcessor::USGovIROther, SupportSuperFormProcessor::USGovIRSMSubscriptionsDidNotReceive, SupportSuperFormProcessor::USGovIRSMSubscriptionsMultiyear, SupportSuperFormProcessor::USGovIRSMSubscriptionsNFR, SupportSuperFormProcessor::USGovIRSMSubscriptionsResend, SupportSuperFormProcessor::USGovIRSMSubscriptionsSCL, SupportSuperFormProcessor::USGovIRSMSubscriptionsTemp, SupportSuperFormProcessor::USGovIRSMTrialsEdit, SupportSuperFormProcessor::USGovIRSMTrialsExtension, SupportSuperFormProcessor::USGovIRSMTrialsNew, SupportSuperFormProcessor::USGovSetCollaboriationID, SupportSuperFormProcessor::USGovSetOrgASE, TicketProcessor::AccountBlocked, TicketProcessor::CMPCreation, TicketProcessor::CodeRequestAdvancedSAST, TicketProcessor::EmailSuppressions, TicketProcessor::LinkTagger, TicketProcessor::LockedAccount, TicketProcessor::Namesquatting, TicketProcessor::OrganizationNotes, TicketProcessor::STAR, TicketProcessor::TwoFARemoval, TicketProcessor::Weighting, Zendesk::AppJobStatuses, Zendesk::Apps, Zendesk::Articles, Zendesk::Automations, Zendesk::Brands, Zendesk::DynamicContent, Zendesk::DynamicContentVariants, Zendesk::GroupMemberships, Zendesk::Groups, Zendesk::JobStatuses, Zendesk::Locales, Zendesk::Macros, Zendesk::OAuthClients, Zendesk::OAuthTokens, Zendesk::OrganizationFields, Zendesk::OrganizationMemberships, Zendesk::Organizations, Zendesk::RequesterRoles, Zendesk::Roles, Zendesk::SLAs, Zendesk::SatisfactionReasons, Zendesk::Schedules, Zendesk::Search, Zendesk::Targets, Zendesk::ThemeJobStatuses, Zendesk::Themes, Zendesk::TicketFieldOptions, Zendesk::TicketFields, Zendesk::TicketForms, Zendesk::TicketUserTypes, Zendesk::Tickets, Zendesk::TriggerCategories, Zendesk::Triggers, Zendesk::UserFieldOptions, Zendesk::UserFields, Zendesk::Users, Zendesk::ViaTypes, Zendesk::Views, Zendesk::Webhooks

Class Method Summary collapse

Class Method Details

.auth_error(system) ⇒ Object

Handles authentication errors from making Faraday requests.

Parameters:

  • system (String)

    The system the error occurred on

Author:

  • Jason Colyer

Since:

  • 1.0.0



117
118
119
120
# File 'lib/support_readiness/client.rb', line 117

def self.auth_error(system)
  puts "Unable to authenticate to #{system}. Check your configuration and try again."
  exit 1
end

.bad_request_error(system, params) ⇒ Object

Handles 400 bad request errors from making Faraday requests.

Parameters:

  • system (String)

    The system the error occurred on

  • params (Hash)

    A Hash of information about the request

Author:

  • Jason Colyer

Since:

  • 1.0.0



129
130
131
132
# File 'lib/support_readiness/client.rb', line 129

def self.bad_request_error(system, params)
  puts "Action '#{params[:action]}' to ID #{params[:id]} on #{system} returned a 400 error"
  exit 1
end

.convert_actions(object) ⇒ Object

Convert user friendly Strings within actions into Zendesk values for standard objects

Parameters:

  • object (Object)

    The object to parse

Author:

  • Jason Colyer

Since:

  • 1.0.112



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/support_readiness/client.rb', line 343

def self.convert_actions(object)
  object['actions'].each_with_index do |a, i|
    if a['field'] == 'brand_id'
      brand = Readiness::Zendesk::Brands.find_by_name(@zendesk_client, a['value'], @brands)
      unless brand.nil?
        object['actions'][i]['value'] = brand.id.to_s
      end
    end
    if a['field'] == 'group_id'
      group = Readiness::Zendesk::Groups.find_by_name(@zendesk_client, a['value'], @groups)
      unless group.nil?
        object['actions'][i]['value'] = group.id.to_s
      end
    end
    if a['field'] == 'organization_id'
      code = a['value']
      next if code.empty?
      next if %r{^[0-9]+$}.match code

      search = Readiness::Zendesk::Search.organizations(@zendesk_client, "salesforce_id:#{code}")
      if search.count == 1
        object['actions'][i]['value'][0] = search.first.id.to_s
      end
    end
    if a['field'] == 'notification_target'
      if a['value'].first =~ /^Target\:\ /
        name = a['value'].first.split('Target: ').last
        target = Readiness::Zendesk::Targets.find_by_name(@zendesk_client, name, @targets)
        object['actions'][i]['value'][0] = target.id.to_s
      end
    end
    if a['field'] == 'role'
      role = Readiness::Zendesk::TicketUserTypes.find_by_name(@zendesk_client, c['value'])
      unless role.nil?
        object['actions'][i]['value'] = role.id.to_s
      end
    end
    if a['field'] == 'assignee_id'
      unless ['', 'current_user', 'requester_id'].include? a['value']
        search = Readiness::Zendesk::Search.users(@zendesk_client, "email:#{a['value']}")
        if search.count == 1
          object['actions'][i]['value'] = search.first.id.to_s
        else
          puts "Cannot find user #{a['value']} for #{object['title']}"
          exit 1
        end
      end
    end
    if a['field'] == 'notification_webhook'
      if a['value'].first =~ /^Webhook\:\ /
        name = a['value'].first.split('Webhook: ').last
        webhook = Readiness::Zendesk::Webhooks.find_by_name(@zendesk_client, name, @webhooks)
        object['actions'][i]['value'][0] = webhook.id.to_s
      end
    end
    if a['field'] == 'ticket_form_id'
      if a['value'] =~ /Form\:\ /
        name = a['value'].split('Form: ').last
        ticket_form = Readiness::Zendesk::TicketForms.find_by_name(@zendesk_client, name, @ticket_forms)
        object['actions'][i]['value'] = ticket_form.id.to_s
      end
    end
    if a['field'] =~ /Field\:\ /
      name = a['field'].split('Field: ').last
      ticket_field = Readiness::Zendesk::TicketFields.find_by_name(@zendesk_client, name, @ticket_fields)
      object['actions'][i]['field'] = "custom_fields_#{ticket_field.id}"
    end
    if a['field'] == 'set_schedule' && a['value'].to_s =~ /Schedule\:\ /
      name = a['value'].split('Schedule: ').last
      schedule = Readiness::Zendesk::Schedules.find_by_name(@zendesk_client, name, @schedules)
      object['actions'][i]['value'] = schedule.id.to_s
    end
  end
  object
end

.convert_conditions(object, attribute_name = 'conditions') ⇒ Object

Convert user friendly Strings within conditions into Zendesk values for standard objects

Parameters:

  • object (Object)

    The object to parse

Author:

  • Jason Colyer

Since:

  • 1.0.112



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/support_readiness/client.rb', line 252

def self.convert_conditions(object, attribute_name = 'conditions')
  %w[any all].each do |type|
    object[attribute_name][type].each_with_index do |c, i|
      if c['field'] == 'brand_id'
        brand = Readiness::Zendesk::Brands.find_by_name(@zendesk_client, c['value'], @brands)
        unless brand.nil?
          object[attribute_name][type][i]['value'] = brand.id.to_s
        end
      end
      if c['field'] == 'group_id'
        group = Readiness::Zendesk::Groups.find_by_name(@zendesk_client, c['value'], @groups)
        unless group.nil?
          object[attribute_name][type][i]['value'] = group.id.to_s
        end
      end
      if c['field'] == 'organization_id'
        code = c['value'].to_s
        next if code.empty?
        next if %r{^[0-9]+$}.match code

        search = Readiness::Zendesk::Search.organizations(@zendesk_client, "salesforce_id:#{code}")
        if search.count == 1
          object[attribute_name][type][i]['value'] = search.first.id.to_s
        end
      end
      if c['field'] == 'satisfaction_reason_code'
        reason = Readiness::Zendesk::SatisfactionReasons.find_by_name(@zendesk_client, c['value'], @satisfaction_reasons)
        unless reason.nil?
          object[attribute_name][type][i]['value'] = reason.reason_code.to_s
        end
      end
      if c['field'] == 'via_id'
        via_type = Readiness::Zendesk::ViaTypes.find_by_name(c['value'])
        unless via_type.nil?
          object[attribute_name][type][i]['value'] = via_type.id.to_s
        end
      end
      if c['field'] == 'requester_role'
        role = Readiness::Zendesk::RequesterRoles.find_by_name(c['value'])
        unless role.nil?
          object[attribute_name][type][i]['value'] = role.id.to_s
        end
      end
      if c['field'] == 'role'
        role = Readiness::Zendesk::TicketUserTypes.find_by_name(@zendesk_client, c['value'])
        unless role.nil?
          object[attribute_name][type][i]['value'] = role.id.to_s
        end
      end
      if c['field'] == 'assignee_id'
        unless ['', 'current_user', 'requester_id'].include? c['value']
          search = Readiness::Zendesk::Search.users(@zendesk_client, "email:#{c['value']}")
          if search.count == 1
            object[attribute_name][type][i]['value'] = search.first.id.to_s
          end
        end
      end
      if c['field'] == 'ticket_form_id'
        if c['value'].to_s =~ /Form\:\ /
          name = c['value'].split('Form: ').last
          ticket_form = Readiness::Zendesk::TicketForms.find_by_name(@zendesk_client, name, @ticket_forms)
          object[attribute_name][type][i]['value'] = ticket_form.id.to_s
        end
      end
      if c['field'].to_s =~ /Field\:\ /
        name = c['field'].split('Field: ').last
        ticket_field = Readiness::Zendesk::TicketFields.find_by_name(@zendesk_client, name, @ticket_fields)
        if attribute_name == 'filter'
          object[attribute_name][type][i]['field'] = "ticket_fields_#{ticket_field.id}"
          field_option = ticket_field.custom_field_options.detect { |o| o.value == c['value'] }
          object[attribute_name][type][i]['value'] = field_option.id.to_s unless field_option.nil?
        else
          object[attribute_name][type][i]['field'] = "custom_fields_#{ticket_field.id}"
        end
      end
      if c['value'].to_s =~ /Schedule\:\ /
        name = c['value'].split('Schedule: ').last
        schedule = Readiness::Zendesk::Schedules.find_by_name(@zendesk_client, name, @schedules)
        object[attribute_name][type][i]['value'] = schedule.id.to_s
      end
    end
  end
  object
end

.convert_standard_names_to_ids(object) ⇒ Object

Convert user friendly Strings into Zendesk values for standard objects

Parameters:

  • object (Object)

    The object to parse

Author:

  • Jason Colyer

Since:

  • 1.0.112



241
242
243
244
# File 'lib/support_readiness/client.rb', line 241

def self.convert_standard_names_to_ids(object)
  new_object = convert_conditions(object)
  convert_actions(new_object)
end

.convert_ticket_form_agent_conditions(object) ⇒ Object

Convert user friendly Strings into Zendesk values for ticket form agent conditions

Parameters:

  • object (Object)

    The ticket form to parse

Author:

  • Jason Colyer

Since:

  • 1.0.118



506
507
508
509
510
511
512
513
514
515
516
# File 'lib/support_readiness/client.rb', line 506

def self.convert_ticket_form_agent_conditions(object)
  object['agent_conditions'].each_with_index do |c, ci|
    parent_field = Readiness::Zendesk::TicketFields.find_by_name(@zendesk_client, c['parent_field_id'], @ticket_fields)
    object['agent_conditions'][ci]['parent_field_id'] = parent_field.id unless parent_field.nil?
    c['child_fields'].each_with_index do |cf, cfi|
      child_field = Readiness::Zendesk::TicketFields.find_by_name(@zendesk_client, cf['id'], @ticket_fields)
      object['agent_conditions'][ci]['child_fields'][cfi]['id'] = child_field.id unless child_field.nil?
    end
  end
  object
end

.convert_ticket_form_brands(object) ⇒ Object

Convert user friendly Strings into Zendesk values for ticket form brands

Parameters:

  • object (Object)

    The ticket form to parse

Author:

  • Jason Colyer

Since:

  • 1.0.118



524
525
526
527
528
529
530
# File 'lib/support_readiness/client.rb', line 524

def self.convert_ticket_form_brands(object)
  object['restricted_brand_ids'] = object['restricted_brand_ids'].map do |b|
    brand = Readiness::Zendesk::Brands.find_by_name(@zendesk_client, b, @brands)
    brand.nil? ? b : brand.id
  end
  object
end

.convert_ticket_form_end_user_conditions(object) ⇒ Object

Convert user friendly Strings into Zendesk values for ticket form end_user conditions

Parameters:

  • object (Object)

    The ticket form to parse

Author:

  • Jason Colyer

Since:

  • 1.0.118



488
489
490
491
492
493
494
495
496
497
498
# File 'lib/support_readiness/client.rb', line 488

def self.convert_ticket_form_end_user_conditions(object)
  object['end_user_conditions'].each_with_index do |c, ci|
    parent_field = Readiness::Zendesk::TicketFields.find_by_name(@zendesk_client, c['parent_field_id'], @ticket_fields)
    object['end_user_conditions'][ci]['parent_field_id'] = parent_field.id unless parent_field.nil?
    c['child_fields'].each_with_index do |cf, cfi|
      child_field = Readiness::Zendesk::TicketFields.find_by_name(@zendesk_client, cf['id'], @ticket_fields)
      object['end_user_conditions'][ci]['child_fields'][cfi]['id'] = child_field.id unless child_field.nil?
    end
  end
  object
end

.convert_ticket_form_names_to_ids(object) ⇒ Object

Convert user friendly Strings into Zendesk values for ticket form objects

Parameters:

  • object (Object)

    The object to parse

Author:

  • Jason Colyer

Since:

  • 1.0.118



461
462
463
464
465
466
# File 'lib/support_readiness/client.rb', line 461

def self.convert_ticket_form_names_to_ids(object)
  new_object = covert_ticket_form_field_ids(object)
  new_object = convert_ticket_form_end_user_conditions(new_object)
  new_object = convert_ticket_form_agent_conditions(new_object)
  convert_ticket_form_brands(new_object)
end

.convert_view_names_to_ids(object) ⇒ Object

Convert user friendly Strings into Zendesk values for view objects

Parameters:

  • object (Object)

    The object to parse

Author:

  • Jason Colyer

Since:

  • 1.0.117



425
426
427
428
# File 'lib/support_readiness/client.rb', line 425

def self.convert_view_names_to_ids(object)
  new_object = convert_conditions(object)
  convert_view_restrictions(new_object)
end

.convert_view_restrictions(object) ⇒ Object

Convert user friendly Strings within restrictions into Zendesk values for view objects

Parameters:

  • object (Object)

    The object to parse

Author:

  • Jason Colyer

Since:

  • 1.0.117



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

def self.convert_view_restrictions(object)
  return object if object['restriction'].nil?
  
  if object['restriction']['type'] == 'Group'
    group = Readiness::Zendesk::Groups.find_by_name(@zendesk_client, object['restriction']['id'], @groups)
    object['restriction']['id'] = group.id unless group.nil?
    object['restriction']['ids'] = object['restriction']['ids'].map do |i|
      group = Readiness::Zendesk::Groups.find_by_name(@zendesk_client, i, @groups)
      group.nil? ? i : group.id
    end
  elsif object['restriction']['type'] == 'User'
    search = Readiness::Zendesk::Search.users(@zendesk_client, "email:#{object['restriction']['id']}")
    if search.count == 1
      object['restriction']['id'] = search.first.id
    end
  end
  object
end

.covert_ticket_form_field_ids(object) ⇒ Object

Convert user friendly Strings into Zendesk values for ticket form field IDs

Parameters:

  • object (Object)

    The ticket form to parse

Author:

  • Jason Colyer

Since:

  • 1.0.118



474
475
476
477
478
479
480
# File 'lib/support_readiness/client.rb', line 474

def self.covert_ticket_form_field_ids(object)
  object['ticket_field_ids'] = object['ticket_field_ids'].map do |f|
    field = Readiness::Zendesk::TicketFields.find_by_name(@zendesk_client, f, @ticket_fields)
    field.nil? ? f : field.id
  end
  object
end

.create_package!(item_list, input_dir = './') ⇒ String

Generates a Time String to be used in zip file generation

Parameters:

  • item_list (Array)

    An Array of items to zip

Returns:

  • (String)

    The path to the zip file

Author:

  • Jason Colyer

Since:

  • 1.0.12



163
164
165
166
167
168
169
170
171
# File 'lib/support_readiness/client.rb', line 163

def self.create_package!(item_list, input_dir = './')
  @output_file = "data/#{timestamp_filename}.zip"
  @input_dir = input_dir
  FileUtils.rm_f @output_file if File.exist? @output_file
  Zip::File.open(@output_file, create: true) do |zipfile|
    write_entries item_list, '', zipfile
  end
  @output_file
end

.erb_renderer(file) ⇒ Object

Generate an ERB renderer for a specific file

Examples:

require 'support_readiness'
renderer = Readiness::Client.erb_renderer('temp.erb')
body = renderer.result_with_hash(name: 'Jason')
pp body
# => "\n" + "Hello Jason!\n"

Parameters:

  • file (String)

    The path (relative or absolute) to the file to render

Author:

  • Jason Colyer

Since:

  • 1.0.12



231
232
233
# File 'lib/support_readiness/client.rb', line 231

def self.erb_renderer(file)
  ERB.new(File.read(file))
end

.handle_request_error(type, system, code, params = {}) ⇒ Object

Handles error occurred from making Faraday requests

Author:

  • Jason Colyer

Since:

  • 1.0.0



90
91
92
93
94
95
96
97
# File 'lib/support_readiness/client.rb', line 90

def self.handle_request_error(type, system, code, params = {})
  auth_error(system) if code == 401 || code == 403
  if type == 1
    bad_request_error(system, params) if code == 400
    not_found_error(system, params) if code == 404
    not_processible_error(system, params) if code == 422
  end
end

.not_found_error(system, params) ⇒ Object

Handles 404 Not found errors from making Faraday requests.

Parameters:

  • system (String)

    The system the error occurred on

  • params (Hash)

    A Hash of information about the request

Author:

  • Jason Colyer

Since:

  • 1.0.0



106
107
108
109
# File 'lib/support_readiness/client.rb', line 106

def self.not_found_error(system, params)
  puts "Action '#{params[:action]}' to ID #{params[:id]} on #{system} returned a 404 error"
  exit 1
end

.not_processible_error(system, params) ⇒ Object

Handles Unprocessable Content errors from making Faraday requests.

Parameters:

  • system (String)

    The system the error occurred on

  • params (Hash)

    A Hash of information about the request

Author:

  • Jason Colyer

Since:

  • 1.0.0



141
142
143
144
145
# File 'lib/support_readiness/client.rb', line 141

def self.not_processible_error(system, params)
  puts "Unable to '#{params[:action]}' on #{system}, as it returned the message:"
  pp params[:message]
  exit 1
end

.put_into_archive(disk_file_path, zipfile, zipfile_path) ⇒ Object

Adds an item to the zip file

Parameters:

  • disk_file_path (String)

    The path to the folder

  • zipfile (Object)

    The Zip object

  • zipfile_path (String)

    The path to the folder within the zip file

Author:

  • Jason Colyer

Since:

  • 1.0.12



215
216
217
# File 'lib/support_readiness/client.rb', line 215

def self.put_into_archive(disk_file_path, zipfile, zipfile_path)
  zipfile.add(zipfile_path, disk_file_path)
end

.recursively_deflate_directory(disk_file_path, zipfile, zipfile_path) ⇒ Object

Recusrively zips a folder

Parameters:

  • disk_file_path (String)

    The path to the folder

  • zipfile (Object)

    The Zip object

  • zipfile_path (String)

    The path to the folder within the zip file

Author:

  • Jason Colyer

Since:

  • 1.0.12



201
202
203
204
205
# File 'lib/support_readiness/client.rb', line 201

def self.recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
  zipfile.mkdir zipfile_path
  subdir = Dir.entries(disk_file_path) - %w[. ..]
  write_entries subdir, zipfile_path, zipfile
end

.timestamp_filenameObject

Generates a Time String to be used in zip file generation

Author:

  • Jason Colyer

Since:

  • 1.0.12



152
153
154
# File 'lib/support_readiness/client.rb', line 152

def self.timestamp_filename
  @name ||= Time.now.strftime('%Y%m%d%H%M%S%L')
end

.to_clean_json(object) ⇒ String

Converts an Object to a JSON String, removing nil values

Parameters:

  • object (Object)

Returns:

  • (String)

Author:

  • Jason Colyer

Since:

  • 1.0.0



34
35
36
37
# File 'lib/support_readiness/client.rb', line 34

def self.to_clean_json(object)
  hash = to_hash(object).compact
  hash.to_json
end

.to_clean_json_with_key(object, key) ⇒ String

Converts an Object to a JSON String with a leading key, removing nil values

Parameters:

  • object (Object)
  • key (String)

Returns:

  • (String)

Author:

  • Jason Colyer

Since:

  • 1.0.0



63
64
65
66
# File 'lib/support_readiness/client.rb', line 63

def self.to_clean_json_with_key(object, key)
  hash = { "#{key}": to_hash(object).compact }
  hash.to_json
end

.to_hash(object) ⇒ Hash

Converts an Object to a Hash

Parameters:

  • object (Object)

Returns:

  • (Hash)

Author:

  • Jason Colyer

Since:

  • 1.0.0



23
24
25
# File 'lib/support_readiness/client.rb', line 23

def self.to_hash(object)
  Hash[object.instance_variables.map { |name| [name.to_s[1..-1], object.instance_variable_get(name)] }]
end

.to_nearly_clean_json(object, keep_nil) ⇒ String

Converts an Object to a JSON String, removing nil values except for ones defined in an Array

Parameters:

  • object (Object)
  • keep_nil (Array)

    An array of keys to keep as nil in the final return

Returns:

  • (String)

Author:

  • Jason Colyer

Since:

  • 1.0.9



47
48
49
50
51
52
53
# File 'lib/support_readiness/client.rb', line 47

def self.to_nearly_clean_json(object, keep_nil)
  hash = to_hash(object).compact
  keep_nil.each do |k|
    hash[key.to_sym][k] = nil unless hash[key.to_sym].keys.include? k
  end
  hash.to_json
end

.to_nearly_clean_json_with_key(object, key, keep_nil) ⇒ String

Converts an Object to a JSON String with a leading key, removing nil values except for ones defined in an Array

Parameters:

  • object (Object)
  • key (String)
  • keep_nil (Array)

    An array of keys to keep as nil in the final return

Returns:

  • (String)

Author:

  • Jason Colyer

Since:

  • 1.0.9



77
78
79
80
81
82
83
# File 'lib/support_readiness/client.rb', line 77

def self.to_nearly_clean_json_with_key(object, key, keep_nil)
  hash = { "#{key}": to_hash(object).compact }
  keep_nil.each do |k|
    hash[key.to_sym][k] = nil unless hash[key.to_sym].keys.include? k
  end
  hash.to_json
end

.to_param_string(parms) ⇒ Object

Since:

  • 1.0.0



11
12
13
14
15
# File 'lib/support_readiness/client.rb', line 11

def self.to_param_string(parms)
  return '' if parms.count.zero?

  "#{parms.join('&')}"
end

.write_entries(items, path, zipfile) ⇒ Object

Writes entries to a zip file

Parameters:

  • items (Array)

    An Array of items to zip

  • path (String)

    The path to the items

  • zipfile (Object)

    The Zip object

Author:

  • Jason Colyer

Since:

  • 1.0.12



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/support_readiness/client.rb', line 181

def self.write_entries(items, path, zipfile)
  items.each do |i|
    zipfile_path = path == '' ? i : File.join(path, i)
    disk_file_path = File.join(@input_dir, zipfile_path)
    if File.directory? disk_file_path
      recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
    else
      put_into_archive(disk_file_path, zipfile, zipfile_path)
    end
  end
end