Class: Mailgun::Templates

Inherits:
Object
  • Object
show all
Defined in:
lib/mailgun/templates/templates.rb

Overview

A Mailgun::Templates object is a simple CRUD interface to Mailgun Templates. Uses Mailgun

Instance Method Summary collapse

Constructor Details

#initialize(client = Mailgun::Client.new) ⇒ Templates

Public: creates a new Mailgun::Templates instance. Defaults to Mailgun::Client



9
10
11
# File 'lib/mailgun/templates/templates.rb', line 9

def initialize(client = Mailgun::Client.new)
  @client = client
end

Instance Method Details

#create(domain, options = {}) ⇒ Object

Public: Add template

domain - [String] Name of the domain for new template(ex. domain.com) options - [Hash] of

name - [String] Name of the template being stored.
description   - [String] Description of the template being stored
template      - [String] (Optional) Content of the template
tag           - [String] (Optional) Initial tag of the created version.
comment       - [String] (Optional) Version comment.
headers       - [String] (Optional) Key Value json dictionary of headers to be stored with the template.
                ex.('{"Subject": "{{subject}}"}')

Returns [Hash] of created template

Raises:



26
27
28
29
30
# File 'lib/mailgun/templates/templates.rb', line 26

def create(domain, options = {})
  raise(ParameterError, 'No domain given to store template on', caller) unless domain

  @client.post("#{domain}/templates", options).to_h
end

#create_version(domain, template_name, options = {}) ⇒ Object

Public: Create a new version of a template

domain - [String] Name of the domain for new template(ex. domain.com) template_name - [String] Template name to lookup for options - [Hash] of

template      - [String] Content of the template
tag           - [String] Initial tag of the created version.
comment       - [String] (Optional) Version comment.
active        - [Boolean] (Optional) If this flag is set to yes, this version becomes active
headers       - [String] (Optional) Key Value json dictionary of headers to be stored with the template.
                ex.('{"Subject": "{{subject}}"}')

Returns [Hash] of updated template

Raises:



120
121
122
123
124
125
# File 'lib/mailgun/templates/templates.rb', line 120

def create_version(domain, template_name, options = {})
  raise(ParameterError, 'No domain given.', caller) unless domain
  raise(ParameterError, 'No template name given.', caller) unless template_name

  @client.post("#{domain}/templates/#{template_name}/versions", options).to_h
end

#delete_version(domain, template_name, tag) ⇒ Object

Public: Delete the version of the template

domain - [String] Domain name where template is stored template_name - [String] Template name to lookup for tag - [String] Version tag to lookup for

Returns [Boolean] if successful or not

Raises:



170
171
172
173
174
175
176
177
# File 'lib/mailgun/templates/templates.rb', line 170

def delete_version(domain, template_name, tag)
  raise(ParameterError, 'No domain given.', caller) unless domain
  raise(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
  raise(ParameterError, 'No version tag given.', caller) unless tag

  @client.delete("#{domain}/templates/#{template_name}/versions/#{tag}")
         .to_h['message'] == 'version has been deleted'
end

#info(domain, template_name, options = {}) ⇒ Object

Public: Get template information

domain - [String] Domain name where template is stored template_name - [String] Template name to lookup for options - [Hash] of

active - [Boolean] (Optional) If this flag is set to yes the active version
          of the template is included in the response.

Returns [Hash] Information on the requested template.

Raises:



41
42
43
44
45
46
# File 'lib/mailgun/templates/templates.rb', line 41

def info(domain, template_name, options = {})
  raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
  raise(ParameterError, 'No template name given to find on provided domain', caller) unless template_name

  @client.get("#{domain}/templates/#{template_name}", options).to_h!
end

#info_version(domain, template_name, tag) ⇒ Object

Public: Get template version information

domain - [String] Domain name where template is stored template_name - [String] Template name to lookup for tag - [String] Version tag to lookup for

Returns [Hash] Information on the requested template + version.

Raises:



134
135
136
137
138
139
140
# File 'lib/mailgun/templates/templates.rb', line 134

def info_version(domain, template_name, tag)
  raise(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
  raise(ParameterError, 'No template name given to find on provided domain', caller) unless template_name
  raise(ParameterError, 'No version tag given.', caller) unless tag

  @client.get("#{domain}/templates/#{template_name}/versions/#{tag}").to_h!
end

#list(domain, options = {}) ⇒ Object Also known as: get_templates

Public: Get Templates

domain - [String] Domain name where template is stored page - [String] Name of a page to retrieve. first, last, next, prev limit - [Integer] Maximum number of records to return. (100 by default) p - [Integer] Pivot is used to retrieve records in chronological order

Returns [Array] A list of templates (hash)

Raises:



87
88
89
90
91
# File 'lib/mailgun/templates/templates.rb', line 87

def list(domain, options = {})
  raise(ParameterError, 'No domain given.', caller) unless domain

  @client.get("#{domain}/templates", options).to_h['items']
end

#remove(domain, template_name) ⇒ Object Also known as: delete, delete_template

Public: Delete Template NOTE: This method deletes all versions of the specified template.

domain - [String] Domain name where template is stored template_name - [String] Template name to lookup for

Returns [Boolean] if successful or not

Raises:



70
71
72
73
74
75
# File 'lib/mailgun/templates/templates.rb', line 70

def remove(domain, template_name)
  raise(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
  raise(ParameterError, 'No template name given to find on provided domain', caller) unless template_name

  @client.delete("#{domain}/templates/#{template_name}").to_h['message'] == 'template has been deleted'
end

#remove_all(domain) ⇒ Object Also known as: delete_templates

Public: Delete Templates NOTE: This method deletes all stored templates for the domain.

domain - [String] Domain name where template is stored

Returns [Boolean] if successful or not

Raises:



100
101
102
103
104
# File 'lib/mailgun/templates/templates.rb', line 100

def remove_all(domain)
  raise(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain

  @client.delete("#{domain}/templates").to_h['message'] == 'templates have been deleted'
end

#template_versions_list(domain, template_name, options = {}) ⇒ Object

Public: Get Template’s Versions list

domain - [String] Domain name where template is stored template_name - [String] Template name to lookup for options - [Hash] of page - [String] Name of a page to retrieve. first, last, next, prev limit - [Integer] Maximum number of records to return. (100 by default) p - [Integer] Pivot is used to retrieve records in chronological order

Returns [Array] A list of template’s versions (hash)

Raises:



189
190
191
192
193
194
# File 'lib/mailgun/templates/templates.rb', line 189

def template_versions_list(domain, template_name, options = {})
  raise(ParameterError, 'No domain given.', caller) unless domain
  raise(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name

  @client.get("#{domain}/templates/#{template_name}/versions", options).to_h
end

#update(domain, template_name, options = {}) ⇒ Object

Public: Update the metadata information of the template

domain - [String] Domain name where template is stored template_name - [String] Template name to lookup for options - [Hash] of

description - [String] Updated description of the template

Returns [Hash] of updated domain

Raises:



56
57
58
59
60
61
# File 'lib/mailgun/templates/templates.rb', line 56

def update(domain, template_name, options = {})
  raise(ParameterError, 'No domain given to add on Mailgun', caller) unless domain
  raise(ParameterError, 'No template name given to find on provided domain', caller) unless template_name

  @client.put("#{domain}/templates/#{template_name}", options).to_h
end

#update_version(domain, template_name, tag, options = {}) ⇒ Object

Public: Update the version of the template

domain - [String] Domain name where template is stored template_name - [String] Template name to lookup for tag - [String] Version tag to lookup for options - [Hash] of

template      - [String] Content of the template
comment       - [String] (Optional) Version comment.
active        - [Boolean] (Optional) If this flag is set to yes, this version becomes active
headers       - [String] (Optional) Key Value json dictionary of headers to be stored with the template.
                ex.('{"Subject": "{{subject}}"}')

Returns [Hash] of updated template’s version

Raises:



155
156
157
158
159
160
161
# File 'lib/mailgun/templates/templates.rb', line 155

def update_version(domain, template_name, tag, options = {})
  raise(ParameterError, 'No domain given.', caller) unless domain
  raise(ParameterError, 'No template name given to find on provided domain.', caller) unless template_name
  raise(ParameterError, 'No version tag given.', caller) unless tag

  @client.put("#{domain}/templates/#{template_name}/versions/#{tag}", options).to_h
end