mandrill-template-manager 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +87 -22
- data/lib/mandrill_template/cli.rb +71 -41
- data/lib/mandrill_template/client.rb +4 -0
- data/lib/mandrill_template/template.rb +24 -11
- data/mandrill-template-manager.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f266294f177aacc1b9526a166683283d174b3f41
|
4
|
+
data.tar.gz: 2325b885208b5dd04fc6f8f1693baa119e8ebca5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7361dfc08ed25beab0f5512303752b1cf5a07b8f88d17c95bbfa4f6ad72fe7272be46b94accfa4d66b912f0a189f58f79059b7ffff5aaae23cb00f759498449a
|
7
|
+
data.tar.gz: 097ad4edb603d6d857ea456286cfa4811435b112796dadd7fa06650ae507108ca9777bebc5f3fee3f65e3278a47f7b25673364bb1baffaf5a8b5c705b66091ff
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Mandrill Template Manager(CLI)
|
1
|
+
# Mandrill Template Manager (CLI)
|
2
2
|
|
3
3
|
Manage [Mandrill Templates](https://mandrillapp.com/api/docs/templates.ruby.html) by CLI.
|
4
4
|
|
@@ -15,23 +15,28 @@ gem 'unicode'
|
|
15
15
|
```
|
16
16
|
|
17
17
|
```
|
18
|
-
$
|
18
|
+
$ bundle install
|
19
|
+
```
|
20
|
+
|
21
|
+
```
|
22
|
+
$ bundle exec mandrilltemplate
|
19
23
|
Commands:
|
20
|
-
mandrilltemplate delete
|
21
|
-
mandrilltemplate export
|
22
|
-
mandrilltemplate
|
24
|
+
mandrilltemplate delete SLUG # delete template from remote.
|
25
|
+
mandrilltemplate export SLUG # export template from remote to local files.
|
26
|
+
mandrilltemplate export_all # export all templates from remote to local files.
|
27
|
+
mandrilltemplate generate SLUG # generate new template files.
|
23
28
|
mandrilltemplate help [COMMAND] # Describe available commands or one specific command
|
24
29
|
mandrilltemplate list # show template list both of remote and local.
|
25
|
-
mandrilltemplate publish
|
26
|
-
mandrilltemplate render
|
27
|
-
mandrilltemplate upload
|
30
|
+
mandrilltemplate publish SLUG # publish template from draft.
|
31
|
+
mandrilltemplate render SLUG [PARAMS_FILE] # render mailbody from local template data. File should be Array. see https://mandrillapp.com/api/docs/templates.JSON.html#method=render.
|
32
|
+
mandrilltemplate upload SLUG # upload template to remote as draft.
|
28
33
|
|
29
34
|
```
|
30
35
|
|
31
36
|
## Workflow
|
32
37
|
|
33
38
|
1. generate new or export exist template as local files.
|
34
|
-
2. modify and manage under version
|
39
|
+
2. modify and manage under version control system.
|
35
40
|
3. upload template.
|
36
41
|
4. publish it.
|
37
42
|
|
@@ -44,7 +49,8 @@ APIKEY is read from environment variable.
|
|
44
49
|
export MANDRILL_APIKEY='your api key'
|
45
50
|
```
|
46
51
|
|
47
|
-
|
52
|
+
|
53
|
+
## List templates
|
48
54
|
|
49
55
|
```
|
50
56
|
$ mandrilltemplate list
|
@@ -65,25 +71,84 @@ Local Templates
|
|
65
71
|
|
66
72
|
```
|
67
73
|
|
74
|
+
you can also specify a label to filter this list:
|
75
|
+
|
76
|
+
```
|
77
|
+
$ mandrilltemplate list LABEL
|
78
|
+
```
|
79
|
+
|
80
|
+
|
81
|
+
## Downloading templates from Mandrill
|
82
|
+
|
83
|
+
```
|
84
|
+
$ mandrilltemplate export SLUG
|
85
|
+
```
|
86
|
+
|
87
|
+
or
|
88
|
+
|
89
|
+
```
|
90
|
+
$ mandrilltemplate export_all
|
91
|
+
```
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
## Uploading and publishing templates
|
96
|
+
|
97
|
+
```
|
98
|
+
$ mandrilltemplate upload SLUG
|
99
|
+
```
|
100
|
+
|
101
|
+
```
|
102
|
+
$ mandrilltemplate publish SLUG
|
103
|
+
```
|
104
|
+
|
105
|
+
or, both at once:
|
106
|
+
|
107
|
+
```
|
108
|
+
$ mandrilltemplate upload SLUG --publish
|
109
|
+
```
|
110
|
+
|
111
|
+
|
112
|
+
## Deleting templates
|
113
|
+
|
114
|
+
```
|
115
|
+
$ mandrilltemplate delete SLUG
|
116
|
+
```
|
117
|
+
|
118
|
+
will delete from server only.
|
119
|
+
|
120
|
+
```
|
121
|
+
$ mandrilltemplate upload SLUG --delete_local
|
122
|
+
```
|
123
|
+
|
124
|
+
will also delete local files.
|
125
|
+
|
126
|
+
|
68
127
|
|
69
128
|
## Template local files
|
70
129
|
|
71
|
-
Templates are stored into `templates/` directory.
|
130
|
+
Templates are stored by defualt into `templates/` directory.
|
131
|
+
|
132
|
+
You can control this by setting the MANDRILL_TEMPLATES_DIR environment variable.
|
133
|
+
|
134
|
+
```
|
135
|
+
export MANDRILL_TEMPLATES_DIR=mandrill_templates
|
136
|
+
```
|
72
137
|
|
73
138
|
```
|
74
139
|
templates/
|
75
|
-
├── ${
|
76
|
-
│ ├── code
|
140
|
+
├── ${template_slug1}
|
141
|
+
│ ├── code.html # code contents
|
77
142
|
│ ├── metadata.yml # metadata of template
|
78
|
-
│ └── text
|
79
|
-
├── ${
|
80
|
-
│ ├── code
|
143
|
+
│ └── text.txt # text contents
|
144
|
+
├── ${template_slug2}
|
145
|
+
│ ├── code.html
|
81
146
|
│ ├── metadata.yml
|
82
|
-
│ └── text
|
83
|
-
└── ${
|
84
|
-
├── code
|
147
|
+
│ └── text.txt
|
148
|
+
└── ${template_slug3}
|
149
|
+
├── code.html
|
85
150
|
├── metadata.yml
|
86
|
-
└── text
|
151
|
+
└── text.txt
|
87
152
|
```
|
88
153
|
|
89
154
|
example of metadata.yml
|
@@ -97,7 +162,7 @@ from_email: test@example.com
|
|
97
162
|
from_name: Boss
|
98
163
|
```
|
99
164
|
|
100
|
-
## Optional: render
|
165
|
+
## Optional: render supports Handlebars preview.
|
101
166
|
|
102
167
|
If you would like to use handlebars template.
|
103
168
|
You should add handlebars rubygem to Gemfile.
|
@@ -114,7 +179,7 @@ It will be compiled as Handlebars Template.
|
|
114
179
|
|
115
180
|
```
|
116
181
|
Usage:
|
117
|
-
mandrilltemplate render
|
182
|
+
mandrilltemplate render SLUG [PARAMS_FILE]
|
118
183
|
|
119
184
|
Options:
|
120
185
|
[--handlebars], [--no-handlebars]
|
@@ -9,75 +9,87 @@ autoload "Handlebars", 'handlebars'
|
|
9
9
|
|
10
10
|
class MandrillTemplateManager < Thor
|
11
11
|
include Thor::Actions
|
12
|
-
VERSION = "0.
|
12
|
+
VERSION = "0.3.0"
|
13
13
|
|
14
|
-
desc "
|
15
|
-
def
|
16
|
-
|
14
|
+
desc "export_all", "export all templates from remote to local files."
|
15
|
+
def export_all
|
16
|
+
remote_templates = MandrillClient.client.templates.list
|
17
|
+
remote_templates.each do |template|
|
18
|
+
export(template["slug"])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "export SLUG", "export template from remote to local files."
|
23
|
+
def export(slug)
|
24
|
+
template = MandrillClient.client.templates.info(slug)
|
17
25
|
meta, code, text = build_template_for_export(template)
|
18
|
-
save_as_local_template(
|
26
|
+
save_as_local_template(meta, code, text)
|
19
27
|
end
|
20
28
|
|
21
|
-
desc "upload
|
22
|
-
|
23
|
-
|
29
|
+
desc "upload SLUG", "upload template to remote as draft."
|
30
|
+
option :publish, type: :boolean, default: false, aliases: :p
|
31
|
+
def upload(slug)
|
32
|
+
template = MandrillTemplate::Local.new(slug)
|
24
33
|
if template.avail
|
25
34
|
upload_template(template)
|
35
|
+
publish(slug) if options[:publish]
|
26
36
|
else
|
27
|
-
puts "Template data not found #{
|
37
|
+
puts "Template data not found #{slug}. Please generate first."
|
28
38
|
end
|
29
39
|
end
|
30
40
|
|
31
|
-
desc "delete
|
32
|
-
|
41
|
+
desc "delete SLUG", "delete template from remote."
|
42
|
+
option :delete_local, type: :boolean, default: false
|
43
|
+
def delete(slug)
|
33
44
|
begin
|
34
|
-
result = MandrillClient.client.templates.delete(
|
45
|
+
result = MandrillClient.client.templates.delete(slug)
|
35
46
|
puts result.to_yaml
|
36
47
|
rescue Mandrill::UnknownTemplateError => e
|
37
48
|
puts e.message
|
38
49
|
end
|
50
|
+
delete_local_template(slug) if options[:delete_local]
|
39
51
|
end
|
40
52
|
|
41
|
-
desc "generate
|
42
|
-
def generate(
|
43
|
-
new_template = MandrillTemplate::Local.new(
|
53
|
+
desc "generate SLUG", "generate new template files."
|
54
|
+
def generate(slug)
|
55
|
+
new_template = MandrillTemplate::Local.new(slug)
|
44
56
|
puts new_template.class
|
45
57
|
meta, code, text = build_template_for_export(new_template)
|
46
|
-
save_as_local_template(
|
58
|
+
save_as_local_template(meta, code, text)
|
47
59
|
end
|
48
60
|
|
49
|
-
desc "publish
|
50
|
-
def publish(
|
51
|
-
puts MandrillClient.client.templates.publish(
|
61
|
+
desc "publish SLUG", "publish template from draft."
|
62
|
+
def publish(slug)
|
63
|
+
puts MandrillClient.client.templates.publish(slug).to_yaml
|
52
64
|
end
|
53
65
|
|
54
|
-
desc "render
|
66
|
+
desc "render SLUG [PARAMS_FILE]", "render mailbody from local template data. File should be Array. see https://mandrillapp.com/api/docs/templates.JSON.html#method=render."
|
55
67
|
option :handlebars, type: :boolean, default: false
|
56
|
-
def render(
|
68
|
+
def render(slug, params = nil)
|
57
69
|
merge_vars = params ? JSON.parse(File.read(params)) : []
|
58
|
-
template = MandrillTemplate::Local.new(
|
70
|
+
template = MandrillTemplate::Local.new(slug)
|
59
71
|
if template.avail
|
60
72
|
if options[:handlebars]
|
61
73
|
handlebars = Handlebars::Context.new
|
62
74
|
h_template = handlebars.compile(template['code'])
|
63
75
|
puts h_template.call(localize_merge_vars(merge_vars))
|
64
76
|
else
|
65
|
-
result = MandrillClient.client.templates.render template.
|
66
|
-
[{"content"=>template["code"], "name"=>template.
|
77
|
+
result = MandrillClient.client.templates.render template.slug,
|
78
|
+
[{"content"=>template["code"], "name"=>template.slug}],
|
67
79
|
merge_vars
|
68
80
|
puts result["html"]
|
69
81
|
end
|
70
82
|
else
|
71
|
-
puts "Template data not found #{
|
83
|
+
puts "Template data not found #{slug}. Please generate first."
|
72
84
|
end
|
73
85
|
end
|
74
86
|
|
75
|
-
desc "list", "show template list both of remote and local."
|
87
|
+
desc "list [LABEL]", "show template list both of remote and local [optionally filtered by LABEL]."
|
76
88
|
option :verbose, type: :boolean, default: false, aliases: :v
|
77
|
-
def list
|
89
|
+
def list(label = nil)
|
78
90
|
puts "Remote Templates"
|
79
91
|
puts "----------------------"
|
80
|
-
remote_templates = MandrillClient.client.templates.list
|
92
|
+
remote_templates = MandrillClient.client.templates.list(label)
|
81
93
|
remote_templates.map! do |template|
|
82
94
|
template["has_diff"] = has_diff_between_draft_and_published?(template)
|
83
95
|
template
|
@@ -117,7 +129,7 @@ class MandrillTemplateManager < Thor
|
|
117
129
|
puts "Local Templates"
|
118
130
|
puts "----------------------"
|
119
131
|
Formatador.display_compact_table(
|
120
|
-
collect_local_templates,
|
132
|
+
collect_local_templates(label),
|
121
133
|
[
|
122
134
|
"name",
|
123
135
|
"slug",
|
@@ -142,6 +154,7 @@ class MandrillTemplateManager < Thor
|
|
142
154
|
def build_template_for_export(t)
|
143
155
|
[
|
144
156
|
{
|
157
|
+
"name" => t['name'],
|
145
158
|
"slug" => t['slug'],
|
146
159
|
"labels" => t['labels'],
|
147
160
|
"subject" => t['subject'],
|
@@ -153,19 +166,27 @@ class MandrillTemplateManager < Thor
|
|
153
166
|
]
|
154
167
|
end
|
155
168
|
|
156
|
-
def
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
169
|
+
def templates_directory
|
170
|
+
MandrillClient.templates_directory
|
171
|
+
end
|
172
|
+
|
173
|
+
def save_as_local_template(meta, code, text)
|
174
|
+
dir_name = meta['slug']
|
175
|
+
empty_directory File.join(templates_directory, dir_name)
|
176
|
+
create_file File.join(templates_directory, dir_name, "metadata.yml"), meta.to_yaml
|
177
|
+
create_file File.join(templates_directory, dir_name, "code.html"), code
|
178
|
+
create_file File.join(templates_directory, dir_name, "text.txt"), text
|
161
179
|
end
|
162
180
|
|
163
|
-
def collect_local_templates
|
181
|
+
def collect_local_templates(label = nil)
|
164
182
|
local_templates = []
|
165
|
-
dirs = Dir.glob("
|
183
|
+
dirs = Dir.glob("#{ templates_directory }/*").map {|path| path.split(File::SEPARATOR).last}
|
166
184
|
dirs.map do |dir|
|
167
185
|
begin
|
168
|
-
|
186
|
+
template = MandrillTemplate::Local.new(dir)
|
187
|
+
if label.nil? || template['labels'].include?(label)
|
188
|
+
local_templates << template
|
189
|
+
end
|
169
190
|
rescue
|
170
191
|
next
|
171
192
|
end
|
@@ -173,13 +194,22 @@ class MandrillTemplateManager < Thor
|
|
173
194
|
local_templates
|
174
195
|
end
|
175
196
|
|
197
|
+
def delete_local_template(slug)
|
198
|
+
template = MandrillTemplate::Local.new(slug)
|
199
|
+
if template.avail
|
200
|
+
template.delete!
|
201
|
+
else
|
202
|
+
puts "Local template data not found #{slug}."
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
176
206
|
def upload_template(t)
|
177
|
-
if remote_template_exists?(t.
|
207
|
+
if remote_template_exists?(t.slug)
|
178
208
|
method = :update
|
179
209
|
else
|
180
210
|
method = :add
|
181
211
|
end
|
182
|
-
result = MandrillClient.client.templates.send(method, t.
|
212
|
+
result = MandrillClient.client.templates.send(method, t.slug,
|
183
213
|
t['from_email'],
|
184
214
|
t['from_name'],
|
185
215
|
t['subject'],
|
@@ -191,9 +221,9 @@ class MandrillTemplateManager < Thor
|
|
191
221
|
puts result.to_yaml
|
192
222
|
end
|
193
223
|
|
194
|
-
def remote_template_exists?(
|
224
|
+
def remote_template_exists?(slug)
|
195
225
|
begin
|
196
|
-
MandrillClient.client.templates.info(
|
226
|
+
MandrillClient.client.templates.info(slug)
|
197
227
|
true
|
198
228
|
rescue Mandrill::UnknownTemplateError
|
199
229
|
false
|
@@ -1,16 +1,17 @@
|
|
1
1
|
require 'mandrill'
|
2
2
|
require 'yaml'
|
3
|
+
require 'fileutils'
|
3
4
|
|
4
5
|
module MandrillTemplate
|
5
6
|
class Local < Hash
|
6
|
-
attr_reader :
|
7
|
+
attr_reader :slug, :avail
|
7
8
|
|
8
|
-
def initialize(
|
9
|
-
@
|
10
|
-
meta, code, text = load_data(
|
9
|
+
def initialize(slug)
|
10
|
+
@slug = slug
|
11
|
+
meta, code, text = load_data(slug)
|
11
12
|
|
12
|
-
self['name'] = name
|
13
|
-
self['slug'] = meta['slug'] ||=
|
13
|
+
self['name'] = meta['name'] ||= slug
|
14
|
+
self['slug'] = meta['slug'] ||= slug
|
14
15
|
self['from_email'] = meta['from_email'] ||= nil
|
15
16
|
self['from_name'] = meta['from_name'] ||= nil
|
16
17
|
self['subject'] = meta['subject'] ||= nil
|
@@ -20,13 +21,17 @@ module MandrillTemplate
|
|
20
21
|
self['text'] = text ||= nil
|
21
22
|
end
|
22
23
|
|
23
|
-
def
|
24
|
-
|
24
|
+
def templates_directory
|
25
|
+
MandrillClient.templates_directory
|
26
|
+
end
|
27
|
+
|
28
|
+
def load_data(slug)
|
29
|
+
if Dir.exists?(File.join(templates_directory, slug))
|
25
30
|
@avail = true
|
26
|
-
code = File.read(File.join(
|
27
|
-
text = File.read(File.join(
|
31
|
+
code = File.read(File.join(templates_directory, slug, "code.html"))
|
32
|
+
text = File.read(File.join(templates_directory, slug, "text.txt"))
|
28
33
|
[
|
29
|
-
YAML.load_file(File.join(
|
34
|
+
YAML.load_file(File.join(templates_directory, slug, "metadata.yml")),
|
30
35
|
code.empty? ? nil : code,
|
31
36
|
text.empty? ? nil : text
|
32
37
|
]
|
@@ -34,5 +39,13 @@ module MandrillTemplate
|
|
34
39
|
[{}, nil, nil]
|
35
40
|
end
|
36
41
|
end
|
42
|
+
|
43
|
+
def delete!
|
44
|
+
dir_name = File.join(templates_directory, slug)
|
45
|
+
puts dir_name
|
46
|
+
if Dir.exists?(dir_name)
|
47
|
+
FileUtils.rm_rf(dir_name)
|
48
|
+
end
|
49
|
+
end
|
37
50
|
end
|
38
51
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mandrill-template-manager
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sawanoboly
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|