Class: Mailtrap::TemplatesAPI

Inherits:
Object
  • Object
show all
Includes:
BaseAPI
Defined in:
lib/mailtrap/templates_api.rb

Overview

Note:

Experimental: the /api/templates endpoints may change their request and response shapes before general availability.

Instance Attribute Summary

Attributes included from BaseAPI

#account_id, #client

Instance Method Summary collapse

Methods included from BaseAPI

included, #initialize

Instance Method Details

#create(options) ⇒ Template

Creates a new template

Parameters:

  • options (Hash) —

    The parameters to create

Options Hash (options):

  • :name (String) —

    The template name (required)

  • :subject (String) —

    The email subject (required)

  • :category (String) —

    The template category (required)

  • :body_html (String, nil) —

    The HTML content. Default: nil.

  • :body_text (String, nil) —

    The plain text content. Default: nil.

Returns:

  • (Template) —

    Created template object

Raises:



54
55
56
# File 'lib/mailtrap/templates_api.rb', line 54

def create(options)
  base_create(options)
end

#delete(template_id) ⇒ nil

Deletes a template

Parameters:

  • template_id (Integer) —

    The template ID

Returns:

  • (nil)

Raises:



77
78
79
# File 'lib/mailtrap/templates_api.rb', line 77

def delete(template_id)
  base_delete(template_id)
end

#get(template_id) ⇒ Template

Retrieves a specific template

Parameters:

  • template_id (Integer) —

    The template ID

Returns:

Raises:



40
41
42
# File 'lib/mailtrap/templates_api.rb', line 40

def get(template_id)
  base_get(template_id)
end

#list(per_page: nil, token: nil) ⇒ TemplatesListResponse

Lists templates for the account, one page at a time. Unlike EmailTemplatesAPI#list, it does not return every template: pass pagination[:next_token] with the same per_page to get the next page.

Parameters:

  • per_page (Integer, nil) (defaults to: nil) —

    Number of templates per page (max 100, default 50)

  • token (Integer, nil) (defaults to: nil) —

    Page number to retrieve (page-token pagination, default 1)

Returns:

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mailtrap/templates_api.rb', line 23

def list(per_page: nil, token: nil)
  query_params = {}
  query_params[:per_page] = per_page unless per_page.nil?
  query_params[:token] = token unless token.nil?

  response = client.get(base_path, query_params)

  TemplatesListResponse.new(
    data: Array(response[:data]).map { |item| build_entity(item, response_class) },
    pagination: response[:pagination]
  )
end

#update(template_id, options) ⇒ Template

Updates an existing template. Only the provided attributes are changed.

Parameters:

  • template_id (Integer) —

    The template ID

  • options (Hash) —

    The parameters to update

Options Hash (options):

  • :name (String) —

    The template name

  • :subject (String) —

    The email subject

  • :category (String) —

    The template category

  • :body_html (String, nil) —

    The HTML content

  • :body_text (String, nil) —

    The plain text content

Returns:

  • (Template) —

    Updated template object

Raises:



69
70
71
# File 'lib/mailtrap/templates_api.rb', line 69

def update(template_id, options)
  base_update(template_id, options)
end