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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f0f07ce0a0fdd67bb48350b6b8704f4dd12aa096
4
- data.tar.gz: 54efe14bbf3fe59062a17370517cb16408a31c7e
3
+ metadata.gz: f266294f177aacc1b9526a166683283d174b3f41
4
+ data.tar.gz: 2325b885208b5dd04fc6f8f1693baa119e8ebca5
5
5
  SHA512:
6
- metadata.gz: 8a2b2400323faf0811dcd3056b5176b1ee7052a03232bf79fa9f18c4d8c196e6832a3566eaa67c15a2c8b630cc73c2ccb6db5ed64e8c3fdaa3e392738f56822c
7
- data.tar.gz: 0e27fea8408dfdd325126442a0eab8837b59c487c212ec5572f5e59bb4e29b81a12c7310937785a824f8ad22cb51fd2a6a97ac2d53c0b96828686a754377456f
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
- $ ./bin/mandrilltemplate
18
+ $ bundle install
19
+ ```
20
+
21
+ ```
22
+ $ bundle exec mandrilltemplate
19
23
  Commands:
20
- mandrilltemplate delete NAME # delete template from remote.
21
- mandrilltemplate export NAME # export template from remote to local files.
22
- mandrilltemplate generate NAME # generate new template files.
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 NAME # publish template from draft.
26
- mandrilltemplate render NAME [PARAMS_FILE] # render mailbody from local template data. File should be Array. see https://mandrillapp.com/api/docs/templates.JSON.html#method=render.
27
- mandrilltemplate upload NAME # upload template to remote as draft.
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 controle system.
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
- Next, check by list subcommand.
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
- ├── ${template_name1}
76
- │ ├── code # code contents
140
+ ├── ${template_slug1}
141
+ │ ├── code.html # code contents
77
142
  │ ├── metadata.yml # metadata of template
78
- │ └── text # text contents
79
- ├── ${template_name2}
80
- │ ├── code
143
+ │ └── text.txt # text contents
144
+ ├── ${template_slug2}
145
+ │ ├── code.html
81
146
  │ ├── metadata.yml
82
- │ └── text
83
- └── ${template_name3}
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 suppots Handlebars preview.
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 NAME [PARAMS_FILE]
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.2.2"
12
+ VERSION = "0.3.0"
13
13
 
14
- desc "export NAME", "export template from remote to local files."
15
- def export(name)
16
- template = MandrillClient.client.templates.info(name)
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(name, meta, code, text)
26
+ save_as_local_template(meta, code, text)
19
27
  end
20
28
 
21
- desc "upload NAME", "upload template to remote as draft."
22
- def upload(name)
23
- template = MandrillTemplate::Local.new(name)
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 #{name}. Please generate first."
37
+ puts "Template data not found #{slug}. Please generate first."
28
38
  end
29
39
  end
30
40
 
31
- desc "delete NAME", "delete template from remote."
32
- def delete(name)
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(name)
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 NAME", "generate new template files."
42
- def generate(name)
43
- new_template = MandrillTemplate::Local.new(name)
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(name, meta, code, text)
58
+ save_as_local_template(meta, code, text)
47
59
  end
48
60
 
49
- desc "publish NAME", "publish template from draft."
50
- def publish(name)
51
- puts MandrillClient.client.templates.publish(name).to_yaml
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 NAME [PARAMS_FILE]", "render mailbody from local template data. File should be Array. see https://mandrillapp.com/api/docs/templates.JSON.html#method=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(name, params = nil)
68
+ def render(slug, params = nil)
57
69
  merge_vars = params ? JSON.parse(File.read(params)) : []
58
- template = MandrillTemplate::Local.new(name)
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.name,
66
- [{"content"=>template["code"], "name"=>template.name}],
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 #{name}. Please generate first."
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 save_as_local_template(name, meta, code, text)
157
- empty_directory File.join("templates", name)
158
- create_file File.join("templates", name, "metadata.yml"), meta.to_yaml
159
- create_file File.join("templates", name, "code"), code
160
- create_file File.join("templates", name, "text"), text
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("templates/*").map {|path| path.split(File::SEPARATOR).last}
183
+ dirs = Dir.glob("#{ templates_directory }/*").map {|path| path.split(File::SEPARATOR).last}
166
184
  dirs.map do |dir|
167
185
  begin
168
- local_templates << MandrillTemplate::Local.new(dir)
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.name)
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.name,
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?(name)
224
+ def remote_template_exists?(slug)
195
225
  begin
196
- MandrillClient.client.templates.info(name)
226
+ MandrillClient.client.templates.info(slug)
197
227
  true
198
228
  rescue Mandrill::UnknownTemplateError
199
229
  false
@@ -2,6 +2,10 @@ require 'mandrill'
2
2
  require 'mandrill_template/client'
3
3
 
4
4
  module MandrillClient
5
+ def self.templates_directory
6
+ ENV['MANDRILL_TEMPLATES_DIR'] || "templates"
7
+ end
8
+
5
9
  def self.client
6
10
  Mandrill::API.new ENV['MANDRILL_APIKEY']
7
11
  end
@@ -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 :name, :avail
7
+ attr_reader :slug, :avail
7
8
 
8
- def initialize(name)
9
- @name = name
10
- meta, code, text = load_data(name)
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'] ||= name
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 load_data(name)
24
- if Dir.exists?(File.join("templates", name))
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("templates", name, "code"))
27
- text = File.read(File.join("templates", name, "text"))
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("templates", name, "metadata.yml")),
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
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "mandrill-template-manager"
7
- spec.version = "0.2.2"
7
+ spec.version = "0.3.0"
8
8
  spec.authors = ["sawanoboly"]
9
9
  spec.email = ["sawanoboriyu@higanworks.com"]
10
10
 
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.2.2
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: 2015-09-10 00:00:00.000000000 Z
11
+ date: 2016-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor