Class: Readiness::Zendesk::Automations

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

Overview

Defines the class Automations 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 = {}) ⇒ Automations

Creates a new Readiness::Zendesk::Automations instance

Examples:

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

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.0



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

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

Instance Attribute Details

#actionsObject

Since:

  • 1.0.0



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

def actions
  @actions
end

#activeObject

Since:

  • 1.0.0



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

def active
  @active
end

#conditionsObject

Since:

  • 1.0.0



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

def conditions
  @conditions
end

#idObject

Since:

  • 1.0.0



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

def id
  @id
end

#positionObject

Since:

  • 1.0.0



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

def position
  @position
end

#titleObject

Since:

  • 1.0.0



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

def title
  @title
end

Class Method Details

.create!(client, automation) ⇒ Object

Creates an automation. 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)
automation = Readiness::Zendesk::Automations.new
automation.title = 'Change priority to low for solved tickets'
automation.conditions = {
  all: [
    {
      field: 'status',
      operator: 'is',
      value: 'solved'
    },
    {
      field: 'priority',
      operator: 'is_not',
      value: 'low'
    }
  ],
  any: []
}
automation.actions = [
    {
      field: 'priority',
      value: 'low'
    }
]
created = Readiness::Zendesk::Automations.create!(client, automation)
pp created.id
# => 26

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



160
161
162
163
164
# File 'lib/support_readiness/zendesk/automations.rb', line 160

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

.delete!(client, automation) ⇒ Boolean

Deletes an automation. 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)
automation = Readiness::Zendesk::Automations.find!(client, 26)
deleted = Readiness::Zendesk::Automations.delete!(client, automation)
pp deleted
# => true

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



267
268
269
270
271
# File 'lib/support_readiness/zendesk/automations.rb', line 267

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

.delete_many!(client, aids) ⇒ Boolean

Deletes multiple automations via a batch job

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)
deleted = Readiness::Zendesk::Automations.delete_many!(client, [1, 7, 19])
pp deleted
# => true

Parameters:

  • client (Object)

    An instance of Client

  • aids (Array)

    An array of automation IDs

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



292
293
294
295
296
# File 'lib/support_readiness/zendesk/automations.rb', line 292

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

.find(client, aid) ⇒ Hash

Locates an automation 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)
automation = Readiness::Zendesk::Automations.find(client, 25)
pp automation.title
# => "Close and Save"

Parameters:

  • client (Object)

    An instance of Client

  • aid (Integer)

    The automation 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/automations.rb', line 85

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

  Oj.load(response.body)
end

.find!(client, aid) ⇒ Object

Locates an automation 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)
automation = Readiness::Zendesk::Automations.find!(client, 25)
pp automation.title
# => "Close and Save"

Parameters:

  • client (Object)

    An instance of Client

  • aid (Integer)

    The automation ID to find

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



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

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

.list(client) ⇒ Array

Lists automations.

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)
automations = Readiness::Zendesk::Automations.list(client)
pp automations.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/automations.rb', line 51

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

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

.update!(client, automation) ⇒ Object

Updates an automation. 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)
automation = Readiness::Zendesk::Automations.find!(client, 26)
automation.title = 'Change priority to medium for solved tickets'
automation.conditions = {
  all: [
    {
      field: 'status'
      operator: 'is'
      value: 'solved'
    },
    {
      field: 'priority'
      operator: 'is_not'
      value: 'medium'
    }
  ]
  any: []
}
automation.actions = [
    {
      field: 'priority'
      value: 'medium'
    }
]
updated = Readiness::Zendesk::Automations.update!(client, automation)
pp updated.title
# => "Change priority to medium for solved tickets"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



208
209
210
211
212
# File 'lib/support_readiness/zendesk/automations.rb', line 208

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

.update_many!(client, automations) ⇒ Array

Updates multiple automations. 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)
automation1 = Readiness::Zendesk::Automations.find!(client, 1)
automation2 = Readiness::Zendesk::Automations.find!(client, 2)
automation3 = Readiness::Zendesk::Automations.find!(client, 3)
automation1.position = 2
automation2.position = 3
automation3.position = 1
automations = [automation1, automation2, automation3]
updated = Readiness::Zendesk::Automations.update_many!(client, automation)
pp updated.first.position
# => 2

Parameters:

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



240
241
242
243
244
245
# File 'lib/support_readiness/zendesk/automations.rb', line 240

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