Class: Readiness::Zendesk::Themes

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

Overview

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

Author:

  • Jason Colyer

Since:

  • 1.0.12

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 = {}) ⇒ Themes

Creates a new Readiness::Zendesk::Themes instance

Examples:

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

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.12



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

def initialize(object = {})
  @author = object['author']
  @brand_id = object['brand_id']
  @id = object['id']
  @live = object['live']
  @name = object['name']
  @version = object['version']
end

Instance Attribute Details

#authorObject

Since:

  • 1.0.12



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

def author
  @author
end

#brand_idObject

Since:

  • 1.0.12



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

def brand_id
  @brand_id
end

#idObject

Since:

  • 1.0.12



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

def id
  @id
end

#liveObject

Since:

  • 1.0.12



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

def live
  @live
end

#nameObject

Since:

  • 1.0.12



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

def name
  @name
end

#versionObject

Since:

  • 1.0.12



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

def version
  @version
end

Class Method Details

.create!(client, theme, location = './') ⇒ Object

Creates a theme. 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)
theme = Readiness::Zendesk::Themes.live_theme(client)
job = Readiness::Zendesk::Themes.create!(client, theme, 'data/modified_theme')
job = Readiness::Zendesk::Themes.upload!(client, job, file)
pp job.id
# => "a66e7bde543c6b6d018f0e07a654feaf"

Parameters:

  • client (Object)

    An instance of Client

  • theme (Object)
  • location (location) (defaults to: './')

    The relative path to the location of the theme files

Returns:

Author:

  • Jason Colyer

Since:

  • 1.0.12



339
340
341
342
343
# File 'lib/support_readiness/zendesk/themes.rb', line 339

def self.create!(client, theme, location = './')
  file = Themes.package!(location)
  job = generate_import_job!(client, theme)
  upload!(client, job, file)
end

.delete!(client, theme) ⇒ Boolean

Deletes a theme. 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)
theme = Readiness::Zendesk::Themes.find!(client, 'a66e7bde543c6b6d018f0e07a654feaf')
delete = Readiness::Zendesk::Themes.delete!(client, theme)
pp delete
# => true

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.20



365
366
367
368
369
# File 'lib/support_readiness/zendesk/themes.rb', line 365

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

.find(client, tid) ⇒ Object

Locates a theme 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)
theme = Readiness::Zendesk::Themes.find(client, '6d7ccf08-5674-467c-9036-3403b6c5e91d')
pp app.name
# => "GitLab Zendesk Global Theme"

Parameters:

  • client (Object)

    An instance of Client

  • tid (String)

    The theme ID to locate

Returns:

  • (Object)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.12



103
104
105
106
107
108
109
# File 'lib/support_readiness/zendesk/themes.rb', line 103

def self.find(client, tid)
  response = client.connection.get "guide/theming/themes/#{tid}"
  handle_request_error(0, 'Zendesk', response.status,  { action: 'get', id: tid }) unless response.status == 200
  return Themes.new(Oj.load(response.body)['theme']) if response.status == 200

  Oj.load(response.body)
end

.find!(client, tid) ⇒ Object

Locates a theme 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)
theme = Readiness::Zendesk::Themes.find!(client, '6d7ccf08-5674-467c-9036-3403b6c5e91d')
pp app.name
# => "GitLab Zendesk Global Theme"

Parameters:

  • client (Object)

    An instance of Client

  • tid (String)

    The theme ID to locate

Returns:

  • (Object)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.12



130
131
132
133
134
# File 'lib/support_readiness/zendesk/themes.rb', line 130

def self.find!(client, tid)
  response = client.connection.get "guide/theming/themes/#{tid}"
  handle_request_error(1, 'Zendesk', response.status, { action: 'Find theme', id: tid }) unless response.status == 200
  Themes.new(Oj.load(response.body)['theme'])
end

.find_by_name(client, name) ⇒ Object

Locates a theme within Zendesk by its name.

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)
theme = Readiness::Zendesk::Themes.find_by_name(client, 'GitLab Zendesk Global Theme')
pp theme.id
# => "6d7ccf08-5674-467c-9036-3403b6c5e91d"

Parameters:

  • client (Object)

    An instance of Client

  • name (String)

    The theme name to locate

Returns:

  • (Object)

Author:

  • Jason Colyer

Since:

  • 1.0.20



154
155
156
157
# File 'lib/support_readiness/zendesk/themes.rb', line 154

def self.find_by_name(client, name)
  themes = Themes.list(client)
  themes.detect { |t| t.name == name }
end

.generate_import_job!(client, theme) ⇒ Hash

Creates an import job for creating a Zendesk theme. 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)
theme = Readiness::Zendesk::Themes.new
theme.brand_id = 123
job = Readiness::Zendesk::Themes.generate_import_job!(client, theme)
pp job.id
# => "a66e7bde543c6b6d018f0e07a654feaf"

Parameters:

Returns:

Author:

  • Jason Colyer

Since:

  • 1.0.12



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/support_readiness/zendesk/themes.rb', line 230

def self.generate_import_job!(client, theme)
  object = {
    job: {
      attributes: {
        brand_id: theme.brand_id,
        format: 'zip'
      }
    }
  }
  response = client.connection.post 'guide/theming/jobs/themes/imports', object.to_json
  handle_request_error(1, 'Zendesk', response.status, { action: 'Create theme import job', id: theme.name }) unless response.status == 202
  Oj.load(response.body)['job']
end

.generate_update_job!(client, theme) ⇒ Object

Creates an update job for creating a Zendesk theme. 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)
theme = Readiness::Zendesk::Themes.live_theme(client)
job = Readiness::Zendesk::Themes.generate_update_job!(client, theme)
pp job.id
# => "a66e7bde543c6b6d018f0e07a654feaf"

Parameters:

Returns:

Author:

  • Jason Colyer

Since:

  • 1.0.12



195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/support_readiness/zendesk/themes.rb', line 195

def self.generate_update_job!(client, theme)
  object = {
    job: {
      attributes: {
        theme_id: theme.id,
        replace_settings: true,
        format: 'zip'
      }
    }
  }
  response = client.connection.post 'guide/theming/jobs/themes/updates', object.to_json
  handle_request_error(1, 'Zendesk', response.status, { action: 'Upload theme', id: theme.id }) unless response.status == 200
  ThemeJobStatuses.new(Oj.load(response.body)['job'])
end

.list(client) ⇒ Array

Lists themes 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)
themes = Readiness::Zendesk::Themes.list(client)
pp themes.first.name
# => "GitLab Zendesk Global Theme"

Parameters:

  • client (Object)

    An instance of Client

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.12



51
52
53
54
55
# File 'lib/support_readiness/zendesk/themes.rb', line 51

def self.list(client)
  response = client.connection.get 'guide/theming/themes'
  handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
  Oj.load(response.body)['themes'].map { |t| Themes.new(t) }
end

.live_theme(client, brand = nil) ⇒ Object

Returns the live theme for the Zendesk instance

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)
theme = Readiness::Zendesk::Themes.live_theme(client)
pp theme.id
# => "28a0b408-626a-4bea-88ab-2d48a8078870"

Parameters:

  • client (Object)

    An instance of Client

  • brand (Object) (defaults to: nil)

    An optional instance of Brands that can be used to find a specific theme

Returns:

  • (Object)

Author:

  • Jason Colyer

Since:

  • 1.0.12



75
76
77
78
79
80
81
82
# File 'lib/support_readiness/zendesk/themes.rb', line 75

def self.live_theme(client, brand = nil)
  themes = Themes.list(client)
  if brand
    themes.detect { |t| t.live && t.brand_id.to_i == brand.id.to_i }
  else
    themes.detect { |t| t.live }
  end
end

.package!(location = './') ⇒ String

Creates a zip file from the given directory for a Zendesk app. This will exit on error

Examples:

require 'support_readiness'
file = Readiness::Zendesk::Themes.package!
pp file
# => "data/20241007151228578.zip"

Parameters:

  • location (location) (defaults to: './')

    The relative path to the location of the theme files

Returns:

  • (String)

    The path to the zip file

Author:

  • Jason Colyer

Since:

  • 1.0.12



171
172
173
174
# File 'lib/support_readiness/zendesk/themes.rb', line 171

def self.package!(location = './')
  item_list = %w[ assets manifest.json script.js settings style.css templates thumbnail.png translations ]
  create_package!(item_list, location)
end

.update!(client, theme, location = './') ⇒ Object

Creates a theme. 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)
theme = Readiness::Zendesk::Themes.new
theme.brand_id = 123
theme.live = false
job = Readiness::Zendesk::Themes.update!(client, theme, 'data/modified_theme')
job = Readiness::Zendesk::Themes.upload!(client, job, file)
pp job.id
# => "a66e7bde543c6b6d018f0e07a654feaf"

Parameters:

  • client (Object)

    An instance of Client

  • theme (Object)
  • location (location) (defaults to: './')

    The relative path to the location of the theme files

Returns:

Author:

  • Jason Colyer

Since:

  • 1.0.12



312
313
314
315
316
# File 'lib/support_readiness/zendesk/themes.rb', line 312

def self.update!(client, theme, location = './')
  file = Themes.package!(location)
  job = generate_update_job!(client, theme)
  upload!(client, job, file)
end

.upload!(client, job, file) ⇒ Object

Uploads a zip file to an import or update job. 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)
file = Readiness::Zendesk::Themes.package!('data/modified_theme')
theme = Readiness::Zendesk::Themes.live_theme(client)
job = Readiness::Zendesk::Themes.generate_import_job!(client, theme)
job = Readiness::Zendesk::Themes.upload!(client, job, file)
pp job.id
# => "a66e7bde543c6b6d018f0e07a654feaf"

Parameters:

Returns:

Author:

  • Jason Colyer

Since:

  • 1.0.12



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/support_readiness/zendesk/themes.rb', line 266

def self.upload!(client, job, file)
  url = if job.is_a?(Hash)
          job['data']['upload']['url']
        else
          job.data['upload']['url']
        end
  params = if job.is_a?(Hash)
             job['data']['upload']['parameters']
           else
             job.data['upload']['parameters']
           end
  job_id = if job.is_a?(Hash)
             job['id']
           else
             job.id
           end
  object = params.merge(
    file: ::Faraday::UploadIO.new(file, 'application/zip')
  )
  response = client.upload_connection.post url, object
  ThemeJobStatuses.find!(client, job_id)
end