Class: Readiness::Repos::Themes

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

Overview

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

Author:

  • Jason Colyer

Since:

  • 1.0.20

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

.convert_file(file) ⇒ Object

Converts the contents of a file for a theme

Parameters:

  • file (String)

    The absolute or relative path of a file to convert

Author:

  • Jason Colyer

Since:

  • 1.0.20



71
72
73
74
75
76
77
# File 'lib/support_readiness/repos/themes.rb', line 71

def self.convert_file(file)
  contents = File.read(file)
  if contents =~ /\[\[form/ || contents =~ /\[\[field/
    contents = scan_and_replace(contents)
  end
  File.write(file, contents)
end

.convert_only(destination, fields, forms) ⇒ Object

Runs the conversion for a theme

Parameters:

  • destination (String)

    The absolute or relative path of the the theme folder to convert

  • fields (Array)

    An array of Zendesk::TicketFields instances

  • forms (Array)

    An array of Zendesk::TicketForms instances

Author:

  • Jason Colyer

Since:

  • 1.0.20



35
36
37
38
39
40
41
42
# File 'lib/support_readiness/repos/themes.rb', line 35

def self.convert_only(destination, fields, forms)
  @destination = destination
  @fields = fields
  @forms = forms
  files_to_convert.each do |file|
    convert_file(file)
  end
end

.create_copy(source = 'data/theme', target = 'data/modified_theme') ⇒ Object

Copies from source to target

Parameters:

  • source (String) (defaults to: 'data/theme')

    The absolute or relative path of the theme folder

  • target (String) (defaults to: 'data/modified_theme')

    The absolute or relative path you want your modified theme put in

Author:

  • Jason Colyer

Since:

  • 1.0.20



51
52
53
# File 'lib/support_readiness/repos/themes.rb', line 51

def self.create_copy(source = 'data/theme', target = 'data/modified_theme')
  FileUtils.copy_entry source, target
end

.create_modified(source, target, fields, forms) ⇒ Object

Creates a modified theme

Parameters:

  • source (String)

    The absolute or relative path of the theme folder

  • target (String)

    The absolute or relative path you want your modified theme put in

  • fields (Array)

    An array of Zendesk::TicketFields instances

  • forms (Array)

    An array of Zendesk::TicketForms instances

Author:

  • Jason Colyer

Since:

  • 1.0.20



22
23
24
25
# File 'lib/support_readiness/repos/themes.rb', line 22

def self.create_modified(source, target, fields, forms)
  create_copy(source, target)
  convert_only(target, fields, forms)
end

.files_to_convertArray

Generates an array of files to use

Returns:

  • (Array)

Author:

  • Jason Colyer

Since:

  • 1.0.20



61
62
63
# File 'lib/support_readiness/repos/themes.rb', line 61

def self.files_to_convert
  Dir["#{@destination}/templates/*.hbs"] + ["#{@destination}/script.js"]
end

.item_from_match(match) ⇒ String

Determine the title/name of the form/field in a match

Parameters:

  • match (String)

    The regex match of a form or field placeholder

Returns:

  • (String)

Author:

  • Jason Colyer

Since:

  • 1.0.20



133
134
135
# File 'lib/support_readiness/repos/themes.rb', line 133

def self.item_from_match(match)
  match.split(':').last.split(']').first.strip.gsub(/^['"]/, '').gsub(/['"]$/, '')
end

.replace_fields(contents, match) ⇒ String

Replaces field placeholders with their matching ID value

Parameters:

  • contents (String)

    The contents of a file being converted

  • match (String)

    The regex match of a field placeholder

Returns:

  • (String)

Author:

  • Jason Colyer

Since:

  • 1.0.20



117
118
119
120
121
122
123
124
# File 'lib/support_readiness/repos/themes.rb', line 117

def self.replace_fields(contents, match)
  name = item_from_match(match)
  field = @fields.detect { |f| f.title == name }
  puts "Bad field in code: #{name}" if field.nil?
  exit 1 if field.nil?

  contents.gsub(match, field.id.to_s)
end

.replace_forms(contents, match) ⇒ String

Replaces form placeholders with their matching ID value

Parameters:

  • contents (String)

    The contents of a file being converted

  • match (String)

    The regex match of a form placeholder

Returns:

  • (String)

Author:

  • Jason Colyer

Since:

  • 1.0.20



100
101
102
103
104
105
106
107
# File 'lib/support_readiness/repos/themes.rb', line 100

def self.replace_forms(contents, match)
  name = item_from_match(match)
  form = @forms.detect { |f| f.name == name }
  puts "Bad form in code: #{name}" if form.nil?
  exit 1 if form.nil?

  contents.gsub(match, form.id.to_s)
end

.scan_and_replace(contents) ⇒ String

Scans and replaces items for themes

Parameters:

  • contents (String)

    The contents of a file being converted

Returns:

  • (String)

Author:

  • Jason Colyer

Since:

  • 1.0.20



86
87
88
89
90
# File 'lib/support_readiness/repos/themes.rb', line 86

def self.scan_and_replace(contents)
  contents.scan(/\[\[form:\ ?['"]?[^\]]+['"]?\]\]/).each { |m| contents = replace_forms(contents, m) }
  contents.scan(/\[\[field:\ ?['"]?[^\]]+['"]?\]\]/).each { |m| contents = replace_fields(contents, m) }
  contents
end