Class: Klaviyo::EmailTemplates

Inherits:
Client
  • Object
show all
Defined in:
lib/klaviyo/apis/email_templates.rb

Constant Summary collapse

EMAIL_TEMPLATES =
'email-templates'
EMAIL_TEMPLATE =
'email-template'
CLONE =
'clone'
RENDER =
'render'
SEND =
'send'

Constants inherited from Client

Client::ALL, Client::BASE_API_URL, Client::CONTENT_JSON, Client::CONTENT_URL_FORM, Client::DEFAULT_COUNT, Client::DEFAULT_PAGE, Client::DEFAULT_SORT_DESC, Client::HTTP_DELETE, Client::HTTP_GET, Client::HTTP_POST, Client::HTTP_PUT, Client::METRIC, Client::METRICS, Client::TIMELINE, Client::V1_API, Client::V2_API

Class Method Summary collapse

Class Method Details

.clone_template(template_id, name:) ⇒ JSON

Creates a copy of a given template with a new name

Parameters:

  • template_id (String)

    The id of the email template to copy

  • :name (String)

    The name of the newly cloned email template

Returns:

  • (JSON)

    a JSON object containing information about the email template



55
56
57
58
59
60
61
# File 'lib/klaviyo/apis/email_templates.rb', line 55

def self.clone_template(template_id, name:)
  path = "#{EMAIL_TEMPLATE}/#{template_id}/#{CLONE}"
  params = {
    name: name
  }
  v1_request(HTTP_POST, path, content_type: CONTENT_URL_FORM, params: params)
end

.create_template(name: nil, html: nil) ⇒ JSON

Creates a new email template

Parameters:

  • :name (String)

    The name of the email template

  • :html (String)

    The HTML content for this template

Returns:

  • (JSON)

    a JSON object containing information about the email template



20
21
22
23
24
25
26
# File 'lib/klaviyo/apis/email_templates.rb', line 20

def self.create_template(name: nil, html: nil)
  params = {
    name: name,
    html: html
  }
  v1_request(HTTP_POST, EMAIL_TEMPLATES, content_type: CONTENT_URL_FORM, params: params)
end

.delete_template(template_id) ⇒ JSON

Deletes a given template.

Parameters:

  • template_id (String)

    The id of the email template

Returns:

  • (JSON)

    a JSON object containing information about the email template



46
47
48
49
# File 'lib/klaviyo/apis/email_templates.rb', line 46

def self.delete_template(template_id)
  path = "#{EMAIL_TEMPLATE}/#{template_id}"
  v1_request(HTTP_DELETE, path)
end

.get_templatesList

Returns a list of all the email templates you’ve created. The templates are returned in sorted order by name.

Returns:

  • (List)

    of JSON formatted email template objects



12
13
14
# File 'lib/klaviyo/apis/email_templates.rb', line 12

def self.get_templates()
  v1_request(HTTP_GET, EMAIL_TEMPLATES)
end

.render_template(template_id, context: {}) ⇒ JSON

Renders the specified template with the provided data and return HTML and text versions of the email

Parameters:

  • template_id (String)

    The id of the email template to copy

  • :context (Hash)

    The context the email template will be rendered with

Returns:

  • (JSON)

    a JSON object containing information about the email template



68
69
70
71
72
73
74
# File 'lib/klaviyo/apis/email_templates.rb', line 68

def self.render_template(template_id, context: {})
  path = "#{EMAIL_TEMPLATE}/#{template_id}/#{RENDER}"
  params = {
    context: context
  }
  v1_request(HTTP_POST, path, content_type: CONTENT_URL_FORM, params: params)
end

.send_template(template_id, from_email:, from_name:, subject:, to:, context: {}) ⇒ JSON

Renders the specified template with the provided data and then send the contents in an email via the service specified

Parameters:

  • template_id (String)

    The id of the email template to copy

  • :from_email (String)

    The from email address; used in the reply-to header

  • :from_name (String)

    The name the email is sent from

  • :subject (String)

    The subject of the email template

  • :to (Mixed)

    The email this template is being sent to

  • :context (Hash)

    The context the email template will be rendered with

Returns:

  • (JSON)

    a JSON object containing information about the email template



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/klaviyo/apis/email_templates.rb', line 85

def self.send_template(template_id, from_email:, from_name:, subject:, to:, context: {})
  path = "#{EMAIL_TEMPLATE}/#{template_id}/#{SEND}"
  params = {
    from_email: from_email,
    from_name: from_name,
    subject: subject,
    to: to,
    context: context
  }
  v1_request(HTTP_POST, path, content_type: CONTENT_URL_FORM, params: params)
end

.update_template(template_id, name:, html:) ⇒ JSON

Updates the name and/or HTML content of a template. Only updates imported HTML templates; does not currently update drag & drop templates

Parameters:

  • template_id (String)

    The id of the email template

  • :name (String)

    The name of the email template

  • :html (String)

    The HTML content for this template

Returns:

  • (JSON)

    a JSON object containing information about the email template



34
35
36
37
38
39
40
41
# File 'lib/klaviyo/apis/email_templates.rb', line 34

def self.update_template(template_id, name:, html:)
  path = "#{EMAIL_TEMPLATE}/#{template_id}"
  params = {
    name: name,
    html: html
  }
  v1_request(HTTP_PUT, path, params)
end