Class: Readiness::Zendesk::Macros

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

Overview

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

Author:

  • Jason Colyer

Since:

  • 1.0.0

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

Creates a new Readiness::Zendesk::Macros instance

Examples:

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

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.0



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

def initialize(object = {})
  @actions = object['actions']
  @active = object['active']
  @description = object['description']
  @id = object['id']
  @restriction = object['restriction']
  @title = object['title']
end

Instance Attribute Details

#actionsObject

Since:

  • 1.0.0



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

def actions
  @actions
end

#activeObject

Since:

  • 1.0.0



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

def active
  @active
end

#descriptionObject

Since:

  • 1.0.0



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

def description
  @description
end

#idObject

Since:

  • 1.0.0



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

def id
  @id
end

#restrictionObject

Since:

  • 1.0.0



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

def restriction
  @restriction
end

#titleObject

Since:

  • 1.0.0



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

def title
  @title
end

Class Method Details

.create!(client, macro) ⇒ Object

Creates a macro. 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)
macro = Readiness::Zendesk::Macros.new
macro.title = 'Roger Wilco'
macro.actions = [{ field: 'status', value: 'solved' }]
create = Readiness::Zendesk::Macros.create!(client, macro)
pp create.id
# => 26

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



140
141
142
143
144
# File 'lib/support_readiness/zendesk/macros.rb', line 140

def self.create!(client, macro)
  response = client.connection.post 'macros', to_clean_json_with_key(macro, 'macro')
  handle_request_error(1, 'Zendesk', response.status, { action: 'Create macro', message: Oj.load(response.body)}) unless response.status == 201
  Macros.new(Oj.load(response.body)['macro'])
end

.delete!(client, macro) ⇒ Boolean

Deletes a macro. 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)
macro = Readiness::Zendesk::Macros.find!(client, 26)
delete = Readiness::Zendesk::Macros.delete!(client, macro)
pp delete
# => true

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



224
225
226
227
228
# File 'lib/support_readiness/zendesk/macros.rb', line 224

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

.delete_many!(client, mids) ⇒ Boolean

Deletes multiple macros

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)
deletes = Readiness::Zendesk::Macros.delete_many!(client, [25, 26])
pp deletes
# => true

Parameters:

  • client (Object)

    An instance of Client

  • mids (Array)

    An array of macro IDs

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



249
250
251
252
253
# File 'lib/support_readiness/zendesk/macros.rb', line 249

def self.delete_many!(client, mids)
  response = client.connection.delete("macros/destroy_many?ids=#{mids.join(',')}")
  handle_request_error(1, 'Zendesk', response.status, { action: 'Delete many macros', message: Oj.load(response.body)}) unless response.status == 204
  true
end

.find(client, mid) ⇒ Hash

Locates a macro 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)
macro = Readiness::Zendesk::Macros.find(client, 25)
pp macro.title
# => "Assign priority tag"

Parameters:

  • client (Object)

    An instance of Client

  • mid (Integer)

    The macro ID to find

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



85
86
87
88
89
90
91
# File 'lib/support_readiness/zendesk/macros.rb', line 85

def self.find(client, mid)
  response = client.connection.get("macros/#{mid}")
  handle_request_error(0, 'Zendesk', response.status,  { action: 'get', id: mid }) unless response.status == 200
  return Macros.new(Oj.load(response.body)['macro']) if response.status == 200

  Oj.load(response.body)
end

.find!(client, mid) ⇒ Object

Locates a macro 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)
macro = Readiness::Zendesk::Macros.find!(client, 25)
pp macro.title
# => "Assign priority tag"

Parameters:

  • client (Object)

    An instance of Client

  • mid (Integer)

    The macro ID to find

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



112
113
114
115
116
# File 'lib/support_readiness/zendesk/macros.rb', line 112

def self.find!(client, mid)
  response = client.connection.get("macros/#{mid}")
  handle_request_error(1, 'Zendesk', response.status, { action: 'Find macro', id: mid }) unless response.status == 200
  Macros.new(Oj.load(response.body)['macro'])
end

.list(client) ⇒ Array

Lists macros

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)
macros = Readiness::Zendesk::Macros.list(client)
pp macros.first.id
# => 25

Parameters:

  • client (Object)

    An instance of Client

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/support_readiness/zendesk/macros.rb', line 51

def self.list(client)
  array = []
  opts = "page[size]=100"
  loop do
    response = client.connection.get("macros?#{opts}")
    handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
    body = Oj.load(response.body)
    array += body['macros'].map { |t| Macros.new(t) }
    break unless body['meta']['has_more']

    opts = body['links'] ['next'].split('?').last
  end
  array
end

.update!(client, macro) ⇒ Object

Updates a macro. 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)
macro = Readiness::Zendesk::Macros.find!(client, 26)
macro.title = 'Solve ticket'
update = Readiness::Zendesk::Macros.update!(client, macro)
pp update.title
# => "Solve ticket"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



167
168
169
170
171
# File 'lib/support_readiness/zendesk/macros.rb', line 167

def self.update!(client, macro)
  response = client.connection.put "macros/#{macro.id}", to_clean_json_with_key(macro, 'macro')
  handle_request_error(1, 'Zendesk', response.status, { action: 'Update macro', id: macro.id, message: Oj.load(response.body)}) unless response.status == 200
  Macros.new(Oj.load(response.body)['macro'])
end

.update_many!(client, macros) ⇒ array

Updates multiple macros. Can only update position and active values.

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)
macro1 = Readiness::Zendesk::Macros.find!(client, 25)
macro1.title = 'Solve ticket'
macro2 = Readiness::Zendesk::Macros.find!(client, 25)
macro2.title = 'Solve ticket and add done tag'
macros = [macro1, macro2]
updates = Readiness::Zendesk::Macros.update_many!(client, macros)
pp updates.last.title
# => "Solve ticket and add done tag"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



197
198
199
200
201
202
# File 'lib/support_readiness/zendesk/macros.rb', line 197

def self.update_many!(client, macros)
  data = { macros: macros.map { |m| to_hash(m).compact } }.to_json
  response = client.connection.put('macros/update_many', data)
  handle_request_error(1, 'Zendesk', response.status, { action: 'Update many macros', message: Oj.load(response.body)}) unless response.status == 200
  Oj.load(response.body)['macros'].map { |m| Macros.new(m) }
end